CS304 Part 2
CS304 Part 2
Example – Extention
Window
width
height
open
close
draw
1- Invoke Window’s
DialogBox draw
2- draw the dialog
controls
box
enable
draw
Example – Restriction
IntegerSet
…
add( elem ) Add element to
… the set
Shape
color
coord
draw
rotate
setColor
Circle
radius
draw
rotate
In our examples we made classes for shape and person. These are abstract concepts
and the classes we make against abstract concepts are called abstract classes. They
are present at or near the top in the class hierarchy to present most generalized
behaviour.
Shape
color
vertices
draw
move
setColor
Person
name
age
gender
eat
walk
Student Doctor
Teacher
Vehicle
color
model
accelerate
applyBrakes
Car Truck
Bus
The entities that actually we see in our real world are called concrete objects and
classes made against these objects are called concrete classes.
Person
Student Doctor
program Teacher
studyYear
study
heldExam
Vehicle
Car Truck
Bus
capacity
load
unload
If there is an abstract class then hierarchy exists in the object model as there will
definitely be some concrete classes as well derived from this abstract class otherwise
there is no use of abstract class.
Glossary:
Lecture No.05
Multiple Inheritance
Inheritance:
We saw inheritance purposes in last lecture
• Generalization
• Extention or sub typing
• Specialization or restriction
Abstract and concrete classes, former is used to represent abstract concepts later is
used to represent concrete concepts.
Overriding derived classes override inherited classes (base classes) behaviour.
Overriding is used for Specialization, Extention, Restriction, and Performance.
Sometimes we want to reuse characteristics of more than one parent class, in that
case we need to inherit a class from more than one classes.
Consider the example of an imaginary specie Mermaid used in fairy tales that lives in
water having features both of a women as well as of a fish, In Object Oriented
programming perspective Mermaid can be derived from two classes Women and
Fish.
Woman Fish
Mermaid
C++ Code:
class Fish {
};
class Woman {
};
};
Our Mermaid class inherits features of both woman and fish suppose our woman
class has method wald() and fish cclass has method swim then our mermaid class
can use both methods i.e can walk as well as can swim.
Woman Fish
void walk() void swim()
Mermaid
C++ code:
#include <iostream>
#include <stdlib.h>
class Fish
{
public:
void swim(){
cout<<"\n In method swim";
}
};
class Woman
{
public:
void walk(){
cout<<"\n In method walk"<<endl;
}
};
};
Mermaid mermaid;
/*This Mermaid object will have two implicit objects one of Fish class and one of
Woman class*/
mermaid.swim();
mermaid.walk();
system("PAUSE");
return 0;
Output:
In method4 swim
In method walk
Take another example of amphibious vehicle (vehicle that can run on land as well as
on water) so it has properties of both land as well as of water vehicle. The general
hierarchy in this case will be,
Vehicle
Here we have added a general Vehicle class as well to add all common functions of
Land Vehicles and Water Vehicles in that class, and specific functions of Land and
4
class member functions are also called class methods
Water vehicle in their respective classes then we have derived Amphibious Vehicle
class from Land Vehicle and Water Vehicle classes (we can do the same in first
example as well concerning Woman, Fish and Mermaid).
C++ code:
class Vehicle
{
};
};
};
};
C++ code:
#include <iostream>
#include <stdlib.h>
class Vehicle
{
public:
void changeGear(){ cout<<"\nI am Vehicle changeGear() function..\n";}
};
public:
void Float(){ cout<<"\nI am float function of Water Vehicle";}
};
};
};
AmphibiousVehicle amphibious;
amphibious.Float();
/*Calling Float function of Water Vehicle class*/
amphibious.Move();
/*Calling Move function of Land Vehicle class*/
system("PAUSE");
return 0;
Output:
As was the case with simple (single) inheritance multiple inheritance also decreases
redundant code as we can inherit a class from many classes and can use their
functions without the need to write them again.
However, there are more disadvantages of multiple inheritance, than its advantages.
Increased complexity
Reduced understanding
Due to increased complexity of class hierarchy the object model becomes difficult it
understand especially for someone who is looking it first time.
Duplicate features
As we are deriving a single class from more than one class so there is a chance of
duplication of features (same methods in both parents), following problems may
arise due to duplicate features,
Problem 1: Ambiguity
Woman Fish
eat eat
… …
Mermaid
As mermaid also needs to eat and its both parents have their own methods of eating
so here question arises,
Which eat operation Mermaid should inherit as both functions are available?
Solution – We can solve this problem by explicitly calling eat method from any of
the parent classes in Mermaid class according to behaviour of Mermaid (i.e. if it eats
like a Woman we can call eat method of Woman class and if eats like Fish we can call
method of Fish class), for this we will Override the Common method in multiply
inherited class and in that class overridden method we will call the appropriate base
class function.