Multiple inheritance in C++ with Syntax and Examples
Multiple inheritance in C++ OOP, Example and syntax of multiple inheritances
In this tutorial, we will learn about the followings;
- Multiple inheritances in C++ OOP
- Example Multiple inheritances in C++ OOP
- The syntax of multiple inheritance
What is Multiple inheritance in C++ OOP?
In multiple inheritance, the child class is derived from more than one parent class.
Syntax of multiple inheritance in C++ OOP
class child_class_name : Access_specifier 1st_parent, Access_specifier 2nd_parent{Body of the class;
};
#include<iostream>Example of multiple inheritance in C++ OOP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using namespace std; class sum1 { protected: int n1; }; class sum2 { protected: int n2; }; class show : public sum1, public sum2 { public: int total() { cout<<"enter n1?"<<endl; cin>>n1; cout<<"enter n2?"<<endl; cin>>n2; cout<<"sum="<<n1+n2<<endl; } }; int main() { show myobject; myobject.total(); } |
Output
enter n1?
3
enter n2?
4
sum=7
Ambiguity in Multiple Inheritance in C++ is the most common problem.
Difference between multiple and single Inheritance in C++
Here, i am showing you a comparison of multiple and single inheritance in C++.
MULTIPLE INHERITANCE | SINGLE INHERITANCE |
In Multiple inheritance derived class have more than one base class. | In Single inheritance derived class have only single base class. |
More overhead in Multiple inheritance and requires more run time as compared to single inheritance. | Less overhead in single inheritance and requires small run time as compared to multiple inheritance. |
Multiple inheritance is a lot of close to generalization. | Single inheritance is a lot of close to specialization. |
Syntax of multiple inheritance is
Class DerivedClass_name : access_specifier Base_Class1, access_specifier Base_Class2, ….{}; . |
Syntax of single inheritance is
Class DerivedClass_name : access_specifier Base_Class{};. |
Multiple inheritance is more complex as compared to the single inheritance. | Single inheritance is simple as compared to the multiple inheritance. |
Multiple inheritance is supported by C++ but Multiple inheritance can’t be implemented in most of other programming languages e.g, C# and Java doesn’t support multiple inheritance through classes. | Mostly all of the programming languages supports Single inheritance. |
Difference between multiple and multi level Inheritance in C++
Here, i am showing you a comparison of multiple and multi level inheritance in C++.
Multiple Inheritance | Multilevel Inheritance |
In Multiple Inheritance a class inherits from more than one parent classes. | 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. |
Class Levels |
|
Multiple Inheritance has two class levels namely, base class and derived class. | Multilevel Inheritance has three class levels namely, base class, intermediate class and derived class. |
Usage |
|
Multiple Inheritance is complex as compared to multi level inheritance and not widely used. | Multilevel Inheritance is widely used as compared to multiple inheritance. |
Exercise and Solution Multiple inheritance
- WAP in C++ to display such a pattern for n number of rows using a number which will start with the number 1 and the first and the last number of each row will be 1 with the help of multiple inheritance.
- C++ program to print a hollow square or rectangle star pattern with the support of Multiple inheritance.
- Multiple inheritance C++ program to display the pattern like a pyramid.
- Multiple inheritance C++ program to show the sum of an A.P. series.
- Multiple inheritance C++ program to print the string in reverse order.
- Multiple inheritance C+ Program Armstrong number of n digits.
- Multiple inheritances C++ program to display a pattern like the right angle.
- Multiple inheritances C++ program to display the cube of the number up to a given integer.
- Multiple inheritance C++ Program to convert a decimal number into binary.
- 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 multiple inheritances.
- Multiple inheritance C++ program to convert a decimal number into binary.
- Write a C++ program to print the rhombus star pattern of N rows using Multiple Inheritance.
- Write a program in C++ to convert a decimal number to hexadecimal using the Multiple inheritances in object-oriented programming (OOP).
- Write a program in C++ to convert a decimal number into octal without using an array using Multiple inheritances in object-oriented programming(OOP).
- C++ program to display Pascal’s triangle using the Multiple inheritances.
- Multiple inheritances C++ program to print hollow rhombus, parallelogram star pattern.
- Multiple inheritance C++ program pattern like a pyramid repeat.
- Multiple Inheritance C++ program to print mirrored right triangle star pattern.
- Multiple inheritance C++ Program to convert an octal number into binary.
- Multiple inheritances Floyd Triangle C++.
- C++ Program to display the sum of the series using Multiple inheritances.
- Multiple Inheritance C++ program to find Strong Numbers within a range of numbers.
- Multiple inheritances C++ to display the n terms of odd natural number and their sum.
Test Your Understandings |
1.class show : public sum1, public sum2, here sum1 and sum2 are child classes of class sum1? YES / NO
2.Multiple inheritance means one class having more than one parents? YES / NO
Topic Covered
Multiple inheritance in C++ OOP, Example and syntax of multiple inheritance.