Pure virtual function in c++ with example program
What is a virtual function in c++ :
- A virtual function is a member function that is declared within a base class and redefined by a derived class. To create virtual function, precede the function’s declaration in the base class with the keyword virtual. When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its own needs.
- A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class if the derived class is not abstract. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly.
Pure Virtual Functions in c++ :
- Pure virtual Functions are virtual functions with no definition. They start with virtual keyword and ends with = 0. Here is the syntax for a pure virtual function,
virtual void f() = 0;
Pure Virtual Functions program in c++ :
class Base //Abstract base class
virtual void show() = 0; //Pure Virtual Function
class Derived:public Base
cout << "wikitechy";
Base obj; //Compile Time Error
b = &d;
Pure Virtual definitions in c++ :
- Pure Virtual functions can be given a small definition in the Abstract class, which you want all the derived classes to have. Still you cannot create object of Abstract class.
- The Pure Virtual function must be defined outside the class definition. If you will define it inside the class definition, complier will give an error. Inline pure virtual definition is Illegal.
- A virtual function is a member function that is declared within a base class and redefined by a derived class.
C++ Abstract class and Pure virtual Function :
- In C++ programming, sometimes inheritance is used only for the better visualization of data and you do not need to create any object of base class.
- For example: If you want to calculate area of different objects like: circle and square then, you can inherit these classes from a shape because it helps to visualize the problem but, you do not need to create any object of shape. In such case, you can declare shape as a abstract class. If you try to create object of a abstract class, compiler shows error.
// An abstract class
// Data members of class
// Pure Virtual Function
virtual void show() = 0;
/* Other members */
Why can't we create Object of Abstract Class in c++ :
- When we create a pure virtual function in Abstract class, we reserve a slot for a function in the VTABLE(studied in last topic), but doesn't put any address in that slot. Hence the VTABLE will be incomplete. as the VTABLE for Abstract class is incomplete, hence the compiler will not let the creation of object for such class and will display an errror message whenever you try to do so.
Why would you want a pure virtual function to have an implementation :
- It is actually pretty rare to see a pure virtual function with an implementation in real-world code, but having that implementation may be desirable when you think that classes which derive from the base class may need some sort of default behavior for the pure virtual function. So, for example, if we have a class that derives from our SomeClass class above, we can write some code like this – where the derived class actually makes a call to the pure virtual function implementation that is inherited:
//this class derives from SomeClass
class DerivedClass: public SomeClass
virtual void pure_virtual()
Makes a call to the pure virtual function
implementation that is inside the SomeClass
class. This can happen because DerivedClass
may not have anything appropriate to define
for this function, so it just calls the SomeClass's
When should pure virtual functions be used in C++ :
- In C++, a regular, "non-pure" virtual function provides a definition, which means that the class in which that virtual function is defined does not need to be declared abstract. You would want to create a pure virtual function when it doesn’t make sense to provide a definition for a virtual function in the base class itself, within the context of inheritance.
Virtual functions program in c++ :
class base // Base Class
public: void show() //NON Virtual function
cout<<"pure virtual function\\n";
virtual void display() //Virtual function
class derived: public base //Deriving new class from base class
cout<<"derived - Show \\n";
cout<<"derived - display \\n";
pure virtual function
pure virtual function
derived – display
What is a virtual Destructors :
- Destructors in the Base class can be Virtual. Whenever Upcasting is done, Destructors of the Base class must be made virtual for proper destrucstion of the object when the program exits.
NOTE : Constructors are never Virtual, only Destructors can be Virtual.