Function overriding in C++ - Time & Space Complexity
When we use function overriding, we replace a parent class function with a child class version. We want to see how this affects the time it takes to run the program.
How does calling an overridden function change the work done as input grows?
Analyze the time complexity of the following code snippet.
class Base {
public:
int n;
virtual void display() {
for(int i = 0; i < n; i++) {
// some work
}
}
};
class Derived : public Base {
public:
void display() override {
for(int i = 0; i < n; i++) {
// some different work
}
}
};
void callDisplay(Base* obj) {
obj->display();
}
This code shows a base class and a derived class both having a display function that loops n times. The callDisplay function calls display through a base pointer.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for loop inside the display function that runs n times.
- How many times: Exactly n times per call to display.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 loop steps |
| 100 | About 100 loop steps |
| 1000 | About 1000 loop steps |
Pattern observation: The work grows directly with n. If n doubles, the work doubles too.
Time Complexity: O(n)
This means the time to run display grows in a straight line with the input size n.
[X] Wrong: "Overriding a function makes the program slower or faster automatically."
[OK] Correct: Overriding only changes which code runs, not how many times the main loop runs. The time depends on the loop inside, not the override itself.
Understanding how overriding affects time helps you explain code behavior clearly. It shows you know when the program work changes and when it stays the same.
"What if the derived class display function had two nested loops each running n times? How would the time complexity change?"
Practice
Solution
Step 1: Understand function overriding concept
Function overriding means a child class changes the behavior of a function defined in its parent class.Step 2: Identify correct description
A child class provides its own version of a parent class's virtual function. correctly describes overriding as providing a new version of a parent's virtual function in the child class.Final Answer:
A child class provides its own version of a parent class's virtual function. -> Option AQuick Check:
Function overriding = child changes parent's virtual function [OK]
- Confusing overriding with overloading
- Thinking overriding happens without virtual keyword
- Believing overriding applies to non-virtual functions
Solution
Step 1: Recall override syntax in child class
In C++11+, the child class function uses the same signature and adds the 'override' keyword after the function declaration.Step 2: Identify correct syntax
virtual void func() override; shows the correct syntax: 'virtual void func() override;'. The 'virtual' keyword can be used in the child class but is optional; however, it is valid and common to include it.Final Answer:
virtual void func() override; -> Option DQuick Check:
Child function uses virtual and override keywords correctly [OK]
- Adding 'virtual' keyword after function name
- Placing 'override' before function name
- Omitting 'override' keyword entirely
class Base {
public:
virtual void show() { std::cout << "Base"; }
};
class Derived : public Base {
public:
void show() override { std::cout << "Derived"; }
};
int main() {
Base* b = new Derived();
b->show();
delete b;
return 0;
}Solution
Step 1: Understand virtual function behavior
The pointer 'b' is of type Base* but points to a Derived object. Because 'show' is virtual, the Derived version is called.Step 2: Trace the function call
Calling b->show() invokes Derived::show(), printing "Derived".Final Answer:
Derived -> Option AQuick Check:
Virtual function calls child's version [OK]
- Expecting base class output
- Forgetting to make base function virtual
- Confusing pointer type with object type
class Parent {
public:
void display() { std::cout << "Parent"; }
};
class Child : public Parent {
public:
void display() override { std::cout << "Child"; }
};Solution
Step 1: Check virtual keyword in parent
For overriding to work, the base class function must be declared virtual.Step 2: Analyze override keyword usage
Child uses 'override' but Parent's display() is not virtual, causing a compilation error.Final Answer:
Parent's display() must be declared virtual to override. -> Option BQuick Check:
Base function must be virtual for override [OK]
- Ignoring virtual keyword in base class
- Thinking override works without virtual
- Assuming override keyword is optional
obj->print();? class A {
public:
virtual void print() { std::cout << "A"; }
};
class B : public A {
public:
void print() override { std::cout << "B"; }
};
class C : public B {
public:
void print() override { std::cout << "C"; }
};
int main() {
A* obj = new C();
obj->print();
delete obj;
return 0;
}Solution
Step 1: Understand pointer and object types
Pointer 'obj' is of type A*, but it points to an object of class C.Step 2: Follow virtual function overriding chain
Since print() is virtual and overridden in B and C, the most derived override (C::print) is called.Final Answer:
C -> Option CQuick Check:
Virtual call resolves to most derived override [OK]
- Expecting base class output
- Confusing pointer type with object type
- Ignoring virtual function behavior
Ready to prove it?
Pass the test and this pattern moves unknown
