Coupon Accepted Successfully!


Pure Virtual Functions

In practical applications, the member function of the base class is rarely used for doing any operation; such functions are called do-nothing functions, dummy functions, or pure virtual functions. The do-nothing functions or pure functions are always virtual functions. Usually, pure virtual functions are defined with a null body. This is so, because derived classes should be able to override them. Any normal function cannot be declared as a pure function. After the declaration of a pure function in a class, the class becomes an abstract class. It cannot be used to declare any object. Any attempt to declare an object will result in the error “cannot create instance of abstract class.” The pure function can be declared as follows:

Declaration of pure virtual function

virtual void display() =0; // pure function

In the above declaration of the function, the display() is a pure virtual function. The assignment operator is not used to assign zero to this function. It is used just to instruct the compiler that the function is a pure virtual function and that it will not have a definition.
A pure virtual function declared in the base class cannot be used for any operation. The class containing the pure virtual function cannot be used to declare objects. Such classes are known as abstract classes or pure abstract classes. Anyone who attempts to declare an object from the abstract class would be reported an error message by the compiler. In addition, the compiler will display the name of the virtual function present in the base class. The classes derived from the pure abstract classes are required to re-declare the pure virtual function. All other derived classes without pure virtual functions are called concrete classes. The concrete classes can be used to create objects. A pure virtual function is similar to an unfilled container that the derived class is made to fill.

15.8 Write a program to declare pure virtual functions.



class first



int b;


first() {b=10;}

virtual void display() =0; // pure function


class second: public first


int d;


second() {d=20;}
void display() {cout<<“\n b=”<<b <<“ d=”<<d;}

int main()

first *p;
// p->display // abnormal program termination
second s;
return 0;


b= 10 d = 20


Explanation: In the above program, the display() function of the base class is declared a pure function. The pointer object *p holds the address of the object of the derived class and invokes the function display() of the derived class. Here, the function display() of the base class does nothing. If we try to invoke the pure function using the statement p-> display() as per the given remarks in the above program (//p->display//abnormalprogram termination), the program is terminated with the error “abnormal program termination.

Test Your Skills Now!
Take a Quiz now
Reviewer Name