Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Multi-level inheritance in C++ language

Posted on February 18, 2017January 21, 2020

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.

Multi-level inheritance in C++ language
Multilevel inheritance
Multi-level inheritance in C++ language
Syntax

Multi-level inheritance in C++ language
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

 

Multi-level inheritance in C++ language
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;
}

Single level inheritance in java         Single level inheritance in C++
 
Multi levelinheritance in Java            Inheritance in Java  
 
Hierarchical Inheritance in Java       Multiple Inheritance in C++
Hybrid inheritance in C++
 
Interface in Java           Inheritance in C++             

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes