What is an Abstract Class in C++?
Understand abstract classes in C++: pure virtual functions, why they can't be instantiated, and how they define interfaces for polymorphism, with code examples.
Expected Interview Answer
An abstract class in C++ is a class that has at least one pure virtual function and therefore cannot be instantiated on its own; it exists to define a common interface that derived classes must implement.
A pure virtual function is declared with '= 0' and provides no required body, forcing every concrete derived class to override it before it can be instantiated. Abstract classes may still contain data members, constructors, and ordinary implemented methods, so they can supply shared state and default behaviour alongside the interface. They are the C++ mechanism for programming to an interface and enabling polymorphism through base-class pointers or references.
- Defines a common interface all derived classes must follow
- Prevents instantiation of an incomplete concept
- Enables polymorphism through base pointers and references
- Lets you mix required interface with shared implementation
- Improves extensibility by decoupling callers from concrete types
AI Mentor Explanation
An abstract class is like the idea of a 'Bowler' in the abstract: every bowler must be able to deliver a ball, but 'bowler' alone isn't a role you can pick for the team. Only concrete kinds — a fast bowler or a spinner — actually define how the delivery works, and only they can walk onto the field. The abstract Bowler fixes the requirement without ever bowling itself.
Step-by-Step Explanation
Step 1
Declare a pure virtual function
Inside the class, write 'virtual returnType name() = 0;' to make the function pure virtual.
Step 2
Class becomes abstract
Any class with at least one pure virtual function is abstract and cannot be instantiated directly.
Step 3
Add shared members if useful
Include data members, constructors, and concrete methods to provide common state and default behaviour.
Step 4
Derive concrete classes
Create derived classes that override every pure virtual function so they become instantiable.
Step 5
Use through base pointers
Store derived objects behind an abstract base pointer or reference to call the overridden methods polymorphically.
What Interviewer Expects
- Definition of a pure virtual function with = 0 syntax
- That an abstract class cannot be instantiated
- Difference between abstract classes and interfaces
- That abstract classes can still hold data and implemented methods
- How abstract classes enable polymorphism
Common Mistakes
- Trying to create an object of an abstract class directly
- Thinking an abstract class can only have pure virtual functions
- Forgetting to override every pure virtual function in a derived class
- Confusing abstract classes with fully abstract interfaces
Best Answer (HR Friendly)
“An abstract class is a template that lists what its subclasses must be able to do without doing it itself. You can't use it on its own; you build specific versions from it that fill in the required details.”
Code Example
#include <iostream>
using namespace std;
class Shape {
public:
virtual double area() const = 0; // pure virtual -> abstract
virtual ~Shape() = default;
};
class Circle : public Shape {
double r;
public:
Circle(double radius) : r(radius) {}
double area() const override { return 3.14159 * r * r; }
};
int main() {
// Shape s; // ERROR: cannot instantiate abstract class
Shape* s = new Circle(2.0);
cout << "Area: " << s->area() << "\n";
delete s;
}Follow-up Questions
- What is a pure virtual function and how is it declared?
- Can an abstract class have a constructor?
- What is the difference between an abstract class and an interface in C++?
- What happens if a derived class does not override all pure virtual functions?
- Can a pure virtual function have a definition in C++?
MCQ Practice
1. What makes a C++ class abstract?
A class is abstract if it declares at least one pure virtual function ('= 0').
2. What happens if you try to instantiate an abstract class?
The compiler rejects instantiation of an abstract class outright.
3. If a derived class overrides only some pure virtual functions, it is:
It remains abstract until every inherited pure virtual function is overridden.
Flash Cards
How is a pure virtual function declared? — With '= 0' after the function signature, e.g. 'virtual void draw() = 0;'.
Can an abstract class be instantiated? — No. It can only be used as a base class; you instantiate its concrete derived classes.
Can an abstract class contain implemented methods? — Yes. It can have data members, constructors, and fully defined non-virtual or virtual methods.
Can a pure virtual function have a body? — Yes, it can be defined out of line, but the class stays abstract and derived classes must still override it.