Type of Inheritance:
Based on number of ways inheriting the features of base class into derived class, it have five types they are:
Single Inheritance:
In Single Inheritance there exists single base class and single derived class. We already discussed the example of single inheritance above: This type of inheritance enables a derived class to inherit properties and behavior from a single parent class. it allows a derived class to inherit the properties and behavior of base class, thus enabling code reuse-ability as well as adding new features to the existing code. This makes the code much more elegant and less repetitive.
The basic Syntax is:
class Base_Class
{
// Class Body:
}
class Derived_Class: Access-Specifier Base_Class
{
//Class Body:
}
Multiple Inheritance:
Multiple Inheritance represent a kind of inheritance when a derived class inherits properties of Multiple classes. For Example, there are three classes A, B, and C and derived Class is D as shown below, and here is the Extended Syntax:
class A
{
// Base Class Body:
}
class B
{
// Base Class Body:
}
class C : Access-Specifier A , Access-Specifier B
{
// Derived Class Body:
}
/* Multiple Inheritance Example - InfoBrother*/
#include <iostream>
using namespace std;
class Shape // Base class
{
protected:
int width;
int height;
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
};
class PaintCost // Base class.
{
public:
int getCost(int area)
{
return area * 11;
}
};
//Derived-class "Rectangle" Inherit Multiple classes.
class Rectangle: public Shape, public PaintCost
{
public:
int getArea()
{
return (width * height);
}
};
main()
{
Rectangle Rect; //Object Created of type Rectangle.
int area;
Rect.setWidth(12);
Rect.setHeight(16);
area = Rect.getArea();
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
// Print the total cost of painting
cout << "Total paint cost: $" << Rect.getCost(area) << endl;
return 0;
}
Multiple Inheritance
Total area: 192
Total paint cost: $2112
Multilevel Inheritance:
Multiple Inheritance represents a type of inheritance when a Derived class is a base class for another class. In Other words, deriving a class from a derived class is known as multi-level Inheritance. Simple multi-level Inheritance is shown in below image where Class Grandfather is a parent of Class Father and Class Father is a parent of Class Child.
As we can see, FATHER Inherited the properties of Grandfather, and child inherited the properties of Father..
The Syntax for Multilevel Inheritance is:
class GrandFather // Base Class
{ . . . . . };
class Father : Access-Specifier GrandFather // Derived Class from GranderFather Class.
{ . . . . . }; //Base class of Child Class.
class Child :Access-Specifier Father // Derived Class from Father Class.
{ . . . . . };
Hierarchical Inheritance:
When there is a Need to create Multiple Derived Classes That Inherit Properties of the same Base Class is known as Hierarchical Inheritance. For Example: Civil, Computer, Mechanical, Electrical are derived from Engineer. Natural language, Programming Language are derived from language.
The Syntax for Hierarchical Inheritance is:
class MAN // Base Class
{ . . . . . };
class Employee : Access-Specifier MAN // Derived Class from MAN
{ . . . . . };
class Father : Access-Specifier MAN // Derived Class from MAN
{ . . . . . };
class Husband : Access-Specifier MAN // Derived Class from MAN
{ . . . . . };
Hybrid Inheritance:
Combination of Multi-level and Hierarchical Inheritance will give us Hybrid Inheritance. this type of inheritance is also known as Virtual Inheritance.
The Syntax for Hybrid Inheritance is:
class cricketer
{ . . . . } //Base Class for Batsman and Bowler.
class Batsman : Access-Specifier cricketer
{ . . . . } //Derived Class of cricketer and base class for All-rounder.
class Bowler : Access-Specifier cricketer
{ . . . . } //Derived Class of cricketer and base class for All-rounder.
class All-rounder : Access-Specifier Batsman , Access-Specifier Bowler
{ . . . . } //Derived Class of Batsman and Bowler.