What are virtual functions and pure virtual functions?
- How virtual functions support runtime polymorphism and the role of abstract classes.
Virtual Functions and Pure Virtual Functions in C++
In C++, virtual functions play a crucial role in achieving runtime polymorphism, allowing different classes to be treated as instances of a common base class. Additionally, pure virtual functions help in creating abstract classes, which cannot be instantiated but can be used as a base for derived classes.
Virtual Functions
In C++, a virtual function is a member function declared within a base class and redefined by a derived class. When a virtual function is called through a base class pointer or reference, the actual function that gets executed is determined at runtime based on the type of object pointed to by the pointer or referenced by the reference.
class Shape {
public:
virtual void draw() {
// Base class implementation
}
};
class Circle : public Shape {
public:
void draw() override {
// Derived class implementation
}
};
In the example above, the draw() function is declared as virtual in the Shape base class and overridden in the Circle derived class. When draw() is called on a Shape pointer pointing to a Circle object, the Circle version of draw() will be executed.
Pure Virtual Functions
A pure virtual function is a virtual function that is declared in a base class but not defined. Classes containing pure virtual functions are known as abstract classes, and they cannot be instantiated. Instead, they are meant to serve as base classes for other classes to inherit from and provide concrete implementations for the pure virtual functions.
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
class Circle : public Shape {
public:
void draw() override {
// Derived class implementation
}
};
In this example, the draw() function in the Shape class is declared as a pure virtual function using = 0. Any class inheriting from Shape must provide an implementation for draw() to become concrete.
Runtime Polymorphism and Abstract Classes
Virtual functions and abstract classes play a key role in achieving runtime polymorphism in C++. By using virtual functions, you can write code that operates on objects of different classes through a common interface provided by the base class. This flexibility allows you to write more generic and reusable code.
Abstract classes serve as blueprints for derived classes to implement specific behaviors. They define a common interface that all derived classes must adhere to while allowing each derived class to provide its own unique implementation. This helps in promoting code reusability and maintaining a clear hierarchy of classes in your program.
Conclusion
Virtual functions and pure virtual functions are essential features in C++ that enable runtime polymorphism and the creation of abstract classes. By leveraging these concepts, you can write more flexible and extensible code that accommodates diverse behaviors while maintaining a structured class hierarchy.