Abstract Class and Pure Virtual Functions in C++

Abstract Class and Pure Virtual Functions

While defining a class we should know about the data members and member functions. Sometimes we know about the name of the member function to be declared but don’t know about the details of the member function and want that the derived class (or classes) should define the details of this member function. In this case we define the member functions as pure virtual functions.

Pure virtual function is a member function declared in a class with virtual keyword and initialized with 0. It does not have any body. The body of the pure virtual function is defined by the children of this class and the class where pure virtual function is declared is called as abstract class. In general abstract class might have a number of data members and member functions including at least one pure virtual function.

Abstract class cannot be instantiated but we can inherit it in derived classes. The derived classes must define the pure virtual functions declared in the base class (abstract class). If the derived class of an abstract class does not define the pure virtual functions declared in the base class then the derived class is also considered as abstract class and cannot be instantiated.

The derived class is then derived by another class where the pure virtual functions declared in the above hierarchy must be defined otherwise the process will continue and all the classes are treated as abstract classes.

An Example:

The following example illustrates that how pure virtual functions are declared in abstract classes and defined in the derived classes.

virtual functions

In the above code pure virtual function is defined at line 9. We can see that pure virtual functions are declared by using virtual keyword and by initializing by 0. It is also important to note that there is no body of this function. If we try to define the body of pure virtual functions then compiler will not allow it and will produce a syntax error.

Abstract Class

The class where the pure virtual function is declared is called abstract class. We can define objects of this class. In the above case, Person is treated as abstract class where the pure virtual function is declared. This class must be inherited by another class to define this function. In the above code, Student class inherits the Person class and defines the pure virtual function of its parent class at line 14.

Characteristics of Abstract Classes

  1. Abstract class must have one or more than one pure virtual functions.
  2. Abstract class cannot be instantiated.
  3. Abstract class must be inherited.
  4. The pure virtual functions declared in the abstract class must be defined by all the derived classes. If a derived class doesn’t define the pure virtual function declared by its parent class then this derived class will be treated as an abstract class itself.
  5. Although objects of abstract class cannot be created but its pointers can be created as shown in the example below.

Characteristics of Abstract Classes

More Related Articles For You

    C++ Tutorial

    C++ Quizzes