Object Oriented Programming with C++/ Session 6 / 1 of 44
Multiple Inheritance and
Polymorphism
Session 6
Object Oriented Programming with C++ / Session
6 / 2 of 44
Session Objectives
■
Describe Multiple Inheritance
•
Constructors under Multiple Inheritance
•
Ambiguity in Multiple Inheritance
•
Multiple Inheritance with a Common Base
■
Describe Virtual Base Classes
•
Constructors and Destructors
■
Use Pointers to Objects to access Member
Functions
Object Oriented Programming with C++ / Session
6 / 3 of 44
Session Objectives(Contd.)
■
Describe Virtual functions
■
Describe Polymorphism
■
Describe Dynamic binding
■
Describe Pure Virtual Functions
■
Describe Abstract classes
■
Describe Virtual destructors
Object Oriented Programming with C++ / Session
6 / 4 of 44
Multiple Inheritance
■
Multiple inheritance is the process of creating a
new class from more than one base class.
•
The derived class inherits the
properties of two or more base
classes.
■
Multiple inheritance can combine the behaviour
of many base classes in a single class.
■
A multiple inheritance hierarchy represents a
combination of its base classes.
Object Oriented Programming with C++ / Session
6 / 5 of 44
Multiple Inheritance (Contd.)
StudentTeacher
Teaching
assistant
Base
class
Base
class
Derived class
Object Oriented Programming with C++ / Session
6 / 6 of 44
Multiple Inheritance (Contd.)
■
The syntax for multiple inheritance is similar to
that for single inheritance.
class Teacher
{ };
class Student
{ };
class Teach_asst: public Teacher, public Student
•
The names of both the base classes are
provided separated by a comma.
•
The rules of inheritance and access
for multiple inheritance are the same
as for single inheritance.
Object Oriented Programming with C++ / Session
6 / 7 of 44
Constructors
class Teacher{
private:
int x;
public:
Teacher(){x =0;} //constructors
Teacher(int s){x = s;}
};
class Student{
private:
int y;
public:
Student(){y = 0;} //constructor
Student(int a){y = a;}
};
Object Oriented Programming with C++ / Session
6 / 8 of 44
Constructors (Contd.)
class Teach_asst: public Teacher,public Student
{
private:
int z;
public:
Teach_asst():Teacher(),Student()
//constructor
{z = 0;}
Teach_asst(int s,int a,int b):
Teacher(s),Student(a)
{z = b;}
};
Object Oriented Programming with C++ / Session
6 / 9 of 44
Constructors (Contd.)
■
Constructors have to be defined to initialise the data
members of all classes.
■
The names of the base class constructor follow the
colon and are separated by commas:
Teach_asst():Teacher(),Student();
■
If the constructors had arguments then:
Teach_asst(int s, arg for Teacher class
int a, arg for Student class
int b): arg for this class
Teacher(s), call Teacher constructor
Student(a) call Student constructor
{z = b;} set own data member
Object Oriented Programming with C++ / Session
6 / 10 of 44
Constructors and Destructors
■
General order for calling Constructors:
•
Base classes as they appear in the list of
base classes: Teacher, Student
•
If there are member objects in the class,
they are initialised next, in the order they
appear in the derived class declaration.
•
The object itself (using the code in its
constructor).
■
General order for calling Destructors:
•
The destructor of the class is called first,
then those of member objects, and then the
base classes.
Object Oriented Programming with C++ / Session
6 / 11 of 44
Ambiguity in Multiple Inheritance
■
Compiler will not be able to understand which function
to use if two base classes have the same function or
data member name.
class Alpha{
public:
void display();
};
class Beta{
public:
void display()
};
class Gamma: public Alpha,public Beta
{ };
Object Oriented Programming with C++ / Session
6 / 12 of 44
Ambiguity (Contd.)
void main()
{
Gamma obj;
obj.display(); //ambiguous: cannot be compiled
}
■
To access the correct function or data
member you will need to use the scope
resolution operator.
obj.Alpha::display();
obj.Beta::display();
Object Oriented Programming with C++ / Session
6 / 13 of 44
Ambiguity (Contd.)
■
It is upto the programmer to avoid such
conflicts and ambiguities.
■
Can be resolved by defining a new function
display()in the derived class:
void Gamma::display() {
Alpha::display();
Beta::display();
}
•
The compiler is able to detect the name
clashes and resolves it.
Object Oriented Programming with C++ / Session
6 / 14 of 44
Multiple Inheritance: Common Base
■
There are many combinations which inheritance
can be put to.
■
There is the possibility of having a class as a base
twice.
StudentTeacher
Teaching
assistant
Person Person
Object Oriented Programming with C++ / Session
6 / 15 of 44
Common Base
■
Both Teacher and Student contain a copy of the
Person class members.
• When Teaching assistant is derived from
both Teacher and Student it contains two
copies of the Person class members - one
from Teacher and one from Student.
•
This gives rise to ambiguity between the
base class data members.
■
Another problem is that declaring an object of class
Teaching assistant will invoke the Person class
constructor twice.
Object Oriented Programming with C++ / Session
6 / 16 of 44
Virtual Base Classes
■
Multiple inheritance hierarchies can be complex,
and may lead to a situation in which a derived
class inherits multiple times from the same
indirect class.
Object Oriented Programming with C++ / Session
6 / 17 of 44
Example
class window{
protected:
int basedata;
};
class border: public window
{ };
class menu: public window
{ };
class border_and_menu: public border, public menu
{
public:
int show()
{return basedata;}
};
Object Oriented Programming with C++ / Session
6 / 18 of 44
Virtual Base Classes (Contd.)
■
When the classes menu and border are derived
from window, each inherits a copy of window.
This copy is called a subobject.
■
Each subobject contains its own copy of
basedata from the class window.
■
When class border_and_menu refers to
basedata the compiler does not know which
copy is being accessed and hence the error
occurs.
Object Oriented Programming with C++ / Session
6 / 19 of 44
Virtual Base Classes (Contd.)
■
To avoid two copies of the base class we use a virtual base
class.
■
To make a base class virtual include the keyword virtual
when listing the base class in the derived class
declaration.
class border: virtual public window
{ };
and
class menu: virtual public window
{ };
•
Useful to avoid unnecessary repetitions of
the same data member in a multiple
inheritance hierarchy.
Object Oriented Programming with C++ / Session
6 / 20 of 44
Virtual Base Classes (Contd.)
■
Virtual base class
is represented by
a single object of
the class.
■
Note the
difference with
normal multiple
inheritance.
Object Oriented Programming with C++ / Session
6 / 21 of 44
Constructors and Destructors
■
Presence of virtual base classes changes
the order in which constructors are called.
•
A virtual base class is initialised before
any non-virtual base class.
•
If there is more than one virtual base
class, the order of initialisation is
determined by their position in the
inheritance graph from top to bottom and
left to right.
■
The call for the destructor follows the same
rules, but in the reverse order.
Object Oriented Programming with C++ / Session
6 / 22 of 44
Pointers to Objects
■
Pointers can point to objects as well as to simple data
types.
Base* ptr;
ptr = new Base;
■
To refer to member functions in the object pointed to
by ptr we cannot use a dot operator.
•
The dot operator requires the identifier
on its left to be a variable.
■
Since ptr is a pointer the arrow operator (->) is used
to access a member function,
ptr->show();
Object Oriented Programming with C++ / Session
6 / 23 of 44
Virtual functions
■
A virtual function is a function that does not
really exist but does affect some parts of a
program.
■
Consider a program to draw different
shapes like triangles, circles, squares,
ellipses etc. Consider that each of these
classes has a member function draw() by
which the object is drawn.
Object Oriented Programming with C++ / Session
6 / 24 of 44
Example
class Shapes{
public:
void draw() //function in the base class
{cout<<"Draw Base\n";}
};
class circle: public Shapes{
private:
int radius;
public:
void draw() //redefined in derived class
{cout<<"Draw circle";}
};
Object Oriented Programming with C++ / Session
6 / 25 of 44
Example(Contd.)
class square: public Shapes{
private: int length;
public:
void draw() //redefined in derived class
{cout<<"Draw square";}
};
void main(){
circle c;
square s;
Shapes* ptr;
ptr = &c;
ptr->draw();
ptr = &s;
ptr->draw();
}