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