Multilevel Inheritance
In the Multilevel inheritance, a derived class will inherit a base class and as well as
the derived class also act as the base class to other class. For example, three classes
called A, B, and C, as shown in the below image, where class C is derived from class
B and class B, is derived from class A. In this situation, each derived class inherit all
the characteristics of its base classes. So class C inherits all the features of class A
and B.
In Short we can say that A class can also be derived
from one class, which is already derived from another
class.
Syntax to create a Multiple Inheritence in C++
class A // base class
{
...........
};
class B : acess_specifier A // derived class
{
...........
};
class C : access_specifier B // derived from derived class B
{
...........
};
A program that
demonstrates
MultiLevel
Inheritance
// Base class (parent) int main() {
class MyClass { MyGrandChild myObj;
public: [Link]();
void myFunction() { return 0;
cout << "Some content in parent class." ; }
}
};
Output:
// Derived class (child)
class MyChild: public MyClass { Some content in parent class.
};
// Derived class (grandchild)
class MyGrandChild: public MyChild {
};
// Multilevel [Link]
#include <iostream> class derive2 : public derive1 // derived from class derive1
using namespace std; {
class base //single base class private:
{ int z;
public: public:
int x; void indata()
void getdata() {
{ cout << "\nEnter value of z= "; cin >> z;
cout << "Enter value of x= "; cin >> x; }
} void product()
}; {
class derive1 : public base // derived class from base cout << "\nProduct= " << x * y * z;
class }
{ };
public:
int y;
void readdata()
{
cout << "\nEnter value of y= "; cin >> y;
}
};
int main()
{
derive2 a; //object of derived
class
[Link]();
[Link]();
[Link]();
[Link]();
return 0;
} //end of program