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

Table of Contents

  • Multi-level inheritance in C++ language
    • Related

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

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes