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

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

In this tutorial, we will learn about the followings;

  1. Multiple inheritance  in C++ OOP
  2. Example Multiple inheritance  in C++ OOP 
  3. The syntax of multiple inheritance

What is Multiple inheritance in C++ OOP?

In multiple inheritance, child class is derived from more than one parent class.

Syntax of multiple inheritance in C++ OOP

Serial# Code
1 class child_class_name :Access_specifier 1st_parent, 

                                     Access_specifier 2nd_parent

2 {
3 Body 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:

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

Answer - Click Here:
.

Fazal Rehman Shamil
Welcome to all friends. The reason for our success is only your love for T4Tutorials. Our team is always available to answer your queries regarding any kind of confusions or discussion regarding your study and career matters. For discussion with us please join our facebook group "T4Tutorials.com". The link of the group is mentioned below. Thanks and love to all for connecting with us. We are nothing without you. Love you all.....
https://web.facebook.com/groups/2066136233601097/

Leave a Reply