#include<iostream>
using namespace std;
class Dada{
public:
int d = 20;
};
class Papa : public Dada
{
public:
int a = 10; // d = 20;
};
class Beta : public Papa {
public:
int b = 5; // a = 10, d = 20
};
int main()
{
Beta obj;
cout<<obj.d;
return 0;
}
What is Inheritance ?
Inheritance allows a class to acquire properties and characteristics of another class.
In above example class Beta inherit class Papa.
class Beta : public Papa
And class Papa inherit class Dada.
class Papa : public Dada
Because of inheritance, object of Beta class can access variable of Papa class and variable of Dada class.
What are the types of Inheritance in C++ ?
- Single inheritance
- Multiple inheritance
- Multilevel inheritance
1. Single Inheritance
When there is only one Child class and only one Parent class.
2. Multiple Inheritance
When a child class have more than one parent class.
class Beta : public Papa, public Dada
3. Multilevel Inheritance
When there is more than one single parent class.
class Papa : public Dada
class Beta : public Papa