0% found this document useful (0 votes)
7 views22 pages

Multiple Inheritance

The document discusses multiple inheritance in C++, including its features, constructors, and issues such as ambiguity and the diamond problem. It provides example code to illustrate these concepts and solutions, particularly the use of virtual base classes to resolve ambiguities. The lecture is presented by Sehrish Munawar Cheema and covers key aspects of object-oriented programming related to inheritance.

Uploaded by

Abdul Qayyum
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views22 pages

Multiple Inheritance

The document discusses multiple inheritance in C++, including its features, constructors, and issues such as ambiguity and the diamond problem. It provides example code to illustrate these concepts and solutions, particularly the use of virtual base classes to resolve ambiguities. The lecture is presented by Sehrish Munawar Cheema and covers key aspects of object-oriented programming related to inheritance.

Uploaded by

Abdul Qayyum
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Object Oriented Programming

Course Code:CC-1022

Lecture#
Lecturer: Sehrish Munawar Cheema
Email: [Link]@[Link]
Today’s Lecture
• Multiple inheritance
– Example program: employ and student class
• Constructor in multiple inheritance
– Example program
• Ambiguity in Multiple Inheritance
Multiple Inheritance
• Multiple Inheritance is a feature of C++
where a class can inherit from more than
one classes.
Constructors in Multiple
Inheritance
Cont..
• The constructors of inherited classes are
called in the same order in which they are
inherited.
class A
{
Implicit Call
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;
}
Explicit Call
class A
{
public:
A() { cout << "A's constructor called" << endl; }
};
class B
{
public:
B() {

A();
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; }
Ambiguity in Multiple
Inheritance
• Ambiguity is created if the names of
functions are similar in two or more parent
classes.
• Compiler cannot determine which function
to execute when object of derived class
attempts to execute such function.
class A
{
public:
void show() { cout << “Class A" << endl; }
};
class B
{
public:
void show() { cout << “Class A" << endl; } Compiler
};
class C: public B, public A // Note the order Error
{ };
int main()
{
C obj;
[Link]();
return 0; }
Removing Ambiguity
• For above program ambiguity can be
removed as follows:

obj.A::show();
obj.B::show();
• Another way is to overload both
functions in derived class.
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 code: Multiple


Inheritance
Diamond Problem
Elaboration
When you have a hybrid inheritance
(Multiple inheritance– more than one parent)
then a Diamond problem may arise. In this
problem a Derived class will have multiple
paths to a Base class. This will result in
duplicate inherited members of the Base
class. This kind of problem is known as
Diamond problem
Example-code
class parent
{ protected: int n; };
class child1: public c parent { };
class child2: public parent { };
class baby:public child1, public child2
{ public:
void set() { n=10;
cout<<“number=”<<n}
};
Cont..
void main()
{
baby obj;
[Link](); Output
Compile Error
}
Solution to diamond
Problem?
Solution Virtual Base class
Virtual Base Classes
• A class that is inherited by child class using virtual
keyword is called virtual base class.
• Virtual base class is necessary in a situation where the
member of base class is duplicated in multi-level
inheritance.
• Now recall diamond problem.
• Solution of diamond problem is virtual Base Classes. To
eliminate the ambiguity, we make Faculty and Student
into virtual base classes.

• Example Code: Diamond Pblm Solution


Example-code
class parent
{ protected: int n; };
class child1: virtual public parent { };
class child2: virtual public parent { };
class baby : public child1, public child2
{ public:
void set() { n=10;
cout<<“number=”<<n}
};
Cont..
void main()
{
baby obj;
[Link](); Output
n=10
}

You might also like