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":
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:
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(); explicitlyAbstract vs concrete
| Abstract class | Concrete class | |
|---|---|---|
| Has a pure virtual? | Yes (≥1) | No |
| Can instantiate? | No | Yes |
| Role | Base / interface | Usable type |
Derivation chains
If a derived class overrides only some inherited pure virtuals, it is still abstract:
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 concreteWhen 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
= 0on a non-virtual function (it must bevirtual). - 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.
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++?
= 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++?
= 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++?
Does one pure virtual function make the whole class abstract in C++?
= 0 is enough to prevent creating objects of the class.