100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is a Pure Virtual Function?

Pure virtual functions explained — the = 0 syntax, abstract classes, and required subclass overrides, with a Java abstract-method comparison.

mediumQ135 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

A pure virtual function is a C++ member function declared with '= 0' that has no implementation in the class that declares it, forcing every concrete (non-abstract) derived class to provide its own implementation before it can be instantiated.

Declaring even one pure virtual function makes the entire class abstract — it cannot be instantiated directly, only used as a base type via pointers or references. A derived class becomes concrete only once it overrides all inherited pure virtual functions; if it leaves any unimplemented, it remains abstract too. A pure virtual function still participates in the vtable, occupying a slot (often pointing to a null or a special abort stub), and can even have a body defined out-of-line that a derived class explicitly calls via BaseClass::method(), which is a lesser-known but legal pattern for providing a default a subclass can opt into. Java and C# express the same idea with 'abstract' methods on abstract classes, and interfaces, without a separate '=0' syntax, but the underlying contract-only, no-instantiation-without-override semantics are identical.

  • Enforces a mandatory contract that all concrete subclasses must fulfill
  • Prevents accidental instantiation of an incomplete base type
  • Documents intent clearly: this base class defines an interface, not usable behavior
  • Enables safe polymorphic use through base pointers while forbidding misuse of the base type itself

AI Mentor Explanation

A national cricket board mandates that every certified “bowler” role must define an actual bowling action — the certification itself provides no default action, it only lists the requirement. You cannot register someone as “just a certified bowler” with no technique; they must specify pace, spin, or whatever style actually gets executed on the field before the certificate is valid. A pure virtual function works the same way: the base class declares the requirement but supplies no implementation, and no concrete object can exist until a real subclass fills it in.

Step-by-Step Explanation

  1. Step 1

    Declare with = 0

    In C++, mark a member function as pure virtual with virtual returnType method(...) = 0; and no default body in the class.

  2. Step 2

    Class becomes abstract

    Any class containing at least one pure virtual function cannot be instantiated directly.

  3. Step 3

    Derived classes must implement it

    A subclass becomes concrete (instantiable) only once it overrides every inherited pure virtual function.

  4. Step 4

    Use via base pointer/reference

    Concrete derived objects are created and then used polymorphically through pointers/references typed as the abstract base.

What Interviewer Expects

  • Correct C++ syntax and semantics: = 0, no default implementation required in the base
  • Understanding that it makes the whole class abstract, not just that one method
  • Awareness that Java/C# express the equivalent idea with “abstract” methods/classes and interfaces
  • Knowing a pure virtual function can still have an out-of-line body a subclass may explicitly invoke

Common Mistakes

  • Believing a pure virtual function can never have any implementation at all in C++
  • Forgetting that a single pure virtual function makes the entire class non-instantiable
  • Confusing “pure virtual” with simply “virtual” (a virtual function alone can have a default body and still be instantiated)
  • Assuming Java has identical "= 0" syntax rather than the abstract keyword

Best Answer (HR Friendly)

A pure virtual function is a method a base class declares but deliberately leaves unimplemented, forcing every concrete subclass to provide its own version before it can be created as an object. It's how you say “every real implementation of this interface must define this behavior” — the base class becomes abstract, so you can only use it through pointers to fully implemented subclasses, never instantiate it directly.

Code Example

Pure virtual function equivalent (Java abstract method)
abstract class Shape {
    // Equivalent to C++ "virtual double area() = 0;"
    abstract double area();
}

class Circle extends Shape {
    double radius;
    Circle(double radius) { this.radius = radius; }
    @Override
    double area() { return Math.PI * radius * radius; } // required implementation
}

// Shape s = new Shape(); // compile error: cannot instantiate abstract class
Shape s = new Circle(2.0); // OK: Circle supplies the required implementation
System.out.println(s.area());

Follow-up Questions

  • Can a pure virtual function have a default body a subclass can still call explicitly?
  • What happens if a derived class fails to implement one of several inherited pure virtual functions?
  • How does a pure virtual destructor differ from a pure virtual regular method?
  • How is Java's “abstract” keyword different from and similar to C++'s "= 0" syntax?

MCQ Practice

1. In C++, declaring one pure virtual function in a class makes that class?

A class with at least one pure virtual function becomes abstract and cannot be instantiated on its own.

2. A derived class becomes instantiable (concrete) when?

A derived class is only concrete once all inherited pure virtual functions have been overridden with real implementations.

3. Java's closest equivalent to a C++ pure virtual function is?

Java expresses the same “declared, unimplemented, mandatory override” contract via abstract methods and interfaces.

Flash Cards

Pure virtual function in one line?A C++ method declared with = 0, no implementation, forcing subclasses to override it.

Effect on the class?The whole class becomes abstract and cannot be instantiated directly.

Java equivalent?An abstract method on an abstract class, or a method on an interface.

Can it have a body?Yes — an out-of-line default body a subclass may explicitly call via Base::method().

1 / 4

Continue Learning