Q13Object Oriented Programming
Question
When do we make a virtual function "pure"? What are the implications of making a function a pure virtual function?
Answer
A deep architectural explanation of Pure Virtual Functions, detailing how setting a function to = 0 mathematically instantly transforms the class into an Abstract Base Class, strictly enforcing interface contracts on all derived subclasses.
Polymorphism is a core pillar of C++, typically achieved utilizing standard virtual functions, allowing a base class pointer to mathematically execute the overridden method of a derived class. However, a "Pure Virtual Function" is a violently extreme modification of this architecture. It is a function that possesses a strict mathematical declaration, but absolutely zero physical implementation.
The Syntax of Purity
To mathematically force a virtual function into a state of "purity," the programmer aggressively appends = 0 directly to the end of the declaration inside the class body.
virtual void calculateArea() = 0;
This = 0 is not an assignment of an integer; it is a strict compiler directive indicating that this function literally does not exist in the current class and MUST be written elsewhere.
When Do We Use a Pure Virtual Function?
A function is made "pure" when the Base Class represents a concept so highly abstract that writing an implementation for the function is mathematically impossible or logically absurd.
For example, imagine a Base Class called Shape. It contains a virtual void draw(). How do you mathematically draw a generic, undefined "Shape"? You cannot. You can only draw a specific Circle or Square. Therefore, draw() in the Shape class must be made pure, forcing the specific subclasses to figure out the implementation details.
The Severe Architectural Implications
Declaring even a single pure virtual function within a class triggers massive, immediate architectural consequences enforced strictly by the compiler:
- 1. Absolute Instantiation Ban (Abstract Class): The exact millisecond a class contains a pure virtual function, the compiler instantly reclassifies it as an "Abstract Base Class." The compiler will now violently refuse to instantiate an object of this class. If you write
Shape s;, the compilation will catastrophically fail. You cannot build an object out of incomplete blueprints. - 2. The Unbreakable Contract: The Abstract Base Class now acts as an ultra-strict, unbreakable interface contract. Any Derived Class that inherits from this base class is legally obligated by the compiler to fully implement (override) every single pure virtual function.
- 3. Contagious Abstraction: If a Derived Class inherits from the Abstract Base Class, but the programmer forgets or refuses to implement the pure virtual function, the compiler punishes the Derived Class by instantly turning it into an Abstract Class as well, blocking its instantiation.
- 4. Pointer Polymorphism Survives: While you cannot instantiate an object of an Abstract Class, you can create a pointer of that type (
Shape* ptr). This pointer can then aggressively hold the memory address of any derived subclass (likeCircle), allowing the program to execute complex polymorphic arrays (e.g., looping through an array ofShape*and callingdraw(), which flawlessly executes the specific circle/square drawing algorithms).