Multilevel inheritance in C++ OOP – Example program
In this tutorial we will learn about the followings;
- Multilevel inheritance in C++ OOP
- Example of multi-level inheritance in C++ OOP
- The syntax of multi-level inheritance in C++ OOP
- The basic program of multi-level inheritance in C++ OOP
What is Multi-level inheritance?
A 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.
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
#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.
Difference between multi level and multiple Inheritance in C++
Here, i am showing you a comparison of multiple and multi level inheritance in C++.
Multilevel Inheritance | Multiple Inheritance |
In Multilevel Inheritance parent shares inherits with child and then child becomes parent of other class and share the resources of parent to its child. | In Multiple Inheritance a class inherits from more than one parent classes. |
Class Levels |
|
Multilevel Inheritance has three class levels namely, base class, intermediate class and derived class. | Multiple Inheritance has two class levels namely, base class and derived class. |
Usage |
|
Multilevel Inheritance is widely used as compared to multiple inheritance. | Multiple Inheritance is complex as compared to multi level inheritance and not widely used. |
Exercise and Solution Multi level inheritance
- C++ program to print a hollow square or rectangle star pattern by achieving the multi-level inheritance.
- Multilevel inheritance C++ program to display the pattern like a pyramid.
- Multilevel inheritance C++ program to show the sum of an A.P. series.
- Multilevel inheritance C++ program to display patterns like the right angle.
- Multi-level inheritances C++ program to display the cube of the number up to a given integer.
- Multilevel inheritance C++ Program to convert a decimal number into binary.
- C++ program to find HCF using Multilevel inheritance.
- Develop a program in C++ to display the pattern like a pyramid using an asterisk * and each row contains an odd number of asterisks by using the Multilevel inheritance.
- Write a C++ program to print the rhombus star pattern of N rows using Multi-Level Inheritance.
- Write a program in C++ to convert a decimal number to hexadecimal using the Multi-Level Inheritance in object-oriented programming (OOP).
- Write a program in C++ to convert a decimal number into an octal without using an array using Multi-Level in object-oriented programming(OOP).
- C++ program to display Pascal’s triangle using the Multi-Level inheritance.
- Multi-Level inheritance C++ program to print hollow rhombus, parallelogram star pattern.
- Multi-Level Inheritance C++ program to print mirrored right triangle star pattern.
- Multi-level inheritance C++ Program to convert an octal number into binary.
- MultiLevel inheritance Floyd Triangle C++.
- C++ Program to display the sum of the series using Multilevel inheritance.
- MultiLevel Inheritance C++ program to find Strong Numbers within a range of numbers.
- Multilevel inheritance C+ Program Armstrong number of n digits.
- Multilevel inheritance C++ program to convert a decimal number into binary.
- Multilevel inheritance C++ to display the n terms of odd natural number and their sum.
- Multilevel inheritance C++ program pattern like a pyramid repeat.
Topic Covered
Multilevel inheritance in C++ OOP with example and program.