0
0
C++programming~10 mins

Base and derived classes in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Base and derived classes
Define Base Class
Define Derived Class
Create Derived Object
Call Derived Method
Call Base Method (inherited)
Use Derived and Base Features
First, we define a base class, then a derived class inherits from it. We create an object of the derived class and use both its own and inherited features.
Execution Sample
C++
#include <iostream>

class Base {
public:
  void greet() { std::cout << "Hello from Base\n"; }
};

class Derived : public Base {
public:
  void greet() { std::cout << "Hello from Derived\n"; }
};

int main() {
  Derived d;
  d.greet();
  d.Base::greet();
  return 0;
}
This code shows a base class and a derived class both with a greet method. The derived class object calls its own greet and the base class greet.
Execution Table
StepActionEvaluationOutput
1Define Base class with greet()Base::greet() ready
2Define Derived class inheriting Base, override greet()Derived::greet() ready
3Create Derived object dObject d created
4Call d.greet()Calls Derived::greet()Hello from Derived
5Call d.Base::greet()Calls Base::greet() explicitlyHello from Base
6Program endsNo more actions
💡 Program ends after calling both greet methods on Derived object
Variable Tracker
VariableStartAfter CreationAfter CallsFinal
d (Derived object)undefinedCreatedUsed to call greet methodsExists until program ends
Key Moments - 2 Insights
Why does d.greet() call Derived's greet, not Base's?
Because Derived overrides greet(), so calling d.greet() uses Derived's version (see execution_table step 4).
How can we call Base's greet() from Derived object?
By specifying d.Base::greet(), we tell the program to use Base's greet explicitly (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at step 4 when d.greet() is called?
AHello from Base
BHello from Derived
CNo output
DCompilation error
💡 Hint
Check the Output column at step 4 in the execution_table
At which step does the program explicitly call the Base class greet() method?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look for the action mentioning d.Base::greet() in the execution_table
If Derived did not override greet(), what would d.greet() print?
AHello from Derived
BNo output
CHello from Base
DCompilation error
💡 Hint
Think about inheritance and method overriding shown in the execution_table steps 4 and 5
Concept Snapshot
Base and derived classes:
- Define base class with methods
- Define derived class inheriting base
- Derived can override base methods
- Derived object uses overridden methods by default
- Use Base::method() to call base version
- Supports code reuse and extension
Full Transcript
This example shows how a derived class inherits from a base class in C++. The base class has a greet method. The derived class overrides this greet method. When we create an object of the derived class and call greet, it uses the derived version. We can also call the base class greet explicitly using the scope operator. This demonstrates basic inheritance and method overriding in C++.