Abstract Classes and Pure Virtual Functions
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
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.Why can you not create an object of an abstract class in C++?
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 in C++?
Abstract class क्या है?
Abstract class ऐसी class है जिसके objects आप सीधे नहीं बना सकते — यह केवल inherit होने को होती है। यह एक या ज़्यादा pure virtual functions घोषित करके एक साझा interface (derived classes को क्या करना है) तय करती है, और साझा code भी दे सकती है।
Pure virtual functions
Pure virtual function एक virtual function है जिसका base में कोई body नहीं, = 0 से चिह्नित। यह कहता है "हर concrete subclass को यह देना होगा":
class Shape {
public:
virtual double area() = 0; // pure virtual → Shape abstract hai
virtual ~Shape() {} // virtual destructor
};अकेला = 0 Shape को abstract बनाता है। Pure virtual के syntax विवरण के लिए, pure virtual & abstract syntax page देखें।
आप इसे instantiate नहीं कर सकते
// Shape s; // ERROR: abstract object nahi bana sakte Shape *p; // OK: Shape ka pointer theek hai
आप फिर भी Shape* pointers या references इस्तेमाल कर सकते हैं — वे concrete subclasses पर इशारा करते हैं।
एक चलता उदाहरण
#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
हर shape अपना area गिनता है, Shape* के ज़रिए एक समान call किया गया।
Interfaces के रूप में abstract classes
C++ में interface बस एक abstract class है जहाँ सभी functions pure virtual हों और कोई data न हो। यह एक शुद्ध अनुबंध परिभाषित करता है:
class Drawable {
public:
virtual void draw() = 0;
virtual ~Drawable() {}
}; // draw() implement karne wali koi bhi class "Drawable" haiमुख्य नियम
- किसी भी pure virtual function वाली class abstract है।
- Abstract class instantiate नहीं हो सकती, पर इसके pointers/references हो सकते हैं।
- Derived class तब तक abstract रहती है जब तक वह सभी pure virtuals override न करे।
- Abstract base को हमेशा virtual destructor दें।
आम गलतियाँ
- Abstract class का object बनाने की कोशिश।
- जिस class को concrete बनाना था उसमें pure virtual बिना implementation छोड़ना।
- Abstract base में virtual destructor भूलना।
- Abstract class (data/implemented methods हो सकते) को शुद्ध interface से भ्रमित करना।
एक abstract Animal परिभाषित करें जिसमें pure virtual sound() हो। Dog और Cow derive करें, हर एक में sound() implement करें, और Animal* के array के ज़रिए call करें।
सारांश
- Abstract class में कम से कम एक pure virtual function होता है।
- Pure virtual
= 0लिखा जाता है और इसका base body नहीं होता। - आप abstract class instantiate नहीं कर सकते, केवल इस पर इशारा कर सकते हैं।
- Interface एक abstract class है जिसमें सब pure-virtual और कोई data न हो।
- Subclass को concrete बनने को हर pure virtual override करना होगा।