Pure Virtual function-Abstract class

Let us try to understand pure virtual function with the help of an example. #include <iostream>using namespace std;class shape {public:    virtual void calculateArea() {}};class rectangle: public shape {public:    void calculateArea() {        cout << “Rectangle Area calculation” << endl;    }}; In the above example, calculateArea function is overridden in rectangle class. In shape class, definition is empty since shape class does not know what to calculate. Can we say that empty definition of calculateArea function in shape class is not required. Instead, anyone who inherit shape class must redefine or override…

Multiple Inheritance

In multiple inheritance, multiple classes are inherited i.e., when class needs properties of two or more classes and follows “Is-A” relationship as well. Let us take an example to understand the concept of multiple inheritance. #include <iostream>using namespace std;class waterHabitat{public:    waterHabitat() {        cout << “water Habitat c’tor” << endl;    }    ~ waterHabitat() {        cout << “water Habitat d’tor” << endl;    }};class landHabitat{public:    landHabitat() {        cout << “land Habitat c’tor” << endl;    }    ~ landHabitat() {        cout << “land Habitat d’tor” << endl;    }};class amphibian : public waterHabitat, public landHabitat {public:   …

Overloaded new and delete operator for arrays

Let us try to understand the overloading of new and delete operator for arrays using below example. #include <iostream>using namespace std;void* operator new(size_t sz) {    cout << “Global Memory Allocation: ” << sz << endl;    void* ptr = malloc(sz);    return ptr;}void operator delete(void* ptr) {    cout << “Global memory Deallocation” << endl;    free(ptr);}class Memory {    enum { mem_size = 10 };    int arr[mem_size];public:    Memory() {        cout << “Memory C’tor” << endl;    } ~Memory() {        cout << “Memory D’tor” << endl;    }    void* operator new(size_t sz) {        cout << “Class Memory…

Inheritance

Let us try to understand the basics of inheritance with the help of below example. In this, we have created one class viz. person. This class has some basic information like age and name of a person. Now, let’s say that I want to create a student class. First option is that all the information we keep in the student class like age, name, roll number and standard. Other option is to keep the basic or common information in one class and use this class to form a student class.…

Overload new and delete operator

new operator allocates the memory then calls the constructor. delete operator calls the destructor then delete the memory. As these are operators, so it can be overloaded. But why we need to overload these operators.* In case, there is lot of allocation and deallocation happens from the heap memory, it can affect the performance. So, to overcome this, we overload the new and delete operator.* Also, as part of above scenario, heap memory gets fragmented. As a result, even though there is sufficient memory present in heap, still it is…

new and delete operator

new operator is used to allocate memory in runtime. This memory is allocated from heap segment of a process. To understand how new operator works, let us see with the help of an example. #include <iostream>using namespace std;class employee {    unsigned short m_age;    unsigned int m_salary;public:    employee() {}    employee(unsigned short age, unsigned int salary) {        m_age = age;        m_salary = salary;    }};int main() {    employee* emp = new employee(30, 40,000);    delete emp;    return 0;} Here new is called for the employee object by passing the value as per the constructor…

Function Overloading

To understand function overloading, we need to understand the concept of polymorphism. Polymorphism means having many or multiple forms. There are two type of polymorphism.1) Static Polymorphism2) Dynamic Polymorphism Function overloading comes under static Polymorphism. Function overloading means functions with same name but different number or type of arguments.Let us understand with the help of example. #include <iostream>using namespace std;class base {public: void display(int num) { cout << “Num: ” << num << endl; } void display(int num1, int num2 = 10) { cout << “Num1: ” << num1 <<…

Composition

Every big thing is made up of small-small things. For example, a house is built from walls, ceiling, and doors. Now in terms of C++, we can say that every complex object is built from small or simple object. This process of creating a complex object from simple and small objects is called composition in C++. In other words, whenever there is “has-a” relationship, then composition comes in to picture. E.g., house has four walls, two doors and one ceiling. Let us try to understand the concept of composition with…

Explicit conversion constructor

Consider an example below to understand the concept of explicit constructor #include <iostream>using namespace std;class person {private: unsigned int m_age; char m_gender;public: person (unsigned int age = 40, char gender = ‘M’); bool operator==(const person& other) { cout << “Comparison”<< endl; if ((other.m_age == m_age) && (other.m_gender == m_gender)) { return true; } return false; }};person::person(unsigned int age, char gender){ cout << “C’tor” << endl; m_age = age; m_gender = gender;}int main() { person p(40, ‘M’); person p1 = 40; //Implicit conversion if ( p == 40) { //Implicit conversion…

Constructor & Destructor

Constructor :As name suggest, it construct an object. Constructor is used to initialize the data members of a class.Consider a class below: #include <iostream>#include <string> using namespace std;class employeeData { unsigned int m_salary; char* m_name;public: ~ employeeData() { //Destructor if (m_name) { delete []m_name; } } employeeData() { // default constructor cout << “employeeData default C’tor” << endl; m_salary = 0; m_name = NULL; } employeeData(unsigned int salary, char* name) { //Parameterized constructor cout << “employeeData parameterized C’tor” << endl; m_salary = salary; unsigned int len = strlen(name); m_name =…