0
0
C++programming~10 mins

Abstract classes in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Abstract classes
Define abstract class with pure virtual function
Cannot create object of abstract class
Create derived class implementing pure virtual function
Create object of derived class
Call implemented function via base pointer/reference
Run program
Shows how an abstract class defines a contract with pure virtual functions, which derived classes must implement 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 class Shape with a pure virtual draw method, and a Circle class that implements draw.
Execution Table
StepActionEvaluationResult
1Define class Shape with pure virtual draw()Shape is abstractCannot instantiate Shape
2Define class Circle derived from ShapeCircle overrides draw()Circle is concrete
3Try to create Shape objectErrorCompilation error: cannot instantiate abstract class
4Create Circle objectAllowedCircle object created
5Call draw() on Circle objectCalls Circle::draw()Circle's draw() runs
6Assign Circle object to Shape pointerAllowedPointer to base holds derived object
7Call draw() via Shape pointerCalls Circle::draw() (polymorphism)Circle's draw() runs
8End of programProgram ends successfully
💡 Program ends after calling draw() on Circle object via base pointer
Variable Tracker
VariableStartAfter Step 4After Step 6Final
Shape objectNone (abstract)N/A (cannot create)N/AN/A
Circle objectNoneCreatedCreatedExists
Shape pointerNoneNonePoints to Circle objectPoints to Circle object
Key Moments - 3 Insights
Why can't we create an object of the abstract class Shape?
Because Shape has a pure virtual function (draw), it is abstract and incomplete. See execution_table step 3 where trying to create Shape causes a compilation error.
How does calling draw() via a Shape pointer call the Circle's draw()?
Because draw() is virtual, the call is resolved at runtime to the derived class's implementation. See execution_table step 7 where Shape pointer calls Circle::draw().
What happens if Circle does not implement draw()?
Circle would also be abstract and cannot be instantiated. The compiler enforces implementation of all pure virtual functions.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 3?
ACompilation error because Shape is abstract
BAn object of Shape is successfully created
CCircle object is created
Ddraw() is called on Shape object
💡 Hint
Check the 'Result' column at step 3 in execution_table
At which step does polymorphism allow calling Circle's draw() via a Shape pointer?
AStep 4
BStep 7
CStep 5
DStep 3
💡 Hint
Look for 'Call draw() via Shape pointer' in execution_table
If Circle did not implement draw(), what would happen?
AShape would no longer be abstract
BCircle would be concrete and instantiable
CCircle would be abstract and cannot be instantiated
DProgram would run without errors
💡 Hint
Refer to key_moments about implementation of pure virtual functions
Concept Snapshot
Abstract classes have at least one pure virtual function (e.g., virtual void f() = 0;).
They cannot be instantiated directly.
Derived classes must implement all pure virtual functions.
Objects of derived classes can be created and used polymorphically via base pointers.
This enforces a contract for subclasses to follow.
Full Transcript
An abstract class in C++ is a class that has at least one pure virtual function, declared with = 0. This means the class cannot be instantiated directly because it is incomplete. Derived classes must provide implementations for all pure virtual functions to become concrete. Once implemented, objects of the derived class can be created. Using a base class pointer to refer to a derived object allows polymorphic calls to the overridden functions. This mechanism enforces a design contract and enables flexible code reuse and extension.