Pure Virtual Function
1
Runtime Polymorphism
• Definition
– Ability to take more than one form
• An essential feature of an OO Language
• It builds upon Inheritance
• Allows run-time interpretation of object type for a
given class hierarchy
– Also Known as “Late Binding”
• Implemented in C++ using virtual functions
2
Dynamic Binding
• Is the run-time determination of which function to call
for a particular object of a derived class based on the
type of the argument
• Declaring a member function to be virtual instructs
the compiler to generate code that guarantees
dynamic binding
• Dynamic binding requires pass-by-reference
3
Whether constructors and
destructors can be virtual??
• Virtual constructors not allowed
• But virtual destructors are allowed
• Because when a pointer is deleted only the of
the class type is called
• Static binding is done
• To do dynamic binding make destructors as
virtual
02/18/16
02/18/16
02/18/16
02/18/16
02/18/16
02/18/16
02/18/16
Employee Class
• Different types of employees based on the
way in which salary is calculated
• All employees has salary but that cannot
be determined in base class
• Force any derived class to implement it
• Make it as pure virtual functions
02/18/16
Pure Virtual Function
• A pure virtual function is a function that has the
notation "= 0" in the declaration of that function. Why
we would want a pure virtual function and what a pure
virtual function looks like is explored in more detail
below.
• Simple Example of a pure virtual function in C++
• class SomeClass
{ public: virtual void pure_virtual() = 0; // a pure virtual
function // note that there is no function body
};
13
The pure specifier
• The "= 0" portion of a pure virtual function is also known
as the pure specifier, because it’s what makes a pure
virtual function “pure”.
• Pure specifier appended to the end of the virtual function
definition may look like the function is being assigned a
value of 0, that is not true.
• The notation "= 0" is just there to indicate that the virtual
function is a pure virtual function, and that the function
has no body or definition.
14
Abstract Classes
• A class with one or more pure virtual functions is
an Abstract Class
• Objects of abstract class can’t be created
• Abstract Class is a class which contains atl east
one Pure Virtual function in it.
• Abstract classes are used to provide an Interface
for its sub classes.
• Classes inheriting an Abstract Class must provide
definition to the pure virtual function, otherwise
they will also become abstract class.
• But pointer of abstract class can hold address of
derived class 15
Simple Example
Math Symbol Problem
• Create an abstract class MathSymbol may provide
a pure virtual function doOperation(), and create two
more classes Plus and Minus implement
doOperation() to provide concrete implementations
of addition in Plus class and subtraction in Minus
class.
16
Algorithm for Math Symbol Problem
• Create Mathsymbol abstract class with
doOperation() pure virtual function.
• Create plus class derived from Mathsymbol.
• Create minus class dervied from
Mathsymbol.
• Perform addition in plus class using
doOperation() pure virtual function.
• Perform subtraction in minus class using
doOperation() pure virtual function.
• Print each values in respective classes.
17
Implementation in C++
• Already learnt creating classes
• Learnt about Inheritance
• Define Pure virtual function in Abstract(base)
class.
• Inherit virtual function for distinct operations.
• Already learnt about how to display the
output.
18
Class Diagram using Dia
19
Example
• Some classes exist logically but not physically.
• Example : Mathsymbol
– Mathsymbol M;
– Mathssymbol makes sense only as a base of some
classes derived from it. Serves as a “category”
– Hence instantiation of such a class must be prevented
class Mathsymbol //Abstract
{
public :
//Pure virtual Function
virtual void doOperation() = 0;
}
Mathsymbol M; // error : variable of an abstract class
20
• A pure virtual function not defined in the
derived class remains a pure virtual function.
• Hence derived class also becomes abstract
class Plus : public Mathsymbol{ //No doOperation() -
Abstract
public :
void print(){
cout << “Doing Addition” << endl;
}
class Minus : public Mathsymbol {
public :
void doOperation(){ // Override Mathsymbol:: doOperation()
int a=10,b=5;
cout << “Subtration is = ” <<a-b<< endl;
}
Minus m; // Valid
Plus p; // error : variable of an abstract class
21
• It is still possible to provide definition of a pure
virtual function in the base class
• The class still remains abstract and functions
must be redefined in the derived classes, but a
common piece of code can be kept there to
facilitate reuse
• In this case, they can not be declared inline
class Mathsymbol { //Abstract
public : class Minus : public Mathsymbol
virtual void doOperation() = 0; {
}; public :
void doOperation(){
// OK, not defined inline Mathsymbol::doOperation(); //Reuse
void Mathsymbol::doOperation(){ cout <<“Subtraction”<< endl;
cout << “Arithmetic" << endl; }
} 22
Example: Pure Virtual Functions
class A { • A is an abstract (base) class
public:
virtual void x() = 0;
– Similar to an interface in Java
virtual void y() = 0; – Declares pure virtual functions (=0)
}; – May also have non-virtual methods, as well
as virtual methods that are not pure virtual
class B : public A {
public:
virtual void x(); • Derived classes override pure virtual
}; methods
– B overrides x(), C overrides y()
class C : public B {
public:
virtual void y(); • Can’t instantiate an abstract class
}; – class that declares pure virtual functions
int main () {
– or inherits ones that are not overridden
A * ap = new C; – A and B are abstract, can create a C
ap->x ();
ap->y ();
delete ap;
• Can still have a pointer or reference to
return 0; an abstract class type
}; – Useful for polymorphism 23
Pure Virtual Functions: Summary
• Pure virtual functions are useful because they
make explicit the abstractness of a class
• Tell both the user and the compiler how it was
intended to be used
• Note : It is a good idea to keep the common code
as close as possible to the root of you hierarchy
24
Difference b/w Virtual and Pure
Virtual Function
• A pure virtual function makes the class it
is defined for abstract. Abstract classes
cannot be instantiated. Derived classes
need to override/implement all inherited
pure virtual functions. If they do not,
they too will become abstract. In C++, a
class can define a pure virtual function
that has an implementation.
25