Method overriding in C++ - Time & Space Complexity
When we use method overriding, we replace a method in a child class with a new version.
We want to see how this affects the time it takes to run the program.
Analyze the time complexity of the following code snippet.
class Base {
public:
virtual void display() {
// simple print
std::cout << "Base display" << std::endl;
}
};
class Derived : public Base {
public:
void display() override {
// overridden print
std::cout << "Derived display" << std::endl;
}
};
void callDisplay(Base* obj) {
obj->display();
}
This code shows a base class with a method and a derived class that overrides it. We call the method through a base pointer.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the display() method once per object.
- How many times: Each callDisplay call triggers exactly one method call.
Each call to display runs a simple print statement, which takes about the same time regardless of input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 method calls, each simple print |
| 100 | 100 method calls, each simple print |
| 1000 | 1000 method calls, each simple print |
Pattern observation: The total work grows directly with the number of calls.
Time Complexity: O(n)
This means if you call the method n times, the total time grows in a straight line with n.
[X] Wrong: "Overriding a method makes the program slower because it adds extra work."
[OK] Correct: Overriding just changes which code runs, but each call still takes about the same time as before.
Understanding how method overriding affects time helps you explain object-oriented design choices clearly and confidently.
"What if the overridden method called another method with a loop inside? How would the time complexity change?"
Practice
method overriding in C++?Solution
Step 1: Understand method overriding concept
Method overriding allows a child class to change how a method works that it inherits from its parent.Step 2: Identify the correct purpose
The child class provides its own version of the method, customizing behavior while keeping the same method name.Final Answer:
To allow a child class to provide a specific implementation of a method already defined in its parent class -> Option BQuick Check:
Method overriding = child changes parent method [OK]
- Confusing overriding with method overloading
- Thinking overriding creates new method names
- Believing overriding copies methods without changes
Solution
Step 1: Recall syntax for virtual methods
In C++, the parent class method must be marked withvirtualto allow overriding.Step 2: Check each option
virtual void display(); correctly usesvirtual void display();. void display() override; usesoverrideincorrectly in declaration, it is used in child method definition. Options C and D have wrong syntax.Final Answer:
virtual void display(); -> Option DQuick Check:
Parent method uses virtual keyword [OK]
- Placing override keyword in parent method declaration
- Using wrong order of keywords
- Omitting virtual keyword in parent
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;
}Solution
Step 1: Understand virtual function behavior
The pointer is of type Base* but points to Derived object. Becauseshow()is virtual, the Derived version is called.Step 2: Trace the call ptr->show()
At runtime, the program calls Derived::show(), printing "Derived".Final Answer:
Derived -> Option AQuick Check:
Virtual method calls child's version [OK]
- Expecting Base output due to pointer type
- Confusing override keyword effect
- Ignoring virtual keyword
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; }
};Solution
Step 1: Check parent method declaration
For overriding to work properly, the parent method must be declaredvirtual. Here, it is not.Step 2: Analyze override keyword usage
The child method usesoverride, which requires the parent method to be virtual. Since parent method is not virtual, this causes a compilation error.Final Answer:
Parent class method greet() should be marked virtual -> Option AQuick Check:
Parent method must be virtual for override [OK]
- Thinking child method must be virtual
- Ignoring need for virtual in parent
- Believing override keyword is optional
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;
}Solution
Step 1: Understand multi-level overriding
Each class overridesdisplay()with its own version. The pointer is of type Animal* but points to a Puppy object.Step 2: Determine which method is called
Becausedisplay()is virtual, the most derived override (Puppy::display) is called at runtime.Final Answer:
Puppy -> Option CQuick Check:
Virtual calls deepest override in inheritance chain [OK]
- Expecting Dog or Animal output due to pointer type
- Thinking multiple overrides cause errors
- Ignoring virtual keyword effect
Ready to prove it?
Pass the test and this pattern moves unknown
