Abstract Classes in C++
In this lesson: Create abstract classes and interfaces in C++ with pure virtual functions (= 0) — examples with output. In Hindi & English.
What is an abstract class?
An abstract class is a class you cannot make objects of directly — it exists only to be inherited. It sets out a common interface (what derived classes must do) by declaring one or more pure virtual functions, and may also supply shared code.
Pure virtual functions
A pure virtual function is a virtual function with no body in the base, marked with = 0. It says "every concrete subclass must provide this":
class Shape {
public:
virtual double area() = 0; // pure virtual → Shape is abstract
virtual ~Shape() {} // virtual destructor
};The single = 0 makes Shape abstract. For the syntax details of pure virtual, see the pure virtual & abstract syntax page.
You cannot instantiate it
// Shape s; // ERROR: cannot create an abstract object Shape *p; // OK: a pointer to Shape is fine
You can still use Shape* pointers or references — they point to concrete subclasses.
A worked example
#include <iostream>
using namespace std;
class Shape {
public:
virtual double area() = 0; // pure virtual
virtual ~Shape() {}
};
class Circle : public Shape {
double r;
public:
Circle(double x) : r(x) {}
double area() override { return 3.14159 * r * r; }
};
class Rectangle : public Shape {
double w, h;
public:
Rectangle(double a, double b) : w(a), h(b) {}
double area() override { return w * h; }
};
int main() {
Shape *shapes[2] = { new Circle(2), new Rectangle(3, 4) };
for (Shape *s : shapes) cout << s->area() << "\n";
return 0;
}12.5664
12
Each shape computes its own area, called uniformly through Shape*.
Abstract classes as interfaces
An interface in C++ is just an abstract class where all functions are pure virtual and there is no data. It defines a pure contract:
class Drawable {
public:
virtual void draw() = 0;
virtual ~Drawable() {}
}; // any class implementing draw() is "Drawable"Key rules
- A class with any pure virtual function is abstract.
- An abstract class cannot be instantiated, but pointers/references to it can.
- A derived class stays abstract until it overrides all pure virtuals.
- Always give an abstract base a virtual destructor.
Common mistakes
- Trying to create an object of an abstract class.
- Leaving a pure virtual unimplemented in a class you meant to be concrete.
- Forgetting the virtual destructor in the abstract base.
- Confusing an abstract class (may have data/implemented methods) with a pure interface.
Define an abstract Animal with a pure virtual sound(). Derive Dog and Cow, implement sound() in each, and call them through an array of Animal*.
Summary
- An abstract class has at least one pure virtual function.
- Pure virtual is written
= 0and has no base body. - You cannot instantiate an abstract class, only point to it.
- An interface is an abstract class with all-pure-virtual and no data.
- A subclass must override every pure virtual to become concrete.
Frequently Asked Questions
What is an abstract class in C++?
What is a pure virtual function in C++?
= 0, such as virtual double area() = 0;. It forces every concrete derived class to provide its own version, and its presence makes the class abstract.