Virtual Function & Abstract
Classes
Virtual Functions
A means to achieve dynamic polymorphism
A virtual function can have a definition in base
class, called in case derived class does not
override that function
Used to support polymorphism with pointers and
references
When virtual function called via pointer ->, the
reference is resolved at run time
called dynamic binding
When virtual function called by referencing a
specific object (using dot operator) reference
is resolved at compile time
called static binding
A virtual function declaration contains virtual
keyword
A class that declares or inherits a virtual function
is called a polymorphic class.
Any no static member function except a
constructor can be virtual
Virtual Functions
Virtual functions can also be friends of other
classes
Some compilers require the class destructor to
be virtual if a class contains any virtual functions
Non-virtual function can call virtual function
Virtual functions in the base class provide
common interface for all of its derived classes
class MyClass
{
public:
virtual void MyFunction()
{
cout << “virtual function”;
}
};
Pure Virtual Functions
Pure virtual function ensures that all derived
classes must override it
A pure virtual function,Vf, can have (optional)
definition in base class, can only be called
using Base::Vf()
Pure virtual function declaration end with = 0;
an indication that it is a pure virtual function
A class becomes Abstract if it contains at
least one pure virtual function
Example: Pure virtual Functions
class Shape
{
public:
// pure virtual function
virtual void Draw() = 0;
};
Abstract Classes
Classes from which it is never intended to
instantiate any objects
Incomplete—derived classes must define the
“missing pieces”.
Too generic to define real objects.
Normally used as base classes and called
abstract base classes
Provide appropriate base class frameworks from
which other classes can inherit.
Abstract Classes
A class is made abstract by declaring one or more
of its virtual functions to be “pure”
I.e., by placing "= 0" in its declaration
A pure virtual function is a virtual member function
of a base class that must be overridden.
Example
virtual void draw() const = 0;
"= 0" is known as a pure specifier.
Tells compiler that there is no implementation.
Abstract classes can have data and concrete functions
Required to have one or more pure virtual functions
Concrete Classes
Classes used to instantiate objects
Provide specifics to make real objects
Every concrete derived class must override all
base-class pure virtual functions
with concrete implementations
If even one pure virtual function is not overridden,
the derived-class will also be abstract
Abstract
AbstractClass
Class
Concrete
ConcreteClasses
Classes
11
class Shape
{
public:
virtual void Draw() = 0; // pure virtual function
};
class Line : public Shape
{
public:
//override interface function
void Draw() // also virtual in derived class
{
cout << “Line is being drawn”;
// …
}
};
Example: Pure virtual Functions / Abstract Class (contd.)
void main()
{
// Shape obj; // error as Shape class is abstract
Line lineObj; // creating a derived class object
Shape* ptr; // creating a base class pointer
ptr = &linedObj; // assigning address of derived
class // object to base class pointer
ptr->Draw(); // derived class draw() will be
called
}