Inheritance in C++
Inheritance:
It is done by creating new classes, and reusing the properties of the existing ones. In C++, The mechanism of deriving a new class from an old one is called Inheritance. The old class is referred to as base class and the new class is referred to as derived class.
Types of Inheritance in C++:
There are 5 types of Inheritance in C++:
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multi-level Inheritance
- Hybrid Inheritance
Single Inheritance:
When a derived class (Child Class) inherits the properties and behaviour of a base class (Parent Class or Super-Class) then it is called Single Inheritance.
#include<iostream.h> #include<conio.h> using namespace std; class A { public: void display() { cout<<"Car is moving"; } }; class B: public A { public: void brand() { cout<<"Brand: BMW"; } }; int main() { B b1; b1.display(); b1.brand(); return 0; }
Output:
Car is moving
Brand: BMW
Multiple Inheritance:
In multiple inheritance, having only one subclass and many super-classes. A subclass inherits from more than one superclass.
#include<iostream.h> #include<conio.h> using namespace std; class Bike { public: Bike() { cout << "Bike is moving" << endl; } }; class Car { public: Car() { cout << "Car is moving" << endl; } }; class Scooty: public Bike, public Bike { }; int main() { Scooty sc; return 0; }
Hierarchical Inheritance
When many derived class (Child Class or Sub-Class) inherits the properties and behaviour of a base class (Parent Class or Super-Class) then it is called Hierarchical Inheritance.
#include<iostream.h> #include<conio.h> using namespace std; class Shape { public: int a; int b; void get_data(int x,int y) { a= x; b = y; } }; class Rectangle: public Shape public: int rect_area() { int result = a*b; return result; } }; class Triangle: public Shape { public: int triangle_area() { float result = 0.5*a*b; return result; } }; int main() { Rectangle rec; Triangle tri; int length, breadth, base, height; cout << "Enter the length and breadth of a rectangle: " <<endl; cin>>length>>breadth; rec.get_data(length, breadth); int x = rec.rect_area(); cout << "Area of the rectangle is : " <<x<<endl; cout << "Enter the base and height of the triangle: " <<endl; cin>>base>>height; tri.get_data(base, height); float y = tri.triangle_area(); cout <<"Area of the triangle is : " <<y<<endl; return 0; }
Multi-level Inheritance:
When a Class serves as a base class (Parent Class or Super-Class) for a derived class (Child Class or Sub-Class) which in turn serves as a base class for another derived class then it is called Multi-level Inheritance.
#include<iostream.h> #include<conio.h> using namespace std; class A { public: void display() { cout<<"Hello World!"; } }; class B: public A { }; class C: public B { }; int main() { C c1; c1.display(); return 0; }
Output:
Hello World!
Hybrid Inheritance:
Hybrid inheritance is a combination of single and multiple inheritance.
#include<iostream.h> #include<conio.h> using namespace std; class X { protected: int a; public: void get_valuea() { cout << "Enter the value of a: " <<endl; cin>>a; } }; class Y: public X { protected: int b; public: void get_valueb() { cout << "Enter the value of b: " <<endl; cin>>b; } }; class Z { protected: int c; public: void get_valuec() { cout << "Enter the value of c: " <<endl; cin>>c; } }; class W: public Y, public Z { protected: int d; public: void add() { get_valuea(); get_valueb(); get_valuec(); cout << "Addition of a,b,c is: " <<a+b+c<<endl; } }; int main() { W w1; w1.add(); return 0; }