Multiple inheritance in C++ OOP, Example and syntax of multiple inheritances

Multiple inheritance in C++ OOP, Example and syntax of multiple inheritances

In this tutorial, we will learn about the followings;

  1. Multiple inheritances in C++ OOP
  2. Example Multiple inheritances in C++ OOP 
  3. 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

Serial#Code
1class child_class_name :Access_specifier 1st_parent, 

                                     Access_specifier 2nd_parent

2{
3Body of the class;
4};

Example of multiple inheritance in C++ OOP

Serial#Code
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

#include<iostream>

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();

}

Line 11:

Line 11 shows multiple inheritance where child class” show” have two parents “sum1” and “sum2”.

Child class “Show” behaviour is as follows;

  • show inherits variable n1 from  sum1
  • Show inherits variable n2 from sum2
Test Your Understandings

1.class show : public sum1, public sum2, here sum1 and sum2 are child classes of class sum1? YES / NO

Answer - Click Here:
NO

2.Multiple inheritance means one class having more than one parents? YES / NO

Answer - Click Here:
No

Topic Covered

Multiple inheritance  in C++ OOP, Example and syntax of multiple inheritance.

Fazal Rehman Shamil Click Here to Know More
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325
Dear Professors and Resarchers!You are welome to Cite these tutorials in your research or slides etc. Please don't forget to mention the reference of website. Copy Paste of text is strcitly forbidden. Images can be reuse because images are protected with watermark.

Leave a Reply

Your email address will not be published. Required fields are marked *