Professional Documents
Culture Documents
7 Динамічний поліморфізм, віртуальні функції, конструктори, деструктори
7 Динамічний поліморфізм, віртуальні функції, конструктори, деструктори
Programming
Maksym M. Dreval
maks.dreval@gmail.com
Spring 2018
Dynamic Polymorphism.
Virtual Methods &
Abstract Classes
Динамічний поліморфізм.
Віртуальні методи та
абстрактні класи
Problem Statement
Shape
draw
calcArea
if ( _shape[i]->getType() == ‘L’ )
static_cast<Line*>(_shape[i])->draw();
else if ( _shape[i]->getType() == ‘C’ )
static_cast<Circle*>(_shape[i])->draw();
…
Sample Output
Line
Circle
Triangle
Circle
…
Problems with Switch
Statement
…Delocalized Code
• Hard to maintain
Solution?
• Consequently, sender of a
message does not need to know
the exact class of receiver
Virtual Functions
• Target of a virtual function call is
determined at run-time
• In C++, we declare a function
virtual by preceding the function
header with keyword “virtual”
class Shape {
…
virtual void draw();
}
Shape Hierarchy
Shape
draw
calcArea
Shape
draw
calcArea
• Cannot be instantiated
• Can be instantiated
class Shape {
…
public:
virtual void draw() = 0;
}
…
Shape s; // Error!
… Pure Virtual Functions
Rectangle
draw
… Pure Virtual Functions
• Output
Shape destructor called
Result
pShape pShape
Shape Part
Quad Part Quad Part
Rect Part Rect Part
Before After
Virtual Destructors
• Make the base class destructor
virtual
class Shape {
…
public:
virtual ~Shape() {
cout << “Shape destructor
called\n”; }
}
…Virtual Destructors
class Quadrilateral : public Shape {
…
public:
virtual ~Quadrilateral() {
cout << “Quadrilateral destructor
called\n”;
}
}
…Virtual Destructors
class Rectangle : public
Quadrilateral {
…
public:
virtual ~Rectangle() {
cout << “Rectangle destructor
called\n”;
}
}
…Virtual Destructors
• Output
Rectangle destructor called
Quadilateral destructor called
Shape destructor called
Result
pShape pShape
Shape Part
Quad Part
Rect Part
Before After
Virtual Functions – Usage
pShape
Dynamic Dispatch
• For non-virtual functions, compiler
just generates code to call the
function
• In case of virtual functions,
compiler generates code to
–access the object
–access the associated vTable
–call the appropriate function
Conclusion
• Polymorphism adds
– Memory overhead due to vTables
– Processing overhead due to extra
pointer manipulation
• However, this overhead is acceptable
for many of the applications
• Moral: “Think about performance
requirements before making a
function virtual”
Multiple Inheritance
Множинне спадкування
Multiple Inheritance
Mermaid
Example – Multiple
Inheritance
Woman Fish
Mermaid
Example – Multiple
Inheritance
Amphibious Vehicle
Example – Multiple
Inheritance
Vehicle
• Reduced understanding
• Duplicate features
Problem – Duplicate
Features
Woman Fish
eat eat
… …
Mermaid
Woman Fish
eat eat
… …
Mermaid
Invoke eat
eat
operation of
… desired class
Problem – Duplicate Features
(Diamond Problem)
Vehicle
changeGear
Transmitter Receiver
... ...
Transmit() Receive()
Phone
Example
class Mermaid:
private Woman, private Fish
Multiple Inheritance
• The derived class inherits data
members and functions form all
the base classes
• Object of derived class can
perform all the tasks that an
object of base class can perform
Example
int main(){
Phone obj;
obj.Transmit();
obj.Receive();
return 0;
}
Multiple Inheritance
int main(){
AmphibiousVehicle obj;
obj.LandVehicle::GetMaxLoad();
obj.WaterVehicle::GetMaxLoad();
return 0;
}
Multiple Inheritance