C++ Multiple,multi level,hierarchical inheritance

 The most important concept in object-oriented programming is inheritance. Generally, Inheritance means something(characteristic or anything else) passed down from the parent to the children.


In the same way in the c++ language, derive the class from the other class(base class) is known as inheritance. The base class is known as parent class and the drive class is known as child class.

There are various models of the inheritance 

Types of inheritance:

we are discussing models of inheritance in cpp programming.

Multilevel inheritance 

Multiple inheritances

Hierarchical inheritance


Multilevel inheritance :


In this c++ language, derived class from the base but again derived from the derived class.This form of inheritance known as multilevel inheritance.

class A
{
...
..
};
class B:public A
{
...
..
};
class C: public B
{
...
..
};

Example of multilevel inheritance:


#include <iostream>
using namespace std;

class classA //create base 
{
    public:
      void display()
      {
          cout<<" Base class here";
      }
};

class classB : public classA //derived of the base class A
{
 void displayb()
      {
          cout<<" derived class b here";
      }
};

class classC : public classB //derived  of the base class B
{
  void displayc()
      {
          cout<<" derived class c here";
      }
};

int main()
{
    classC obj; //create object 
    obj.display(); //display function of class A
    
    return 0;
}


Multiple inheritances:


In the c++ programming language, the derived has more than two-parent classes known as multiple inheritances. One class derived from two-parent classes.

For example, cricket derived from the class of physical activity and class of sports.


Graphic representation.




                


Example of multiple inheritance:


#include <iostream>
using namespace std;

class physicalactivity { //create the base class
  public:
    physicalactivity()
    {
      cout << "physicalactivitys make body moveable ." << endl;
    }
};

class sports { //create the derived 
  public:
    sports()
    {
      cout << "sports make you healthy" << endl;
    }
};

class cricket: public physicalactivity, public sports { //create derived of cricket from physical activity and sports class

};

int main()
{
    cricket b1; //create object of the cricket 
    return 0;

}

In this example, one is a sport and another class is physical activity. The cricket class has the properties which are found in both classes. Cricket physical activity as well as sport.
For writing base class place a comma and then access specifiers with the class name.


Hierarchical inheritance:

In this model of inheritance, more than one class derived from the base .It is known as inheritance. All the common properties of the derived classes included in the base.

For an example of hierarchical inheritance:

class person
{
...
..
};
class lawyer :public person
{
...
..
}; 

class judge:public person
{
...
..
};

The class of lawyer is the first person then profession lawyer.In the same way,The judge is also a person.So, both derived classes have common properties in the base.
#include <iostream>
using namespace std;

class character                                //create a class of character
{
     public:                                            //public data members
        string profession;
        int age;

        character() {                                                            //constructor
profession=" nothing ";
age=20;
}
        void display()                                                            //public function
        {
             cout << "My profession is: " << profession << endl;
             cout << "My age is: " << age << endl;
             cout << "I can eat." << endl; 
         cout << "I can sleep." << endl;
        }
        
};


class gunman : public character                                              //derived
{
    public:
       void gun() { cout << "I have ability to fight with gun " << endl; }
};


class fightman : public character                                        //another derived
{
    public:
       void fight() { cout << "I have power to fight" << endl; }
};

class swordman : public character                                        //another derived
{
    public:
       void sword() { cout << "I ability to fight with sword ." << endl; }
};

int main()
{
     gunman man;                                                //create an object of the gunman
     man.profession = "gunman";
     man.age = 26;
     man.display();
     man.gun();

 

     return 0;
}

Output:

My profession is: gunman My age is: 26 I can eat. I can sleep. I have ability to fight with gun


Please write the comment,if you find anything incorrect and any problem in the above topic or you want to share more information about the above topic.

Comments

Popular posts from this blog

Diferrence between flutter and react native

Flutter Widgets

Flutter layout