[go: up one dir, main page]

0% found this document useful (0 votes)
60 views24 pages

09 Polymorphism

Uploaded by

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

09 Polymorphism

Uploaded by

l226676
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

1

CS217 Object Oriented Programming

Polymorphism and Virtual functions


2

Overriding a functions in the subclass

When a subclass declares a function of the


name previously known in its superclass, the
original method is overridden. This means that
the subclass hides the previous meaning of the
function identifier and any invocation encoded
within the subclass will refer to the newer method.
3
4

Overriding a function in the subclass


5

Overriding a method in the subclass


I want to ask you now for your full attention as we are going
to introduce one of the most important objective notions:

Polymorphism.

This is a method to redefine the behavior of a


superclass without touching its implementation.
6

Overriding a method in the subclass


• The word “polymorphism” means that the one and same
class may show many (“poly”) forms (“morphs”) not
defined by the class itself, but by its subclasses.

• Another definition says that polymorphism is the ability to


realize class behaviour in multiple ways.

• Both definitions are ambiguous and could not actually be


used to explain the true nature of the phenomenon. I
believe that an example will work better.
7
• Take a look at the code below. It’s almost the same as the previous one.
The main function may look different but the class definitions are nearly identical.
There’s only one difference – one keyword has been added to the code.
• Can you find it?
8

Overriding a method in the subclass


Yes, you’re right – it’s the word “virtual”, placed in front of
the MakeSound member function, inside the Pet class body.
This word means that the method won’t be overridden within any
of the possible subclasses. It also means that the method will
be redefined(replaced) at the level of the original class.
To make a long story short – the example program will output a
somewhat surprising piece of text. This is how it goes:

Kitty the Cat says: Meow! Meow!


Kitty the Cat says: Meow! Meow!
Kitty the Cat says: Meow! Meow!
Doggie the Dog says: Woof! Woof!
Doggie the Dog says: Woof! Woof!
Doggie the Dog says: Woof! Woof!
9

6.3.4 Overriding a method in the subclass


(4)
The next example shows that the binding between the origin of the virtual
function (inside the superclass) and its replacement (defined within the
subclass) is created dynamically, during the execution of the program.
Take a look at the example →
10

6.3.4 Overriding a method in the subclass


(4)
We invoke the MakeSound method as part of
the Pet constructor. We already know that the method
is polymorphically replaced by the new implementations
presented by the Cat and Dog subclasses. We don’t know
yet when the replacement occurs.
The program will output the following lines:

Kitty the Pet says: Shh! Shh!


Doggie the Pet says: Shh! Shh!

This means that the binding between the original functions


and their polymorphic implementations is established when
the subclass object is created, not sooner.
11

6.3.5 Overriding a method in the subclass


(5)
• Overriding a method in the subclass – continued
• The experiment we’re going to perform using our software guinea pig refers to the fact
that the virtual method may be invoked not only from outside the class but also from
within.
• Check out this example, please →
12

Classes and Virtual Destructors


• Classes with pointer member variables should have the
destructor
• Destructor can be designed to deallocate storage for dynamic
objects
• If a derived class object is passed to a formal parameter
of the base class type, destructor of the base class
executes
• Regardless of whether object is passed by reference or by value
• Solution: use a virtual destructor (base class)
13
14
15
Virtual functions for class A:
fun2

Virtual functions for class B:


fun1 and fun2

Virtual functions for class C:


fun1 and fun2 and fun3
16

Classes and Virtual Destructors (continued)


• The virtual destructor of a base class automatically
makes the destructor of a derived class virtual
• After executing the destructor of the derived class, the destructor of
the base class executes
• If a base class contains virtual functions, make the
destructor of the base class virtual
17
18

Abstract Classes and Pure Virtual Functions


• Through inheritance, we can derive new classes without
designing them from scratch
• Derived classes inherit existing members of base class, can add
their own members, and also redefine or override public and
protected member functions
• Base class can contain functions that you would want each derived
class to implement
• Base class may contain functions that may not have meaningful
definitions in the base class
19

Abstract Classes and Pure Virtual Functions


(continued)

• To make them pure virtual functions:


20

B C
Y Z
21

Virtual functions
Class A is Abstract class

Class B is non abstract class


22

Abstract Classes and Pure Virtual Functions


(continued)
• Abstract class: contains one or more pure virtual functions

You cannot create objects of an abstract class


23

Abstract Classes and Pure Virtual Functions


(continued)
• If we derive rectangle from shape, and want to make it
a nonabstract class:
• We must provide the definitions of the pure virtual functions of its
base class
• Note that an abstract class can contain instance variables,
constructors, and functions that are not pure virtual
• The class must provide the definitions of constructor/functions that
are not pure virtual
24

You might also like