Multi level inheritance in C++ OOP, Example of multi level inheritance, Syntax of multi level inheritance, Basic program of multi level inheritance

Multi level inheritance in C++ OOP, Example of multi level inheritance, Syntax of multi level inheritance, Basic program of multi level inheritance

In this tutorial we will learn about the followings;

  1. Multilevel inheritance in C++ OOP
  2.  Example of multi-level inheritance in C++ OOP
  3. The syntax of multi-level inheritance in C++ OOP
  4. The basic program of multi-level inheritance  in C++ OOP

What is Multi-level inheritance?

Derived class can be derived from another derived class. A child class can be the parent of another class.

Give an example of multilevel inheritance?

Class C is a child of class B and class B is child class of class A.

multivel child parent relationship of classes
Figure: multi-level inheritance.

What is the syntax of multilevel inheritance?

Class A

{

Statements of Class A

};

Class B: public class A

{

Statements of Class  b

};

Class C: public class B

{

Statements of Class  C

};

Basic Program of multi-level inheritance

Line#  Code
 1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

#include<iostream>

using namespace std;

class sum

{

protected:

int n1;

 

};
class child1 : public sum

{

protected:

int n2;

};

class child2 : public child1

{

public:int sum()

{

cout<<“enter n1″<<endl;

cin>>n1;

cout<<“enter n2″<<endl;

cin>>n2;

cout<<“sum=”<<n1+n2<<endl;

}

};

int main()

{

child2 myobject;

myobject.sum();

}

Here in this example there is multi level inheritance because

  • Declaration of variable n1 is inherited from sum to child1 
  • Declaration of Variable n2 is inherited from chil1 to child2 
  • Child2 now uses one variable of its first parent and one variable of its grandfather.

So, this is a better example of multilevel inheritance.