Multilevel Inheritance

In this type of inheritance the derived class inherits from a class, which in turn inherits from some other class. The Super class for one, is sub class for the other.

Multilevel Inheritance
Multilevel Inheritance

Example:

Class A
     // Base class for Class B
{
       //body
}
Class B : public A
     // Base class for class C 
{
     //body
}
Class C : public B
    // final derived class
{
    //body
}

In multilevel inheritance, level of inheritance is not fixed to only two or three levels. It is depends on the level of programming to inherit data from the base class.

Here, class B is intermediate class. B is base class for class C and derived class for class A.

#include<iostream.h>
#include<conio.h>
class worker // Base class declaration
{
	int age;
	char name[20] ;
	public;
	void get()
	{
		cout << “your name please” ;
		cin >> name;
		cout << “your age please” ;
	}
	void show()
	{
		cout << “In my name is : “ <<name<< “ In my age is : “ <<age;
	}
};

class manager : public worker //Intermediate base class derived
{ 								//publicly from the base class
	int now;
	public:
	void get() 
	{
		worker ::get() ; //calling get ( ) fn. of base class
		cout << “no. of workers under you:”;
		cin >> now;
	}
	void show( )
	{
		worker : : show() ; //calling show ( ) fn. of base class
		cout << “In no. of workers under me are: “<< now;
	}
};
class ceo : public manager 	//declaration of derived class
{ 						  	//publicly inherited from the
	int nom; 				//intermediate base class
	public:
	void get()
	{
		manager :: get() ;
		cout << “no. of managers under you are:”; cin >> nom;
	} 
	void show()
	{
		cout << “In the no. of managers under me are: In”;
		cout << “nom;
	}
};
void main()
{
	clrscr() ;
	ceo cl ;
	cl.get() ; 
	cout << “\n\n”;
	cl.show() ;
}

Related posts