Multi-level Inheritance in C++

In "Multi-level Inheritance" their at least three different classes.

LOGO


Basically in this type of inheritance a class is derived from a Parent class then an another class is derived from the previously derived class. So their are levels of inheritance occurred. To understand it better see the "Block-Diagram" below.

Block-Diagram
BLOCK-DIAGRAM

Syntax:-
class Parent1
{
//Body of the Parent1 class
};
class Parent2 : access_specifier Parent1
{
//Body of the Parent2 class
};
class Child : access_specifier Parent2
{
//Body of the Child class
};


Example of Multi-level Inheritance
#include<iostream>
using namespace std;

class Parent1
{
 protected:
  int a=10;
}; 
//Parent2 is derived from Parent1
class Parent2 : public Parent1
{
 protected:
  int b=8;
};
//Child is derived from Parent2
class Child : public Parent2
{
 public:
  int c=12;
                //Child class have all the properties of Parent1 & Parent2
  void put()
  {
   cout<<"Sum is "<<a+b+c;
  }
};


int main()

{
 Child x; //Object of Child class
 x.put();
}


Also visit ----

Single Inheritance

Multiple Inheritance



For giving some suggestion please comment us or check our "Contact us" page. Also more update related to coding will be added in this Blog later on, so please get attached with this Blog by clicking the follow button. Thank you for your support and time...

Post a Comment

0 Comments