0% found this document useful (0 votes)
45 views5 pages

C++ Inheritance - 4

This document discusses C++ multilevel inheritance. Multilevel inheritance allows a class to inherit from another derived class, creating a hierarchy with multiple levels. For example, a Mammal class derived from Animal, with the Human class derived from Mammal. This allows the inheriting class to access properties of all parent classes. The document provides a code example demonstrating a base class with two levels of derived classes, each inheriting properties and methods from its parent classes.

Uploaded by

md shankar
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)
45 views5 pages

C++ Inheritance - 4

This document discusses C++ multilevel inheritance. Multilevel inheritance allows a class to inherit from another derived class, creating a hierarchy with multiple levels. For example, a Mammal class derived from Animal, with the Human class derived from Mammal. This allows the inheriting class to access properties of all parent classes. The document provides a code example demonstrating a base class with two levels of derived classes, each inheriting properties and methods from its parent classes.

Uploaded by

md shankar
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
You are on page 1/ 5

10/13/2020 C++ Multilevel Inheritance (With Examples) - Trytoprogram

C++ Tutorials

C++ Multilevel Inheritance

If a class is derived from another derived class then it is called multilevel inheritance. So in C++ multilevel
inheritance, a class has more than one parent class.

For example, if we take animals as a base class then mammals are the derived class which has features of
animals and then humans are the also derived class that is derived from sub-class mammals which inherit all
the features of mammals.

C++ Multilevel Inheritance Block Diagram


Here is the block diagram of C++ multilevel inheritance to make it clear.

www.trytoprogram.com/cplusplus-programming/multilevel-inheritance/ 1/5
10/13/2020 C++ Multilevel Inheritance (With Examples) - Trytoprogram

As shown in above block diagram, class C has class B and class A as parent classes. Depending on the relation
the level of inheritance can be extended to any level.

As in other inheritance, based on the visibility mode used or access speci er used while deriving, the
properties of the base class are derived. Access speci er can be private, protected or public.

Click here to learn in detail about access speci ers and their use in inheritance

C++ Programming Multilevel Inheritance Syntax

www.trytoprogram.com/cplusplus-programming/multilevel-inheritance/ 2/5
10/13/2020 C++ Multilevel Inheritance (With Examples) - Trytoprogram

class A // base class


{
...........
};
class B : acess_specifier A // derived class
{
...........
} ;
class C : access_specifier B // derived from derived class B
{
...........
} ;

C++ Multilevel Inheritance Example


Here is a simple example to illustrate the concept of c++ multilevel inheritance.

www.trytoprogram.com/cplusplus-programming/multilevel-inheritance/ 3/5
10/13/2020 C++ Multilevel Inheritance (With Examples) - Trytoprogram

// inheritance.cpp
#include <iostream>
using namespace std;
class base //single base class
{
public:
int x;
void getdata()
{
cout << "Enter value of x= "; cin >> x;
}
};
class derive1 : public base // derived class from base class
{
public:
int y;
void readdata()
{
cout << "\nEnter value of y= "; cin >> y;
}
};
class derive2 : public derive1 // derived from class derive1
{
private:
int z;
public:
void indata()
{
cout << "\nEnter value of z= "; cin >> z;
}
void product()
{
cout << "\nProduct= " << x * y * z;
}
};
int main()
{
derive2 a; //object of derived class
a.getdata();
a.readdata();
a.indata();

www.trytoprogram.com/cplusplus-programming/multilevel-inheritance/ 4/5
10/13/2020 C++ Multilevel Inheritance (With Examples) - Trytoprogram

a.product();
return 0;
} //end of program

Output

Enter value of x= 2

Enter value of y= 3

Enter value of z= 3

Product= 18

All rights reserved @trytoprogram.com

www.trytoprogram.com/cplusplus-programming/multilevel-inheritance/ 5/5

You might also like