C++ : Complete Developer Guide — Part 1

C++ Fast development reference khizaruddins, khizaruddins.medium.com mother of programming language
Photo by Alex Pasarelu on Unsplash

Introduction:

C++ was developed by Danish computer Scientist Bjarne Stroustrup at Bell Labs since 1979 as an extension of the C language.

Some terminologies for understanding:

  1. Compiler: A compiler takes program and converts it into machine executable code, it takes whole program file and compiles it.
  2. Interpreter: An interpreter takes program and interprets into machine code, it takes one line as an input and interprets it, one line or statement at a time.
  3. Assembler: An assembler takes assembly language and converts into machine code.
  4. Linker: It is a computer program that links and merges various object files together in order to make an executable file.
  5. Loader: It is a part of operating system and is responsible for loading executable files into memory and execute them, calculating the size of a program (instructions and data) and creates memory space for it are some more features.
C++ fast development reference, see plus plus, c++ khizaruddins, khizaruddins.medium.com
Photo by David Travis on Unsplash
c++ fast development reference static type khizaruddins khizaruddins.medium.com
Photo by Amos from Stockphotos.com on Unsplash

1. Static typed:

Statically typed languages are those which does type checking at compile time. Without type definition these programs give compile time errors.

C++ fast development reference khizaruddins khizaruddins.medium.com dynamic type
Photo by Fraser Cottrell on Unsplash

2.Dynamic typed:

Dynamic typed languages are those which does type checking at run time.

C++ fast development reference, khizaruddins, khizaruddins.medium.com strong typed
Photo by Clay Banks on Unsplash

3. Strongly Typed:

In strongly typed languages two unrelated type of data cannot be converted implicitly. i.e. two different types of data cannot be merged into a single type of data.

int a, res;
string b;
res = a + b; // <- this will give compile time error
C++ fast development reference khizaruddins, khizaruddins.medium.com weak type
Photo by Erika Fletcher on Unsplash

4. Weakly typed (loosely type):

In weakly typed languages two unrelated type of data can be converted implicitly. i.e. two different types of data can be merged into a single type of data. for eg. Javascript

let a = 0;
let b = "Hello";
let res = a + b; // no error
C++ fast development reference khizaruddins khizaruddins.medium.com, setting c++ up
Photo by Adi Goldstein on Unsplash

SETUP Environment C++:

  1. Windows: To set up c++ on windows Pls go through this link.
  2. Mac OS: To set up c++ on mac pls go through this link.
  3. Linux Ubuntu: To set up c++ on ubuntu pls go through this link.
#include <iostream>int main() {  std::cout << "Hello world" << std::endl;  return 0;}
Output: Hello world
#include <iostream>using namespace std;int main() {  cout << "Hello world" << endl;  return 0;}
Photo by David Pupaza on Unsplash

Types of Errors in code:

1. Compile time errors:

Errors which occurs at the time of compilation, when we violate the rule of writing syntax then this error is thrown. This type of error are highlighted by compiler line number and are easy to find.

#include <iostream>using namespace std;int main(){  int x = 10;  int y = 15;  // semicolon missed  cout << " "<< (x, y);}
error: expected ';' before '}' token

2. Runtime errors:

Errors which occurs at the runtime, most common runtime error is divide by zero error. This type of error are not highlighted by compiler and are hard to find.

#include <iostream>using namespace std;int main(){int n = 9, div = 0;   // wrong logic   // number is divided by 0,   // so this program abnormally terminates   div = n/0;   cout <<"result = " << div;}
warning: division by zero [-Wdiv-by-zero]
div = n/0;

3. Logical Errors:

The desire output is not obtained after compilation and running code are logical errors. Logical errors are errors due to wrong logical implementation, for this we need to debug our code to find out the root cause.

#include <iostream>using namespace std;int main() {  for (int i = 1; i < 10; i++){    cout << i;  }  return 0;}
1 2 3 4 5 6 7 8 9

4. Linker errors:

These error occurs when after compilation we link the different object files with main’s object. These are errors generated when the executable of the program cannot be generated. This may be due to wrong function prototyping, incorrect header files. One of the most common linker error is writing Main() instead of main().

5. Resource errors:

Resource errors are those when we try to open a file in program and that file does not exist or some header file which we try to import and those header file does not exist.

Variables and its Types:

Programs are user interaction which takes user input performs some calculation or executes according to logic given and gives an output.

c++ fast development reference khizaruddins, khizaruddins.medium.com container variable and types
Photo by TAN Erica on Unsplash

Data types are divided into three types:

1. Primitive data types:

These data types are built-in or predefined data types and can be used directly by the user to declare variables. example: int, char , float, bool etc

2. Derived data types:

These data types are derived from primitive or built-in data types which are four only function, array, pointer, reference.

3. Abstract or user defined data type:

These data types are defined by user itself. Like, defining a class in C++ or a structure. Class, Structure, Union, Enumeration, Typedef defined DataType are some example.

#include <iostream>int main() { int a;}
int age;
string name;

Initialisation of variable:

The very first time when a value is set on a variable, then it is called variable initialisation.

int count; // variable declarationcount = 0; // variable initialisation
int count = 0;
  1. Camel case: first letter of word remains small and first letter of second word will be capital joining both words without spaces. for eg. firstName, lastName.
  2. Snake case: Spaces between words will be replaced with underscore (_) and all letters will be lower case letters. For eg first_name, last_name.
  3. Kebab case: Spaces between words will be replaced with hyphen (-) and all letters will be lower case letters. For eg first-name, last-name. Mostly this case is not used in programming language instead it is used in Frontend CSS for class name and ids in xml and in lisp language as well.
  4. Pascal case: All letters of all words will be capitalise and it will be joined with no spaces. For eg FirstName, LastName.
  5. Upper snake case: All letters of all words will be upper case letters two or more words are separated with an underscore(_) for eg. FIRST_NAME, LAST_NAME. Usually these are used to store some constants like for eg api key. for eg. API_KEY, FIREBASE_KEY.

Constants:

Constants are variables which are declared with values that cannot be altered or changed in later point of time, if the const value are changed after initialisation it will throw a compile time error.

#include <iostream>int main () {   const int abc; // constant declaration   const float PI = 3.14; // constant declaration with
// initialisation
}

Control flow:

It is important to know what control flow is in programming language.

#include <iostream>using namespace std;int main() {  int c, a, b;  cout << "Enter two numbers" << endl;  cin >> a >> b;  c = a + b;  cout << "the sum of two number is " << c << endl;  return 0;}

Comments:

Comments are programmer readable explanations in the code, such explanation conveys what the program does, comments are never compiled and it gets striped out, before the compiler starts seeing program.

#include <iostream>using namespace std;int main () {   int a; // single line comment this is not read by compiler    /*
this is multiline comment
i.e. everything inside this is for developers to read
which is not seen by compiler
*/ return 0;}
#include <iostream>int main() {   int c, a, b;

// addition of a and b
c = a + b; cout << "the sum is:\n" << c; return 0;
}
#include <iostream>int main() {int c, a, b;

// addition of a and b <- needs to update comment to subtraction
c = a - b; cout << "the sum is:\n" << c; return 0;
}

TLDR;

For more tech related blogs pls visit:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store