A Tour of C++


My Notes on the C++ language based on the book - A tour of C++ by Stroustoup, various CPPCON videos and stack overflow articles.

Reference

Prerequisites 

before we proceed

Processor's Computational Model

Assembly Language <A little explanation about the assembly language>

HELlo world

#include <iostream>

using namespace std;

int main()

{

    cout << "Hello World!" << endl;

    return 0;

}

Compiling the program

Save the above program in a file called (say) main.cpp and compile it as following:

g++ main.cpp -o hello_world_exe

The above command should create an executable by the name 'hello_world_exe' in the same directory. 

Run the above executable as (you may omit the ./ depending on your PATH setting)

./hello_world_exe

Statically typed language

C++ is a statically typed, compiled language.

It means that each variable/expression has a type associated with it and the type defines the operations, which can be performed on the type. 

For example:

int x = 0; // x is of type int

x = "some_string"; // ERROR! Cannot assign const char* to int (both types are different)


In a dynamically typed language, however, the above can be rewritten as:


x = 0

x = "some_string" # No Error here. Type of x changes from int to str

Compilation

C++ is a compiled language. 

A compiler is a software that converts a source code from one language to destination language. The source and destination language can be same in certain cases (more on this later).

A processor understands machine instructions (not even assembly) and a compiler (for a given language, given OS and given HW) converts from the source language to the machine instruction possibly through a set of tool chain. Note that compilation for C++ is  not a single step process. 

A typical C++ compiler, say g++, usually has the following stages

Input C++ file -> Preprocessor -> Tokenizer -> Syntax Analyser (+Semantic analysis) -> Optimizers -> I[nstruction Selection -> Instruction Scheduling?] -> Register Allocation -> Code generation -> assembly output file

assembly output file -> assembler -> object code

object code -> linker -> machine code/executable (specific to an OS and HW. This file is NOT portable!)

All the above steps are implicitly handled by g++ (simplifying things here! More on separate compilation and linking later)

Why are executables os dependAnt?

https://stackoverflow.com/questions/49426747/why-are-c-executables-platform-dependant

Exercise

For the "Hello World" program above, check the following:

g++ -E main.cpp (Output will be printed on the terminal. You may redirect it to file as: g++ -E main.cpp |& tee preprocessed.cpp. Note that preprocessed.cpp is a valid C++ file)

fundamental Data types 

DO NOTE that a lot of things on data types are "implementation defined". Be sure to not make any assumptions! Such as sizeof(int) is always 4.

Arithmetic types

bool

char

Must be 1 byte (i.e. at least 8 bits. Note that some machines may have more than 8 bits per byte)

int

long long

double

long double

void

pointer

array

reference