Topic :Inheritance Types: Single | Multiple | Multilevel | Hierarchical | Hybrid



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.



image: single Inheritance: infobrother

The basic Syntax is:


class Base_Class
{
   // Class Body:
}
 
class Derived_Class: Access-Specifier Base_Class
{
   //Class Body:
}

  • In Above Syntax:

    » Access-Specifier Defines the Scope and Visibility of a Class.
    » Class is The Keyword to Define Class.
    » Base_Class is the name of Class whose members will be inherited by Derived Class.
    » Derived_Class is the the name of Class Which will Inherit the Members of Base_Class
    » : colon " : " is the Special Character to make a Relationship between the Base and Derived Class.



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: 
} 


  • In Above Syntax:

    » Access-Specifier Defines the Scope and Visibility of a Class.
    » Class is The Keyword to Define Class.
    » Base_Class is the name of Class whose members will be inherited by Derived Class.
    » Derived_Class is the the name of Class Which will Inherit the Members of Base_Class
    » : colon " : " is the Special Character to make a Relationship between the Base and Derived Class.
    » Class A is the Base class of Class C.
    » Class B is the Base Class of Class C.
    » The Derived Class "C" Inherit the Properties and Method of two Classes A, and B. using the comma Operator.



image: Multiple Inheritance: infobrother

/* 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;
}
 
 


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.



image: Multilevel Inheritance: infobrother

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.
{ . . . . . };

  • In Above Syntax:

    » Access-Specifier Defines the Scope and Visibility of a Class.
    » Class is The Keyword to Define Class.
    » Base_Class is the name of Class whose members will be inherited by Derived Class.
    » Derived_Class is the the name of Class Which will Inherit the Members of Base_Class
    » : colon " : " is the Special Character to make a Relationship between the Base and Derived Class.
    » The Class GrandFather is the Base Class for Class Father .
    » The Class Father is the Derived class from Class GrandFather and base class of Child.
    » The Class Child is the 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.



image: Hierarchical Inheritance: infobrother

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
{ . . . . . };

  • In Above Syntax:

    » Access-Specifier Defines the Scope and Visibility of a Class.
    » Class is The Keyword to Define Class.
    » Base_Class is the name of Class whose members will be inherited by Derived Class.
    » Derived_Class is the the name of Class Which will Inherit the Members of Base_Class
    » : colon " : " is the Special Character to make a Relationship between the Base and Derived Class.
    » MAN is the base class for all other class.
    » The Classes Employee , Father and Husband is the Derived Class of Man Class.



Hybrid Inheritance:

Combination of Multi-level and Hierarchical Inheritance will give us Hybrid Inheritance. this type of inheritance is also known as Virtual Inheritance.


image: Hybrid Inheritance: infobrother

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.

  • In Above Syntax:

    » Access-Specifier Defines the Scope and Visibility of a Class.
    » Class is The Keyword to Define Class.
    » : colon " : " is the Special Character to make a Relationship between the Base and Derived Class.
    » Cricketer Class is the base Class for Batsman and Bowler.
    » Batsman Class is the derived class of Cricketer and base class for All-rounder.
    » Bowler Class is the Derived class of Cricketer and base class for All-rounder.
    » All rounder class is the derived class of Batsman and Bowler.



  • Advantages of Inheritance:

    If we Develop Any Application using the concept of Inheritance, The Application will have the Following Advantages:
    » Development of Application will take less time.
    » Application will assume less Memory.
    » Execution time of Application will be less.
    » Enhance Application Performance.
    » Repetition of the code will reduce so that we get consistence results and less storage cost.










I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement