0
0
C++programming~10 mins

Interface-like behavior in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Interface-like behavior
Define Abstract Class
Declare Pure Virtual Functions
Create Derived Classes
Override Pure Virtual Functions
Use Base Class Pointer/Reference
Call Overridden Functions at Runtime
This flow shows how an abstract class with pure virtual functions acts like an interface, forcing derived classes to implement specific methods, and how polymorphism allows calling these methods through base class pointers.
Execution Sample
C++
#include <iostream>

class IShape {
public:
  virtual void draw() = 0;
};

class Circle : public IShape {
public:
  void draw() override { std::cout << "Drawing Circle\n"; }
};
Defines an interface-like abstract class IShape with a pure virtual draw method, and a Circle class that implements draw.
Execution Table
StepActionEvaluationResult
1Create Circle objectCircle c;Object c created
2Assign base pointerIShape* shape = &c;Pointer shape points to c
3Call draw via base pointershape->draw();Calls Circle::draw(), prints 'Drawing Circle'
4Attempt to instantiate IShapeIShape i;Error: cannot instantiate abstract class
5ExitEnd of programProgram ends normally
💡 Cannot instantiate abstract class IShape; polymorphic call calls derived draw()
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
c (Circle object)nonecreatedcreatedcreatedcreated
shape (IShape*)nullptrnullptrpoints to cpoints to cpoints to c
Key Moments - 3 Insights
Why can't we create an object of the abstract class IShape?
Because IShape has a pure virtual function (draw), it is abstract and cannot be instantiated. See execution_table step 4 where trying to create IShape causes an error.
How does calling draw() on shape call Circle's draw()?
The base pointer shape points to a Circle object. The virtual function mechanism calls the overridden Circle::draw() at runtime, as shown in execution_table step 3.
What happens if Circle does not override draw()?
Circle would also be abstract and cannot be instantiated. The compiler enforces overriding pure virtual functions.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does step 3's call to shape->draw() output?
ADrawing Circle
BDrawing Shape
CCompilation error
DNo output
💡 Hint
See execution_table row 3 under Result column
At which step does the program show an error for trying to instantiate an abstract class?
AStep 1
BStep 4
CStep 2
DStep 5
💡 Hint
Check execution_table row 4 under Evaluation and Result
If we remove the override of draw() in Circle, what changes in variable_tracker?
Ashape points to base class instead
Bshape pointer becomes nullptr
CCircle object cannot be created, so no 'created' state
DNo change in variable_tracker
💡 Hint
Refer to key_moments about overriding pure virtual functions
Concept Snapshot
Interface-like behavior in C++:
- Use abstract class with pure virtual functions (e.g., virtual void f() = 0;)
- Derived classes must override these functions
- Cannot instantiate abstract class
- Use base class pointers to call overridden methods polymorphically
- Enables interface-like design without explicit 'interface' keyword
Full Transcript
This example shows how C++ uses abstract classes with pure virtual functions to create interface-like behavior. The abstract class IShape declares a pure virtual function draw(), making it impossible to create IShape objects directly. The Circle class inherits from IShape and overrides draw(), providing its own implementation. We create a Circle object and assign its address to a base class pointer of type IShape*. When we call draw() through this pointer, the Circle's version runs because of virtual function dispatch. Attempting to instantiate IShape directly causes a compile-time error. This pattern enforces that derived classes implement required methods, mimicking interfaces in other languages.