Multiple Inheritance and Polymorphism: Session 6
Multiple Inheritance and Polymorphism: Session 6
Polymorphism
Session 6
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
Session Objectives(Contd.)
Describe Virtual functions
Describe Polymorphism
Describe Dynamic binding
Describe Pure Virtual Functions
Describe Abstract classes
Describe Virtual destructors
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.
Multiple Inheritance (Contd.)
Base Teacher Student Base
class class
Teaching
assistant Derived class
Multiple Inheritance (Contd.)
Teacher Student
Teaching
assistant
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.
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.
Virtual Base Class
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;}
};
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.
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.
Virtual Base Classes (Contd.)
Virtual base
class is
represented by a
single object of
the class.
Note the
difference with
normal multiple
inheritance.
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.
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();
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.
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";}
};
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();
Virtual functions(Contd.)
The address of the derived class object is
assigned to a pointer of the base class:
ptr = &c; //c is an object of class circle