Inheritance is the process by which object of one class acquires the properties of the object of another class.
The class from which properties are inherited is called base class and the class to which properties are inherited is called derived class. The main feature of inheritance is the reusability of code we don’t have to write the same code again.
Types of inheritance:
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
1. Single Inheritance: In a single inheritance the derived class is derived from a single base class. Let’s take an example.
#include<iostream.h> #include<conio.h> class A { public: void add() { int a,b,c; cout << "enter any number" << endl; cin >>a; cout << "enter any number" << endl; cin >>b; c=a+b; cout <<"add" << c << endl; } }; class B:public A { public: void sub() { int a,b,c; cout << "enter any number" << endl; cin >>a; cout << "enter any number" << endl; cin >>b; c=a-b; cout << "sub=" << c << endl; } }; void main() { clrscr(); B b1; b1.add(); b1.sub(); getch(); }
2. Multiple Inheritance- In multiple inheritance derived class is derived from more than one base class.
#include<iostream.h> #include<conio.h> class A { public: void add() { int a,b,c; cout << "enter any number" << endl; cin >>a; cout << "enter any number" << endl; cin >>b; c=a+b; cout << "add=" << c << endl; } }; class B { public: void sub() { int a,b,c; cout << "enter any number" << endl; cin >>a; cout << "enter any number" << endl; cin >>b; c=a-b; cout << "sub=" << c << endl; } }; class C:public A,public B { public: void div() { float a,b,c; cout << "enter any number" << endl; cin >>a; cout << "enter any number" << endl; cin >>b; c=a/b; cout <<"div=" << c << endl; } }; void main() { clrscr(); C c1; c1.add(); c1.sub(); c1.div(); getch(); }
3. Multilevel Inheritance: In multilevel inheritance, class B is derived from class A and class C is derived from class B.
#include<iostream.h> #include<conio.h> class A { public: void add() { int a,b,c; cout << "enter any number" << endl; cin >>a; cout << "enter any number" << endl; cin >>b; c=a+b; cout << "add=" << c << endl; } }; class B:public A { public: void sub() { int a,b,c; cout <<"enter any number" << endl; cin >>a; cout << "enter any number" << endl; cin >>b; c=a-b; cout << "sub="<< c << endl; } }; class C:public B { public: void div() { float a,b,c; cout << "enter any number" << endl; cin >>a; cout << "enter any number" << endl; cin >>b; c=a/b; cout << "div=" << c << endl; } }; void main() { clrscr(); C c1; c1.add(); c1.sub(); c1.div(); getch(); }
4. Hierarchical Inheritance: In hierarchical, multiple classes can be derived from a single base class.
#include<iostream.h> #include<conio.h> class A { public: void add() { int a,b,c; cout << "enter any number" << endl; cin >>a; cout << "enter any number" << endl; cin >>b; c=a+b; cout << c << endl; } }; class B:public A { public: void sub() { int a,b,c; cout << "enter any number" << endl; cin >>a; cout << "enter any number" << endl; cin >>b; c=a-b; cout << c << endl; } }; class C:public A { public: void div() { int a,b,c; cout << "enter any number" << endl; cin >>a; cout << "enter any number" << endl; cin >>b; c=a/b; cout << c << endl; } }; void main() { clrscr(); B b1; b1.add(); b1.sub(); C c1; c1.div(); getch(); }
5. Hybrid Inheritance- Hybrid inheritance is very important part of the inheritance because this inheritance is also known as Diamond problem.
It is the combination of multiple and multilevel inheritances.
#include<iostream.h> #include<conio.h> class first { public: void add() { cout << "addition performed" << endl; } }; class second: virtual public first { public: void sub() { cout << "subtraction performed" << endl; } }; class third:virtual public first { public: void div() { cout << "divide performed" << endl; } }; class fourth:public second,public third { public: void mul() { cout << "multiplication performed" << endl; } }; void main() { clrscr(); fourth f1; f1.add(); f1.sub(); f1.div(); f1.mul(); getch(); }
If you do not write the virtual keyword then there will be a compile-time error that is an ambiguous error.
There is a compile-time error because class fourth don’t know which method to call so you have to use the virtual keyword.