Course Content
C++ OOP
C++ OOP
Pure Virtual Method
The concept of virtual methods is key to achieving polymorphism, enabling flexible and extensible software design. Pure virtual methods extend this by defining interfaces and supporting dynamic binding.
Syntax of Pure Virtual method
A pure virtual method is declared with the virtual
keyword and initialized to zero. It indicates that the function has no definition within the class
and must be overridden by any subclass before objects of that class
can be instantiated.
virtual.h
virtual void example() = 0;
This method functions similarly to a standard one, except that it lacks a body enclosed in { }
and is instead terminated with = 0;
, indicating no implementation. However, the other rules for this method are the same as for regular methods.
Declaring a pure virtual method makes the class
where it is declared abstract, meaning it becomes impossible to instantiate objects of that class. This restriction exists because every method within the class
must be implemented before creating and using an instance, in order to prevent errors and unpredictable behavior. Look at the code below:
Animal.h
class Animal { public: virtual void speak() = 0; };
The Animal
class
features a pure virtual method named speak()
, preventing the instantiation of objects from this class. This design makes sense, as the purpose of the virtual method is to represent the distinctive sounds each animal makes.
By making the method virtual, it allows each subclass to implement its own version, capturing the individuality of each animal's sound. Declaring it as pure virtual indicates that no default implementation exists, emphasizing the need for concrete implementations in derived classes.
main.cpp
#include "Animal.h" int main() { // cannot declare variable 'animal' to be of abstract type Animal animal; }
This also makes sense. Creating instances of the Animal
class
would be impractical and counterintuitive, as it represents an abstract concept that serves as a category for various animals. There is no specific behavior associated with a generic animal, reinforcing the abstract nature of the class
and highlighting the importance of creating specialized subclasses to capture the unique sounds of each specific animal.
main.cpp
#include <iostream> class Animal { public: // Pure virtual function to enforce implementation in derived classes virtual void speak() = 0; }; class Cat : public Animal { public: void speak() override { std::cout << "Meow!" << std::endl; } }; class Dog : public Animal { public: void speak() override { std::cout << "Bark!" << std::endl; } }; class Cow : public Animal { public: void speak() override { std::cout << "Moo!" << std::endl; } }; void pet(Animal& animal) { animal.speak(); } int main() { // Replace `Cat` with `Dog` or `Cow` to see their specific behavior Cat cat; pet(cat); }
While it's not possible to create an object of type Animal
directly, we can still use it as a parameter in a function. Similar to the earlier button example, this approach leverages polymorphism to craft a versatile program that can dynamically alter its behavior at runtime.
Try passing an object of a different class
to the function and observe the output. Also, attempt to create an object of the abstract class
to see how the compiler prevents instantiation due to unimplemented pure virtual methods.
Thanks for your feedback!