Pure Virtual function and Abstract Class in C++

Pure Virtual function and Abstract Class in C++

What is Abstract Class in C++?

  • An abstract class in C++ is a class that cannot be instantiated directly, meaning you cannot create objects of an abstract class.
  • Instead, it serves as a template for other classes by defining a common interface and behavior that derived classes must adhere to.

Key Characteristics of an Abstract Class in C++

Pure Virtual Function

  • Pure virtual function is a special type of virtual function that is declared in an abstract base class but has no implementation in that class.
  • An abstract class typically contains one or more pure virtual functions, which are declared using the virtual keyword followed by = 0.
  • These functions have no implementation in the abstract class and must be implemented by any class that derives from it.
Loading…

Inability to Instantiate

You cannot create objects of an abstract class. Attempting to do so will result in a compilation error.
Loading…

Derived Class Implementation

  • Derived classes that inherit from an abstract class must provide concrete implementations for all pure virtual functions defined in the abstract base class.
  • Not adhering to this will lead to a compilation error.

Abstract Class in C++ Syntax

Loading…
  • class AbstractClassName: Defines the abstract class with a specific name (AbstractClassName in this example).
  • public: Specifies the access control for the class members.
  • virtual returnType functionName(parameters) = 0: Declares a pure virtual function within the abstract class.
  • The = 0 at the end of the declaration indicates that this is a pure virtual function with no implementation.

Abstract Class in C++ Example

let's understand abstract class with an example:
Loading…
  • Shape is an abstract base class with a pure virtual function calculateArea(). Objects of the Shape class cannot be instantiated.
  • Two derived classes, Circle and Rectangle, inherit from Shape and provide concrete implementations of the calculateArea() function.

Constructor in Abstract class?

  • Yes, an abstract class in C++ can have a constructor. Abstract classes can have constructors just like any other class.
  • When you create an object of a derived class, the constructor of the abstract base class is automatically called to set up the base class part of the object. for example
Loading…
In this example, both the abstract class AbstractClass and the concrete-derived class ConcreteClass have constructors.

Conclusion

Abstract classes act as blueprints, guiding the structure of derived classes, while pure virtual functions serve as placeholders for functionality that must be implemented by those derived classes.
Key takeaways:
  • Abstract classes cannot be instantiated and are meant to be inherited from.
  • Pure virtual functions have no implementation in the abstract class and must be implemented by derived classes.
  • Abstract classes and pure virtual functions promote code modularity, extensibility, and polymorphism in object-oriented programming.