Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
📘 Lesson  ·  Lesson 72

Pure Virtual & Abstract Class

Written and reviewed by · Senior IT Faculty · 15+ years’ experience

Quick recap

A pure virtual function is a virtual function with no base implementation, and a class holding one is abstract. This page is a compact syntax reference; for the concept, interfaces, and a full worked example, see the main abstract classes lesson.

The = 0 syntax

Write the virtual function, then = 0 instead of a body. The = 0 is the "pure specifier":

C++
class Base {
public:
    virtual void run() = 0;    // pure virtual → Base is abstract
    virtual ~Base() {}
};

Pure virtual with a body

Unusually, a pure virtual can still have a definition, which derived classes may call explicitly. The class remains abstract:

C++
class Base {
public:
    virtual void log() = 0;    // still pure/abstract
};
void Base::log() {             // optional shared body
    cout << "[Base log]\n";
}
// Derived can call Base::log(); explicitly

Abstract vs concrete

Abstract classConcrete class
Has a pure virtual?Yes (≥1)No
Can instantiate?NoYes
RoleBase / interfaceUsable type

Derivation chains

If a derived class overrides only some inherited pure virtuals, it is still abstract:

C++
class A { public: virtual void f() = 0; virtual void g() = 0; };
class B : public A { public: void f() override {} };  // g() still pure → B abstract
class C : public B { public: void g() override {} };  // now concrete

When a class is abstract

  • It declares a pure virtual function (= 0), or
  • It inherits one and does not override it.

Either way, objects of that class cannot be created — only pointers/references. This underpins polymorphism and virtual functions.

Common mistakes

  • Putting = 0 on a non-virtual function (it must be virtual).
  • Assuming a partly-overridden derived class is concrete.
  • Trying to instantiate a class that still has an unimplemented pure virtual.
  • Forgetting the virtual destructor in the abstract base.
🏋️ Practice

Write an abstract Payment with pure virtual pay(double). Derive Card and Cash, implement pay in each, and call them through a Payment*.

Summary

  • Pure virtual syntax is virtual T f() = 0;.
  • A pure virtual may optionally have a body defined outside the class.
  • Abstract = has a pure virtual; concrete = fully implemented.
  • A partly-overridden subclass stays abstract.
  • For concept and examples, see the main abstract classes lesson.

Frequently Asked Questions

How do you write a pure virtual function in C++?
You write a virtual function and assign = 0 in place of a body, such as virtual void draw() = 0;. The = 0 is called the pure specifier; it marks the function as having no base implementation and makes the enclosing class abstract.
Can a pure virtual function have a body in C++?
Yes, unusually, a pure virtual function may be given a definition outside the class, even though it is declared with = 0. The class stays abstract, but derived classes can call that shared implementation explicitly with the base-class scope, which is occasionally useful for common default logic.
What is the difference between an abstract class and a concrete class in C++?
A concrete class has implementations for all its functions and can be instantiated. An abstract class has at least one unimplemented pure virtual function, so it cannot be instantiated and serves only as a base that derived classes complete.
Does one pure virtual function make the whole class abstract in C++?
Yes. A class becomes abstract the moment it has even a single pure virtual function, whether declared there or inherited without being overridden. That one = 0 is enough to prevent creating objects of the class.
How do you make a derived class concrete in C++?
A derived class becomes concrete by overriding every pure virtual function it inherits, providing a real implementation for each. If it overrides some but not all of them, it remains abstract and still cannot be instantiated.

त्वरित सार

Pure virtual function एक virtual function है जिसका base implementation नहीं, और इसे रखने वाली class abstract है। यह पृष्ठ एक संक्षिप्त syntax reference है; concept, interfaces, और पूरे चलते उदाहरण के लिए, मुख्य abstract classes lesson देखें।

= 0 syntax

Virtual function लिखें, फिर body की जगह = 0= 0 "pure specifier" है:

C++
class Base {
public:
    virtual void run() = 0;    // pure virtual → Base abstract hai
    virtual ~Base() {}
};

Body के साथ pure virtual

असामान्य रूप से, pure virtual की फिर भी एक definition हो सकती है, जिसे derived classes स्पष्ट रूप से call कर सकती हैं। Class abstract रहती है:

C++
class Base {
public:
    virtual void log() = 0;    // phir bhi pure/abstract
};
void Base::log() {             // optional shared body
    cout << "[Base log]\n";
}
// Derived, Base::log(); explicitly call kar sakti hai

Abstract बनाम concrete

Abstract classConcrete class
Pure virtual है?हाँ (≥1)नहीं
Instantiate कर सकते?नहींहाँ
भूमिकाBase / interfaceउपयोगी type

Derivation chains

अगर derived class केवल कुछ inherited pure virtuals override करे, यह अब भी abstract है:

C++
class A { public: virtual void f() = 0; virtual void g() = 0; };
class B : public A { public: void f() override {} };  // g() abhi pure → B abstract
class C : public B { public: void g() override {} };  // ab concrete

Class कब abstract है

  • यह एक pure virtual function घोषित करती है (= 0), या
  • यह एक inherit करती है और override नहीं करती।

किसी भी तरह, उस class के objects नहीं बन सकते — केवल pointers/references। यह polymorphism और virtual functions का आधार है।

आम गलतियाँ

  • Non-virtual function पर = 0 लगाना (यह virtual होना चाहिए)।
  • आंशिक-override derived class को concrete मानना।
  • ऐसी class instantiate करने की कोशिश जिसमें अब भी बिना implementation pure virtual हो।
  • Abstract base में virtual destructor भूलना।
🏋️ अभ्यास

एक abstract Payment लिखें जिसमें pure virtual pay(double) हो। Card और Cash derive करें, हर एक में pay implement करें, और Payment* के ज़रिए call करें।

सारांश

  • Pure virtual syntax virtual T f() = 0; है।
  • Pure virtual की वैकल्पिक रूप से class के बाहर एक body परिभाषित हो सकती है।
  • Abstract = pure virtual है; concrete = पूरी तरह implemented।
  • आंशिक-override subclass abstract रहती है।
  • Concept और उदाहरणों के लिए, मुख्य abstract classes lesson देखें।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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