Page 230 - DCAP404 _Object Oriented Programming
P. 230
Unit 10: Virtual Functions and Polymorphism
To avoid this situation virtual or abstract base class is used. Both of the derived classes B and C Notes
are created as virtual base classes, meaning they should share a common copy of member in
their base class. The modified definition is shown in Figure 10.2.
Figure 10.2: Use of Virtual Base Classes to Disambiguation of Inheritance
Self Assessment
Fill in the blanks:
9. A base class containing one or more pure virtual member functions is called an
……………………… class.
10. An abstract class contains at least one………………………….. .
11. A class derived from an abstract base class will also be abstract unless you …………………..
each pure virtual function in the derived class.
12. Only a subclass of an abstract class can be instantiated directly if all ……………………….
pure virtual methods have been implemented by that class or a parent class.
10.4 Polymorphism
Polymorphism is an important OOP concept. Polymorphism means the ability to take more
than one form. For example, an operation may exhibit different behavior in different instances.
The behavior depends upon the types of data used in the operation. For example, consider the
operation of addition. For tow numbers, the operation will generate a sum. If the operands are
strings, then the operation will produce a third string by contention. The diagram given below,
illustrates that a single function name can be used to handle different number and types of
arguments. This is something similar to a particular word having several different meanings
depending on the context.
Task Polymorphism means that some code or operations or objects behave differently
in different contexts. In a group of four analyze the meaning of this statement with an
example.
Polymorphism plays an important role in allowing objects having different internal structures
to share the same external interface. This means that a general class of operations may be
LOVELY PROFESSIONAL UNIVERSITY 223