Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
🟣 OOP  ·  Lesson 49

Abstract Classes and Pure Virtual Functions

Written and reviewed by · 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":

C++
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

C++
// 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

C++
#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;
}
Output:
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:

C++
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.
🏋️ Practice

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 = 0 and 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++?
An abstract class is a class that has at least one pure virtual function and therefore cannot be instantiated on its own. It exists to be a base class, defining a common interface that derived classes must implement, while possibly providing some shared behaviour too.
What is a pure virtual function in C++?
A pure virtual function is a virtual function with no implementation in the base class, declared by writing = 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++?
Because an abstract class has at least one function with no implementation, the compiler cannot create a complete, usable object from it. You can still use pointers or references to the abstract type, which point to objects of concrete derived classes.
What is the difference between an abstract class and an interface in C++?
C++ has no separate interface keyword; an interface is simply an abstract class where every function is pure virtual and there is no data. A general abstract class may also include implemented functions and data members alongside its pure virtual ones.
What happens if a derived class does not override all pure virtual functions in C++?
If a derived class leaves any inherited pure virtual function without an implementation, that derived class is itself abstract and cannot be instantiated either. Only when every pure virtual function is implemented does a class become concrete and usable.

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 को यह देना होगा":

C++
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 नहीं कर सकते

C++
// Shape s;        // ERROR: abstract object nahi bana sakte
Shape *p;           // OK: Shape ka pointer theek hai

आप फिर भी Shape* pointers या references इस्तेमाल कर सकते हैं — वे concrete subclasses पर इशारा करते हैं।

एक चलता उदाहरण

C++
#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;
}
Output:
12.5664
12

हर shape अपना area गिनता है, Shape* के ज़रिए एक समान call किया गया।

Interfaces के रूप में abstract classes

C++ में interface बस एक abstract class है जहाँ सभी functions pure virtual हों और कोई data न हो। यह एक शुद्ध अनुबंध परिभाषित करता है:

C++
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 करना होगा।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.