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.
The following example illustrates that how pure virtual functions are declared in abstract classes and defined in the derived classes.
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.
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
- Abstract class must have one or more than one pure virtual functions.
- Abstract class cannot be instantiated.
- Abstract class must be inherited.
- 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.
- Although objects of abstract class cannot be created but its pointers can be created as shown in the example below.
More Related Articles For You
- What is C++
- C++ and Object Oriented Programming OOPS concepts
- Syntax and Structure of C++ program
- Data Types in C++
- C++ Variables
- Types of operators in C++
- Decision making in C++
- C++ Loop Types
- Storage Classes in C++
- Functions in C++
- Classes and Objects in C++
- Access controls in C++ Classes
- Defining Class and Object in C++
- Accessing Public and Private Data Members in C++
- Member Functions in Classes
- Types of Member Functions in C++
- Inline Functions in C++
- Namespaces in C++
- Function Overloading in Classes in C++
- Constructors and Destructors in C++
- Static Keywords in C++
- Const Keyword in C++ Programming
- References in C++
- Copy Constructor in C++
- Pointer to Members in C++ Classes
- Introduction to Inheritance
- Types of Inheritance
- Order of Constructor Call in Inheritance
- Upcasting in C++
- Function Overriding in C++
- Virtual Functions in C++
- Virtual Destructors in C++
- Operator Overloading in C++