0
0
C++programming~10 mins

Pure virtual functions in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pure virtual functions
Declare base class with pure virtual function
Cannot instantiate base class
Derive subclass and override pure virtual function
Instantiate subclass object
Call overridden function via base pointer/reference
Execute subclass implementation
This flow shows how a pure virtual function makes a class abstract, requiring subclasses to provide their own implementation before objects can be created.
Execution Sample
C++
class Shape {
public:
  virtual void draw() = 0; // pure virtual
};

class Circle : public Shape {
public:
  void draw() override { /* draw circle */ }
};
Defines an abstract Shape class with a pure virtual draw function, then a Circle subclass that implements draw.
Execution Table
StepActionEvaluationResult
1Declare Shape with pure virtual draw()draw() = 0Shape is abstract, cannot create objects
2Try to create Shape objectShape s;Error: cannot instantiate abstract class
3Define Circle subclass overriding draw()void draw() overrideCircle is concrete, can create objects
4Create Circle objectCircle c;Object c created
5Call draw() via Shape pointerShape* p = &c; p->draw();Circle's draw() runs
6Program ends--
💡 Execution stops after Circle object is created and draw() is called; base class cannot be instantiated.
Variable Tracker
VariableStartAfter Step 4After Step 5Final
Shape objectnone (abstract)none (cannot create)nonenone
Circle object cnonecreatedexistsexists
Shape* pnonenonepoints to cpoints to c
Key Moments - 3 Insights
Why can't we create an object of the base class Shape?
Because Shape has a pure virtual function (draw() = 0), it is abstract. As shown in execution_table step 2, trying to create Shape causes an error.
What happens when we call draw() through a Shape pointer to a Circle object?
The Circle's overridden draw() runs, as shown in execution_table step 5, demonstrating polymorphism.
Does the base class provide any implementation for the pure virtual function?
No, the pure virtual function has no implementation in the base class, forcing subclasses to implement it, as seen in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 2 when trying to create a Shape object?
AShape object is created successfully
BShape object is created but draw() is not callable
CCompilation error because Shape is abstract
DProgram crashes at runtime
💡 Hint
See execution_table row for step 2 describing the error when instantiating Shape.
At which step does the Circle object get created?
AStep 3
BStep 4
CStep 1
DStep 5
💡 Hint
Check execution_table row for step 4 where Circle object c is created.
If Circle did not override draw(), what would happen when calling p->draw() at step 5?
ACompile-time error due to missing override
BBase class draw() runs
CCircle's draw() runs anyway
DProgram runs but does nothing
💡 Hint
Pure virtual functions require subclasses to override; see key_moments about subclass implementation.
Concept Snapshot
Pure virtual functions use syntax: virtual ReturnType func() = 0;
They make a class abstract (no objects allowed).
Derived classes must override them.
Allows polymorphism via base pointers.
Forces subclass to provide specific behavior.
Full Transcript
Pure virtual functions in C++ are declared by assigning 0 to a virtual function in a base class. This makes the class abstract, so you cannot create objects of it directly. Instead, you create subclasses that override the pure virtual function with their own implementation. Then you can create objects of these subclasses. When you call the function through a base class pointer or reference, the subclass's version runs. This is a key part of polymorphism in C++. The execution steps show declaring the abstract base class, failing to create its object, defining a subclass with the function implemented, creating its object, and calling the function through a base pointer.