Inheritance in C++Inheritance is a very important feature of object-oriented programming approach. Further, which provides the mechanism of reusability of classes.
Moreover, it is very important to reuse classes which are tested. And this idea also saves resources and increases the reliability of the software.
Basically, there are three types( Hybrid and Hierarchical are also but the are not basic types) of inheritance which are.
1- Single
2-Multiple
3-Multilevel
I will show each type of inheritance by example, the best real-life example of inheritance is a family structure in which son inherits the property of father and father inherits the property of grandfather. In examples, I will declare three classes Son, Father, Mother and GrandFather and I will make a program
based on these classes which demonstrate inheritance.
Syntax for derivation
class derived : visibility base
{
// Members of derived
};
Now, i am going to declare all four classes
class GrandFather
{
int a;
int b;
int c;
public:
int add(int, int);
};
class Father
{
int d;
public:
int subtract(int, int);
};
class Mother
{
int e;
public:
int multiply(int, int);
};
class Son
{
int f;
public:
float division(int, int);
};
______________________________
Example of single inheritance
class Father : public GrandFather
{
int d;
public:
int subtract(int, int);
};
Then actually Father class becomes
Class Father
{
int a;
int b;
int c;
int d;
public:
int add(int, int);
int subtract(int, int);
};
___________________________
Example of multiple inheritance
class Son : public Father, public Mother
{
int f;
public:
float division(int, int);
};
The actually Son class becomes after inherited properties from Father and Mother
class Son
{
int f;
int d;
int e;
public:
int subtract(int, int);
float division(int, int);
int multiply(int, int);
};
_______________________
Example of multilevel inheritance
class Father : public GrandFather
{
int a;
int b;
int c;
int d;
public:
int add(int, int);
int subtract(int, int);
};
Finally Son class becomes and the path of inheritance is Son<-Father<-GrandFather
class Son : public Father
{
int a;
int b;
int c;
int d;
int f;
public:
int add(int, int);
int subtract(int, int);
float division(int, int);
};
Conclusion- To sum up, I have explained inheritance hope it will be helpful for you.