Sometimes implementation of all function cannot be provided in a base class because we don’t know the implementation. Such a class is called abstract class. For example, let Shape be a base class. We cannot provide implementation of function draw() in Shape, but we know every derived class must have implementation of draw(). Similarly an Animal class doesn’t have implementation of move() (assuming that all animals move), but all animals must know how to move. We cannot create objects of abstract classes.
A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration. See the following example.
A complete example:
A pure virtual function is implemented by classes which are derived from a Abstract class. Following is a simple example to demonstrate the same.
Some Interesting Facts:
1) A class is abstract if it has at least one pure virtual function.
In the following example, Test is an abstract class because it has a pure virtual function show().
Compiler Error: cannot declare variable 't' to be of abstract type 'Test' because the following virtual functions are pure within 'Test': note: virtual void Test::show()
2) We can have pointers and references of abstract class type.
For example the following program works fine.
3) If we do not override the pure virtual function in derived class, then derived class also becomes abstract class.
The following example demonstrates the same.
Compiler Error: cannot declare variable 'd' to be of abstract type 'Derived' because the following virtual functions are pure within 'Derived': virtual void Base::show()
4) An abstract class can have constructors.
For example, the following program compiles and runs fine.
x = 4, y = 5
Comparison with Java
In Java, a class can be made abstract by using abstract keyword. Similarly a function can be made pure virtual or abstract by using abstract keyword. See
Abstract Classes in Java for more details.
Interface vs Abstract Classes:
An interface does not have implementation of any of its methods, it can be considered as a collection of method declarations. In C++, an interface can be simulated by making all methods as pure virtual. In Java, there is a separate keyword for interface.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Virtual functions in derived classes
- Pure virtual destructor in C++
- Pure Functions
- Abstract Classes in Java
- C++ interview questions on virtual function and abstract class
- Can static functions be virtual in C++?
- Can virtual functions be inlined?
- Can virtual functions be private in C++?
- Virtual Functions and Runtime Polymorphism in C++ | Set 1 (Introduction)
- What happens when a virtual function is called inside a non-virtual function in C++
- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
- Mathematical Functions in Python | Set 1 (Numeric Functions)
- abstract keyword in java
- Difference between Abstract Class and Interface in Java
- Virtual Function in C++
Improved By : Lavish Saluja