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:   …

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.…

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 <<…

Virtual Function

Function is a virtual function when the virtual keyword is added in the beginning of function declaration.virtual void display();virtual keyword is required in declaration. In function definition, virtual keyword is not required. When you want to override(redefine) the function in derived class, then virtual function is required. This means virtual function works only when inheritance is involved. Let us understand with the help of an example. class shape {public: virtual void draw() {} virtual void func() {}};class rectangle: public shape {public: void draw() { cout << “Draw rectangle” << endl;…

Dynamic Cast

This is one of the type casting provided by C++. Let us understand with the help of an example. #include <iostream>using namespace std;#define MAX_LENGTH 50class person { unsigned int m_age; char m_name[MAX_LENGTH];public: person(unsigned int age, char* name) { m_age = age;    int len = strlen(name); strncpy(m_name, name, len); m_name[len+1] = ‘\0’; } virtual ~person(){}};class student : public person { unsigned int m_rollNumber;public: student(unsigned int age, char* name, unsigned int rollNumber):person(age, name),m_rollNumber(rollNumber) { } ~student(){}};class teacher: public person {unsigned int m_salary;public: teacher(unsigned int age, char* name, unsigned int salary):person(age, name),m_salary(salary) {…