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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.