Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 72

Pure Virtual Function in C++

In this lesson: Pure virtual function syntax in C++, abstract vs concrete classes and interfaces — with example. In Hindi & English.

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.
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

Next topicTemplates Your progress: 72 / 81 (89%) · Lesson 73 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free