The Book of Modern C++

This book is a guidance for people moving from C to C++ to Modern C++.

Chapter 1

Hello world program

In C :

#include <stdio.h>

int main(void)
{
  printf("Hello World!\n");
}

In C++ :

#include <iostream>

int main()
{
  std::cout << "Hello World";
}

Chapter 2

Printing different values of char

In C :

Printing decimal value.

#include <stdio.h>

int main(void)
{
  char mychar;
  mychar = 'a';
  printf("Decimal: %d Octal: %o Hexadecimal: %X", mychar, mychar, mychar);
}

Priting ASCII value

#include <stdio.h>

int main(void)
{
  char mychar;
  mychar = 'a';
  printf("%c", mychar);
}

In C++ :

cout automatically prints ASCII value

#include <iostream>

int main()
{
  char mychar;
  mychar = 'a';
  std::cout << mychar;
}

Chapter 3

Printing different values of int

In C :

#include <stdio.h>

int main(void)
{
        int x = 123;
        int y = -256;
        printf("The value of an unsigned integer is: %d, %d", x, y);
}

However also provides support for unsigned int.

#include <stdio.h>

int main(void)
{
        unsigned int x = 123;
        unsigned int y = -256;
        printf("The value of an unsigned integer is: %u, %u", x, y);
}

In C++ :

#include <iostream>

int main()
{
        int x = 123;
        int y = -256;
        std::cout << "The value of x is: " << x << ", the value of y is: " << y;
}



Chapter 4

Printing different values of float

In C :

#include <stdio.h>

int main(void)
{
        float myfloat;
        myfloat = 123.456;
        printf("The value of a floating-point variable is: %f", myfloat);
}

In C++ :

#include <iostream>

int main()
{
        float myfloat;
        myfloat = 123.456;
        std::cout << "The value of d is: " << myfloat;
}
         

Chapter 5

Printing strings

In C :

#include <stdio.h>

int main(void)
{
        char* p = "Hello World!";
        printf("%s", p);
}
#include <stdio.h>

int main(void)
{
        char* p = "Hello World!";
        printf("%c", *p);
}

In C++ :

#include <iostream>
#include <string>

int main()
{
        std::string s = "Hello World.";
        std::cout << s;
}

Chapter 6

Pointer and References

In C :

Pointers in C

#include <stdio.h>

int main(void)
{
        int x = 123;
        printf("The value before the change: %d\n", x);
        int* p = &x;
        *p = 456;
        printf("The value after the change: %d\n", x);
}


When passing to function

#include <stdio.h>

// Double the number passed in as 'x'.
int double_number_a(int x) {
    return 2 * x;
}

// Double the number pointed to by 'x'.
void double_number_b(int* x) {
    *x *= 2;
}

int main() {
    int num = 5;
    
    printf("%d\n", double_number_a(num));
    printf("%d\n", num);

    double_number_b(&num);
    printf("%d\n", num);
    
    return 0;
}

In C++ :

Pointers in C++ remain same as in C.

#include <iostream>

int main()
{
        int x = 123;
        std::cout << "The value before the change: " << x;
        int* p = &x;
        *p = 456;
        std::cout << "The value after the change: " << x;
}

However we prefer to use references in C++ rather than raw pointers.

#include <iostream>

int main()
{
        int x = 123;
        int& y = x;
        x = 456;
        // both x and y now hold the value of 456
        y = 789;
        // both x and y now hold the value of 789
}

When passing to function

#include <iostream>

// Double the number passed in as 'x'.
int double_number_a(int x) {
    return 2 * x;
}

// Double the number pointed to by 'x'.
void double_number_b(int* x) {
    *x *= 2;
}

// Double the number pointed to by 'x'.
void double_number_c(int& x) {
    x *= 2;
}

int main() {
    auto num = 5;
    
    std::cout << double_number_a(num) << std::endl;
    std::cout << num << std::endl;
    
    double_number_b(&num);
    std::cout << num << std::endl;
    
    double_number_c(num);
    std::cout << num << std::endl;

    return 0;
}

Chapter 7

Array

In C :

#include <stdio.h>

int main() {
    int my_array[5];

    size_t len = sizeof(my_array) / sizeof(my_array[0]);
    for(size_t i = 0; i < len; i++) {
        my_array[i] = i;
    }

    printf("%d\n", my_array[2]);
    return 0;
}

In C++ :

#include <iostream>
#include <array>

int main() {
    std::array<int, 5> my_array;

    for(size_t i = 0; i < my_array.size(); i++) {
        my_array[i] = i;
    }

    std::cout << my_array[2] << std::endl;
    return 0;
}

Chapter 8

Loops

In C :

#include <stdio.h>

int main() {
    int a[] = {4, 5, 6, 7};
    for(int i = 1; i <= 3; i++)
    {
        printf("%d\n", a[i]);
    }
    return 0;
}

In C++ :

#include <iostream>

int main() {
    int a[] = {4, 5, 6, 7};

    for(auto i : a)
    {
        std::cout << i << std::endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
 
int main() {
    std::vector<int> v = {0, 1, 2, 3};
    for(auto i : v) { // access using const reference
        std::cout << i << std::endl;
    }
    return 0;
}

Chapter 9

Templates (Only in C++)

Function Templates :

#include <iostream>

template <class T>
T largest(T n1, T n2) {
    return (n1 > n2) ? n1 : n2;
}

int main() {
    int x = 2;
    int y = 3;
    std::cout << largest(x, y) << std::endl;
    return 0;
}

Chapter 10

Lambda Functions (Only in C++)

#include <iostream>

auto add = [] (int a, int b) {
   std::cout << "Sum = " << a + b;
};

int main() {

  add(10, 8);

  return 0;
}
#include <iostream>

auto multiply = [](int y)->int {
    return 2*y;
};


int main() {
    
    int x = 1;
    std::cout << multiply(x) << std::endl;
    
    x = 2;
    std::cout << multiply(x) << std::endl;

    return 0;
}

Chapter 11

Struct

In C :

#include <stdio.h>

struct my_struct {
    int x;
    int y;
};

int main() {
    struct my_struct object1;
    object1.x = 1;
    printf("%d\n", object1.x);
    return 0;
}

In C++ :

#include <iostream>

struct my_struct {
    int x;
    int y;
};

int main() {
    struct my_struct object1;
    object1.x = 1;
    std::cout << object1.x << std::endl;
    return 0;
}

Classes (In C++ Only) :


#include <iostream>

class human {
public:
    int height;
    int weight;
};

int main() {
    human obj;
    obj.height = 180;
    obj.weight = 220;

    std::cout << john.height << std::endl;

    return 0;
}

Chapter 12

Constructor & Destructor

In C++ :

#include <iostream>

class human {
public:
    int height;
    int weight;
    human(int h, int w) {
        height = h;
        weight = w;
    }
    ~human(){};
    int get_height() const {
        return height;
    }
    int get_weight() const {
        return weight;
    }
};

int main() {
    human obj(180, 220);
    std::cout << obj.get_height() << std::endl;
    std::cout << obj.get_weight() << std::endl;
    return 0;
}

Access control by Encapsulation


#include <iostream>

class human {
private: // abstracted from the user
    int m_h;
    int m_w;
public: 
    human(int h, int w) {
        m_h = h;
        m_w = w;
    }
    int get_h() const {
        return m_h;
    }
    int get_w() const {
        return m_w;
    }
    void set_h_w(int h, int w) {
        m_h = h;
        m_w = w;
    }

};

int main() {
    human obj(180, 220);

    std::cout << obj.get_h() << std::endl;
    std::cout << obj.get_w() << std::endl;
    
    obj.set_h_w(18, 22);
    
    std::cout << obj.get_h() << std::endl;
    std::cout << obj.get_w() << std::endl;

    return 0;
}

Inheritance



#include <iostream>

class human {
public:
    int height;
    int weight;
public: 
    human(int h, int w) {
        height = h;
        weight = w;
    }
    int get_height() const {
        return height;
    }
    int get_weight() const {
        return weight;
    }
};

class adult : public human { // inherit from human class
public:
    std::string occupation;
public:
    adult(int h, int w) : human(h, w) {
       occupation = "doctor"; 
    } // call the base class constructor from this constructor

    std::string get_occupation() const {
        return occupation;
    }   
};

int main() {
    adult obj(180, 220);  // object of derived class
    std::cout << obj.get_occupation() << std::endl;

    obj.occupation = "lawyer";
    
    std::cout << obj.get_height() << std::endl;
    std::cout << obj.get_weight() << std::endl;
    std::cout << obj.get_occupation() << std::endl;
    
    return 0;
}

Polymorphism


#include <iostream>

class human {
public:
    int height;
    float weight;
    
    human(int h, int w) {
        height = h;
        weight = w;
    }
};

//print with int argument
void print(int x) {
    std::cout << x << std::endl;
}

//print with float argument
void print(float x) {
    std::cout << x  << std::endl;
}

int main() {
    human obj(180, 220);
    
    print(obj.height);
    
    print(obj.weight);
    return 0;
}

Function Polymorphism


#include <iostream>

class human {
public:
    int height;
    int weight;
public: 
    human(int h, int w) {
        height = h;
        weight = w;
    }
    int get_height() const {
        return height;
    }
    int get_weight() const {
        return weight;
    }
    virtual void print_all() = 0; // ()=0 creates a _pure_ virtual function that must be overridden.
    //Alternatively you can just write a default function as a virtual function with no requirement to be overwritten.
};

class adult : public human { // inherit from human class
public:
    std::string occupation;
public:
    adult(int h, int w) : human(h, w) {
       occupation = "doctor"; 
    } // call the base class constructor from this constructor

    std::string get_occupation() const {
        return occupation;
    }   

    void print_all() { // virtual function overridden in derived class
        std::cout << height << std::endl;
        std::cout << weight << std::endl;
        std::cout << occupation << std::endl;
    }
};

int main() {
    adult obj(180, 220); // object of derived class
    std::cout << obj.get_occupation() << std::endl;
    
    obj.occupation = "lawyer";
    
    obj.print_all();
    return 0;
}

Chapter 13

Linked List

In C :

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

void printList(struct Node* head) {
    struct Node* curr = head;
    while (curr != NULL) {
        printf("%d -> ", curr->data);
        curr = curr->next;
    }
    printf("NULL\n");
}

int main() {
    struct Node* head = NULL;
    
    head = createNode(5);
    printList(head);


    return 0;
}