Bird
Raised Fist0
C++programming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Method overriding
Define Base Class with Method
↓
Define Derived Class with Same Method
↓
Create Derived Object
↓
Call Method on Derived Object
↓
Derived Method Runs, Overrides Base
↓
End
Method overriding happens when a derived class has a method with the same name as its base class, so calling it on a derived object runs the new method instead of the base one.
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();
}
This code defines a base class and a derived class that both have a greet method. Calling greet on the derived object runs the derived version.
Execution Table
StepActionClassMethod CalledOutput
1Create object dDerivedNone
2Call d.greet()Derivedgreet()Hello from Derived
3End of mainNoneNone
💡 Program ends after calling the overridden greet method on Derived object.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
dundefinedDerived object createdNo changeDerived object exists
Key Moments - 2 Insights
Why does calling d.greet() run Derived's greet, not Base's?
Because d is an object of Derived, and Derived has its own greet method that overrides Base's. See execution_table step 2 where Derived's greet runs.
What if Derived did not have greet method?
Then calling d.greet() would run Base's greet method, since Derived inherits it. This is not shown in the table but is the default behavior.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed when d.greet() is called?
ANo output
BHello from Derived
CHello from Base
DCompilation error
💡 Hint
Check execution_table row 2 under Output column.
At which step is the Derived object created?
AStep 1
BStep 2
CStep 3
DBefore Step 1
💡 Hint
Look at execution_table row 1 Action column.
If Derived did not override greet(), what would happen when d.greet() is called?
ANo method runs
BDerived's greet runs
CBase's greet runs
DProgram crashes
💡 Hint
Refer to key_moments second question about default behavior.
Concept Snapshot
Method overriding means a derived class has a method with the same name as its base class.
When calling that method on a derived object, the derived version runs.
This allows customizing behavior in subclasses.
In C++, overriding requires same method signature.
Without overriding, base class method runs.
Full Transcript
Method overriding in C++ happens when a derived class defines a method with the same name as one in its base class. When you create an object of the derived class and call that method, the derived class's version runs instead of the base class's. In the example, the base class has a greet method that prints "Hello from Base". The derived class also has a greet method that prints "Hello from Derived". When we create a Derived object d and call d.greet(), it prints "Hello from Derived". This shows the derived method overrides the base method. If the derived class did not have greet, calling d.greet() would run the base class's greet method. This is how C++ supports customizing behavior in subclasses by overriding methods.

Practice

(1/5)
1. What is the main purpose of method overriding in C++?
easy
A. To create a new method with a different name in the child class
B. To allow a child class to provide a specific implementation of a method already defined in its parent class
C. To prevent a method from being used in the child class
D. To copy all methods from the parent class to the child class without changes

Solution

  1. Step 1: Understand method overriding concept

    Method overriding allows a child class to change how a method works that it inherits from its parent.
  2. Step 2: Identify the correct purpose

    The child class provides its own version of the method, customizing behavior while keeping the same method name.
  3. Final Answer:

    To allow a child class to provide a specific implementation of a method already defined in its parent class -> Option B
  4. Quick Check:

    Method overriding = child changes parent method [OK]
Hint: Overriding means child changes parent's method behavior [OK]
Common Mistakes:
  • Confusing overriding with method overloading
  • Thinking overriding creates new method names
  • Believing overriding copies methods without changes
2. Which of the following is the correct way to declare a method for overriding in C++?
easy
A. override void display();
B. void display() override;
C. void virtual display();
D. virtual void display();

Solution

  1. Step 1: Recall syntax for virtual methods

    In C++, the parent class method must be marked with virtual to allow overriding.
  2. Step 2: Check each option

    virtual void display(); correctly uses virtual void display();. void display() override; uses override incorrectly in declaration, it is used in child method definition. Options C and D have wrong syntax.
  3. Final Answer:

    virtual void display(); -> Option D
  4. Quick Check:

    Parent method uses virtual keyword [OK]
Hint: Parent method must be virtual for overriding [OK]
Common Mistakes:
  • Placing override keyword in parent method declaration
  • Using wrong order of keywords
  • Omitting virtual keyword in parent
3. What will be the output of the following C++ code?
class Base {
public:
    virtual void show() { std::cout << "Base" << std::endl; }
};
class Derived : public Base {
public:
    void show() override { std::cout << "Derived" << std::endl; }
};
int main() {
    Base* ptr = new Derived();
    ptr->show();
    delete ptr;
    return 0;
}
medium
A. Derived
B. Runtime error
C. Compilation error
D. Base

Solution

  1. Step 1: Understand virtual function behavior

    The pointer is of type Base* but points to Derived object. Because show() is virtual, the Derived version is called.
  2. Step 2: Trace the call ptr->show()

    At runtime, the program calls Derived::show(), printing "Derived".
  3. Final Answer:

    Derived -> Option A
  4. Quick Check:

    Virtual method calls child's version [OK]
Hint: Virtual calls use actual object method, not pointer type [OK]
Common Mistakes:
  • Expecting Base output due to pointer type
  • Confusing override keyword effect
  • Ignoring virtual keyword
4. Identify the error in the following code snippet related to method overriding:
class Parent {
public:
    void greet() { std::cout << "Hello from Parent" << std::endl; }
};
class Child : public Parent {
public:
    void greet() override { std::cout << "Hello from Child" << std::endl; }
};
medium
A. Parent class method greet() should be marked virtual
B. Child class method greet() should be marked virtual
C. override keyword is not allowed in C++
D. No error, code is correct

Solution

  1. Step 1: Check parent method declaration

    For overriding to work properly, the parent method must be declared virtual. Here, it is not.
  2. Step 2: Analyze override keyword usage

    The child method uses override, which requires the parent method to be virtual. Since parent method is not virtual, this causes a compilation error.
  3. Final Answer:

    Parent class method greet() should be marked virtual -> Option A
  4. Quick Check:

    Parent method must be virtual for override [OK]
Hint: Parent method must be virtual if child uses override [OK]
Common Mistakes:
  • Thinking child method must be virtual
  • Ignoring need for virtual in parent
  • Believing override keyword is optional
5. Given the classes below, what will be the output when ptr->display(); is called?
class Animal {
public:
    virtual void display() { std::cout << "Animal" << std::endl; }
};
class Dog : public Animal {
public:
    void display() override { std::cout << "Dog" << std::endl; }
};
class Puppy : public Dog {
public:
    void display() override { std::cout << "Puppy" << std::endl; }
};

int main() {
    Animal* ptr = new Puppy();
    ptr->display();
    delete ptr;
    return 0;
}
hard
A. Animal
B. Dog
C. Puppy
D. Compilation error due to multiple overrides

Solution

  1. Step 1: Understand multi-level overriding

    Each class overrides display() with its own version. The pointer is of type Animal* but points to a Puppy object.
  2. Step 2: Determine which method is called

    Because display() is virtual, the most derived override (Puppy::display) is called at runtime.
  3. Final Answer:

    Puppy -> Option C
  4. Quick Check:

    Virtual calls deepest override in inheritance chain [OK]
Hint: Virtual calls deepest override in inheritance chain [OK]
Common Mistakes:
  • Expecting Dog or Animal output due to pointer type
  • Thinking multiple overrides cause errors
  • Ignoring virtual keyword effect
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard