0
0
C++programming~10 mins

Function overriding in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Function overriding
Base class defines function
Derived class defines function with same name
Create derived class object
Call function on derived object
Derived class function runs, overrides base
End
Function overriding happens when a derived class provides its own version of a function defined in its base class. When called on a derived object, the derived version runs.
Execution Sample
C++
#include <iostream>
class Base {
public:
  virtual void show() { std::cout << "Base show"; }
};
class Derived : public Base {
public:
  void show() override { std::cout << "Derived show"; }
};
int main() {
  Derived d;
  d.show();
}
This code shows a base class and a derived class both having a show() function. Calling show() on derived object runs derived version.
Execution Table
StepActionFunction CalledOutput
1Create Derived object dNone
2Call d.show()Derived::show()Derived show
3Program endsNone
💡 No more code to run, program ends
Variable Tracker
VariableStartAfter Step 1After Step 2Final
d (Derived object)NoneCreatedExistsExists
Key Moments - 2 Insights
Why does calling show() on derived object run Derived::show() and not Base::show()?
Because Derived class defines its own show() with the same name, it overrides Base's show(). See execution_table step 2 where Derived::show() runs.
What if Derived did not have show()?
Then calling d.show() would run Base::show() because Derived inherits it. Overriding only happens if Derived defines the function.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what function is called at step 2?
ABase::show()
BDerived::show()
CNo function called
DBoth Base::show() and Derived::show()
💡 Hint
Check the 'Function Called' column at step 2 in execution_table
At which step is the Derived object created?
AStep 2
BStep 3
CStep 1
DNo object created
💡 Hint
Look at the 'Action' column in execution_table for object creation
If Derived did not override show(), what would be the output at step 2?
ABase show
BDerived show
CNo output
DCompilation error
💡 Hint
Refer to key_moments explanation about overriding and inheritance
Concept Snapshot
Function overriding means a derived class has a function with the same name as in base class.
When called on derived object, derived function runs, hiding base version.
Overriding requires same function signature.
If derived does not override, base function runs.
Used for customizing behavior in derived classes.
Full Transcript
Function overriding in C++ happens when a derived class defines a function with the same name as one in its base class. When you create an object of the derived class and call that function, the derived class's version runs instead of the base class's. In the example, the base class has a show() function that prints "Base show". The derived class also has a show() function that prints "Derived show". When we create a derived object and call show(), it prints "Derived show". This is because the derived class's function overrides the base class's. If the derived class did not have its own show(), calling show() on the derived object would run the base class's version. This lets derived classes customize or replace behavior from base classes.