Junior — Middle
Explain what the term 'pure virtual method' means in object-oriented programming.
sobes.tech AI
Answer from AI
A pure virtual method is a method in a base class that has no implementation and must be overridden in derived classes. In C++, it is declared with = 0 in the method declaration. A class containing at least one pure virtual method becomes abstract and cannot be instantiated.
Example:
class Shape {
public:
virtual void draw() = 0; // pure virtual method
};
class Circle : public Shape {
public:
void draw() override {
// implementation of circle drawing
}
};
This allows creating interfaces and ensuring polymorphism, forcing inheritors to implement the necessary methods.