0% found this document useful (0 votes)
3 views7 pages

Understanding Multilevel Inheritance in C++

Multilevel inheritance allows a derived class to inherit from a base class, while also serving as a base class for another derived class. In this structure, each derived class inherits characteristics from its base classes, exemplified by classes A, B, and C. The document includes C++ syntax and a program demonstrating multilevel inheritance with classes MyClass, MyChild, and MyGrandChild.

Uploaded by

iqrak6321
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Understanding Multilevel Inheritance in C++

Multilevel inheritance allows a derived class to inherit from a base class, while also serving as a base class for another derived class. In this structure, each derived class inherits characteristics from its base classes, exemplified by classes A, B, and C. The document includes C++ syntax and a program demonstrating multilevel inheritance with classes MyClass, MyChild, and MyGrandChild.

Uploaded by

iqrak6321
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like