0% found this document useful (0 votes)
12 views

Lecture-3 (Module 3)

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lecture-3 (Module 3)

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Module:3

Multiple Inheritance in C++


Multiple Inheritance
Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The constructors of
inherited classes are called in the same order in which they are inherited. For example, in the following program,
B’s constructor is called before A’s constructor.
A class can be derived from more than one base class.
Eg:
(i) A CHILD class is derived from FATHER and MOTHER class
(ii) A PETROL class is derived from LIQUID and FUEL class.
Syntax:

class A

... .. ...

};

class B

... .. ...

};

class C: public A,public B

... ... ...

};
#include<iostream>

using namespace std;

class A

public:

A() { cout << "A's constructor called" << endl; }

};

class B

public:

B() { cout << "B's constructor called" << endl; }

};
class C: public B, public A // Note the order

public:

C() { cout << "C's constructor called" << endl; }

};

int main()

C c;

return 0;

}
Output:
B's constructor called

A's constructor called

C's constructor called

The destructors are called in reverse order of constructors.


The diamond problem The diamond problem occurs when two superclasses of a class have a common base class. For
example, in the following diagram, the TA class gets two copies of all attributes of Person class, this causes ambiguities.
Example:
#include<iostream>

using namespace std;

class Person {

// Data members of person

public:

Person(int x) { cout << "Person::Person(int ) called" << endl; }

};
class Faculty : public Person {

// data members of Faculty

public:

Faculty(int x):Person(x) {

cout<<"Faculty::Faculty(int ) called"<< endl;

};
class Student : public Person {

// data members of Student

public:

Student(int x):Person(x) {

cout<<"Student::Student(int ) called"<< endl;

};
class TA : public Faculty, public Student {

public:

TA(int x):Student(x), Faculty(x) {

cout<<"TA::TA(int ) called"<< endl;

};

int main() {

TA ta1(30);

}
Person::Person(int ) called

Faculty::Faculty(int ) called

Person::Person(int ) called

Student::Student(int ) called

TA::TA(int ) called

In the above program, constructor of ‘Person’ is called two times. Destructor of ‘Person’ will also be called two
times when object ‘ta1’ is destructed. So object ‘ta1’ has two copies of all members of ‘Person’, this causes
ambiguities. The solution to this problem is ‘virtual’ keyword. We make the classes ‘Faculty’ and ‘Student’ as virtual
base classes to avoid two copies of ‘Person’ in ‘TA’ class.
For example, consider the following program.

#include<iostream>

using namespace std;

class Person {

public:

Person(int x) { cout << "Person::Person(int ) called" << endl; }

Person() { cout << "Person::Person() called" << endl; }

};

class Faculty : virtual public Person {

public:

Faculty(int x):Person(x) {

cout<<"Faculty::Faculty(int ) called"<< endl;

}
class Student : virtual public Person {

public:

Student(int x):Person(x) {

cout<<"Student::Student(int ) called"<< endl;

};

class TA : public Faculty, public Student {

public:

TA(int x):Student(x), Faculty(x) {

cout<<"TA::TA(int ) called"<< endl;

};
int main() {

TA ta1(30);

Output:
Person::Person() called

Faculty::Faculty(int ) called

Student::Student(int ) called

TA::TA(int ) called
In the above program, constructor of ‘Person’ is called once. One important thing to note in the above output is,
the default constructor of ‘Person’ is called. When we use ‘virtual’ keyword, the default constructor of grandparent
class is called by default even if the parent classes explicitly call parameterized constructor. How to call the
parameterized constructor of the ‘Person’ class? The constructor has to be called in ‘TA’ class. For example,
see the following program.
#include<iostream>

using namespace std;

class Person {

public:

Person(int x) { cout << "Person::Person(int ) called" << endl; }

Person() { cout << "Person::Person() called" << endl; }

};

class Faculty : virtual public Person {

public:

Faculty(int x):Person(x) {

cout<<"Faculty::Faculty(int ) called"<< endl;

};
class Student : virtual public Person {

public:

Student(int x):Person(x) {

cout<<"Student::Student(int ) called"<< endl;

};

class TA : public Faculty, public Student {

public:

TA(int x):Student(x), Faculty(x), Person(x) {

cout<<"TA::TA(int ) called"<< endl;

};

int main() {

TA ta1(30);

}
Output:
Person::Person(int ) called

Faculty::Faculty(int ) called

Student::Student(int ) called

TA::TA(int ) called
Virtual base class in C++
Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a given class
appearing in an inheritance hierarchy when using multiple inheritances.
Need for Virtual Base Classes:
Consider the situation where we have one class A .This class is A is inherited by two other classes B and C. Both
these class are inherited into another in a new class D as shown in figure below.
As we can see from the figure that data members/function of class A are inherited twice to class D. One through
class B and second through class C. When any data / function member of class A is accessed by an object of
class D, ambiguity arises as to which data/function member would be called? One inherited through B or the other
inherited through C. This confuses compiler and it displays error.
#include <iostream>

using namespace std;

class A {

public:

void show()

cout << "Hello form A \n";

};

class B : public A {

};
class C : public A {

};

class D : public B, public C {

};

int main()

D object;

object.show();

}
Compile Errors:
prog.cpp: In function 'int main()':

prog.cpp:29:9: error: request for member 'show' is ambiguous

object.show();

prog.cpp:8:8: note: candidates are: void A::show()

void show()

prog.cpp:8:8: note: void A::show()


How to resolve this issue?

To resolve this ambiguity when class A is inherited in both class B and class C, it is declared as virtual base
class by placing a keyword virtual as :
Syntax for Virtual Base Classes:
Syntax 1:

class B : virtual public A

};

Syntax 2:

class C : public virtual A

};
Note: virtual can be written before or after the public. Now only one copy of data/function member will be copied
to class C and class B and class A becomes the virtual base class.

Virtual base classes offer a way to save space and avoid ambiguities in class hierarchies that use multiple
inheritances. When a base class is specified as a virtual base, it can act as an indirect base more than once
without duplication of its data members. A single copy of its data members is shared by all the base classes that
use virtual base.
#include <iostream>

using namespace std;

class A {

public:

int a;

A() // constructor

a = 10;

};
class B : public virtual A {

};

class C : public virtual A {

};

class D : public B, public C {

};

int main()

D object; // object creation of class d

cout << "a = " << object.a << endl;

return 0;

}
Output:

a = 10
Explanation :The class A has just one data member a which is public. This class is virtually inherited in class B
and class C. Now class B and class C becomes virtual base class and no duplication of data member a is done.
#include <iostream>

using namespace std;

class A {

public:

void show()

cout << "Hello from A \n";

};
class B : public virtual A {

};

class C : public virtual A {

};

class D : public B, public C {

};

int main()

D object;

object.show();

}
Output:

Hello from A

You might also like