Overview
In C++, virtual functions and pure virtual functions are fundamental concepts used in the implementation of polymorphism, a core feature of object-oriented programming. Understanding the differences between these two types of functions and knowing when to use each is crucial for designing flexible and maintainable software.
Key Concepts
- Polymorphism: The ability of different classes to provide a unique interface for different implementations of methods.
- Virtual Functions: Member functions in a class that can be overridden in derived classes.
- Pure Virtual Functions: A virtual function that does not provide an implementation and must be overridden in derived classes, making the base class abstract.
Common Interview Questions
Basic Level
- What is a virtual function in C++?
- How do you declare a pure virtual function?
Intermediate Level
- What is the difference between virtual functions and pure virtual functions?
Advanced Level
- How do virtual functions and pure virtual functions affect class design and polymorphism?
Detailed Answers
1. What is a virtual function in C++?
Answer: A virtual function in C++ is a function that can be overridden in derived classes. It is declared in the base class using the virtual
keyword. When a base class pointer or reference points to a derived class object, calling a virtual function invokes the version defined in the derived class, achieving polymorphism.
Key Points:
- Virtual functions enable runtime polymorphism.
- They are declared in the base class and can be overridden in derived classes.
- Use the virtual
keyword in the function declaration.
Example:
class Base {
public:
virtual void display() {
cout << "Display Base class" << endl;
}
};
class Derived : public Base {
public:
void display() override { // Override the base class function
cout << "Display Derived class" << endl;
}
};
2. How do you declare a pure virtual function?
Answer: A pure virtual function is declared by assigning 0
to the virtual function in its declaration in the base class. This makes the base class abstract, meaning it cannot be instantiated. Derived classes must override all pure virtual functions to become concrete classes that can be instantiated.
Key Points:
- Declaring a pure virtual function makes the class abstract.
- Pure virtual functions must be overridden in derived classes.
- Use = 0
in the function declaration to make it pure virtual.
Example:
class Base {
public:
virtual void display() = 0; // Pure virtual function
};
class Derived : public Base {
public:
void display() override { // Provides implementation
cout << "Display Derived class" << endl;
}
};
3. What is the difference between virtual functions and pure virtual functions?
Answer: The key difference lies in the requirement and provision of the function body. Virtual functions have an implementation in the base class that can be overridden by derived classes, while pure virtual functions do not have an implementation in the base class, mandating that derived classes provide an implementation. Pure virtual functions make a class abstract, preventing it from being instantiated directly.
Key Points:
- Virtual functions provide default behavior that derived classes can override.
- Pure virtual functions do not provide default behavior and require derived classes to provide an implementation.
- A class with at least one pure virtual function becomes an abstract class.
Example:
// Virtual function example
class Base {
public:
virtual void show() { cout << "Base show" << endl; }
};
// Pure virtual function example
class AbstractBase {
public:
virtual void show() = 0; // No implementation in the base class
};
4. How do virtual functions and pure virtual functions affect class design and polymorphism?
Answer: Virtual functions and pure virtual functions are instrumental in achieving runtime polymorphism, allowing objects to behave differently based on their actual class rather than their type of reference or pointer. Virtual functions provide flexibility by allowing derived classes to either use or override the base class implementation. Pure virtual functions enforce a contract that derived classes must implement specific functionality, leading to a clear and structured design where base classes define interfaces through pure virtual functions.
Key Points:
- Virtual functions allow for flexible overriding of behavior in derived classes.
- Pure virtual functions enforce an interface contract for derived classes.
- Both mechanisms enable runtime polymorphism, enhancing design flexibility and reuse.
Example:
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
virtual void move() { cout << "Move shape" << endl; } // Virtual function
};
class Circle : public Shape {
public:
void draw() override { cout << "Draw circle" << endl; } // Implementation of pure virtual function
};
This design allows for different kinds of shapes to be implemented and manipulated polymorphically, adhering to a common interface while allowing for specific behaviors.