Categories: C++Learn C++

Multi-level inheritance in C++ language

Multi-level inheritance in C++ language

In this tutorial, we will discuss an Oop concept of the multi-level inheritance in C++ language.

Already, we’ve looked at multilevel inheritance in inheritance in C++. Multilevel inheritance is a type of concept in C++ inheritance. A base class is inherited by an intermediate class and derived class is inherited by a child class.

In the diagram below, class C inherits class B and class B inherits class A which means class C is the child of class B and class B is the child to class A.

Multilevel inheritance
Syntax

Inheritance
At the above diagram, we can clearly understand multilevel inheritance.We have three classes: principal , teacher and student. Principal is the base class, the teacher is the intermediate class and the student is the derived class. Class teacher extend the class principal and the class student extends the class teacher. Now, we can access data member and methods of other two (parent and intermediate)classes from student(child) class.

Program 1

#include <iostream>
#include <conio.h>
using namespace std;
class Base{
public:
    void display1(){
    cout<<"I am base class"<<endl;
    }

};
class Intermediate: public Base{
public:
    void display2(){
    cout<<"I am an intermediate class"<<endl;
    }

};

class derived: public Intermediate{
public:
    void display3(){
    cout<<"I am derived class"<<endl;
    }

};
int main()
{
    derived obj;
    obj.display1();
    obj.display2();
    obj.display3();
    getch();
    return 0;
}

The above code is executed, it produces the following result

I am base class
I am an intermediate class
I am derived class

Program 2

 

Example

 

Explanation of above program

#include <iostream>
using namespace std;
class A  //class A
{           // start of class A
public:  //public section in class A
int x,y;  //assign two variables x,y
void values()
{
x=12;  //assign value for x
y=18;  //assign value for y
}
};   // end of class A
class B:public A  //inherit properties B from A
{   // start of class B
public:  //public section in class B
void add(){  //function add
cout << “The result of x+y is ” << x+y<<endl;
// display statement output of x+y
}
};  // end of class B
class C:public B //inherit properties C from B
{
public:  //public section in class C
void sub() //function sub
{
cout << “The result of x-y is ” << x-y<<endl;
// display statement output of x-y
}
};
class D:public C//inherit properties D from C
{// start of class C
public:  //public section in class D
void mul() //function mul
{
cout << “The result of x*y is ” << x*y<<endl;
 // display statement output of x*y
}
};  // end of class C
int main() // main section
{
D obj;  //object for class D(child classs)
obj.values();  //called function value through object
obj.add(); //called function add through object
obj.sub(); //called function sub through object
obj.mul();//called function mul through object
return 0;
}

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.