Base and derived classes in C++ - Time & Space Complexity
When using base and derived classes, it's important to know how the program's running time changes as the input grows.
We want to see how the number of operations changes when creating or using objects from these classes.
Analyze the time complexity of the following code snippet.
class Base {
public:
void display() { /* simple print */ }
};
class Derived : public Base {
public:
void show() { /* simple print */ }
};
int main() {
Derived obj;
obj.display();
obj.show();
return 0;
}
This code creates an object of a derived class and calls methods from both base and derived classes.
Look for loops, recursion, or repeated calls.
- Primary operation: There are no loops or recursion here; only simple method calls.
- How many times: Each method is called once.
Since there are no loops or repeated calls, the number of operations stays the same no matter how big the input is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 (object creation + 2 method calls) |
| 100 | 3 (same as above) |
| 1000 | 3 (still the same) |
Pattern observation: The operations do not increase with input size; they stay constant.
Time Complexity: O(1)
This means the program runs in constant time, doing the same amount of work regardless of input size.
[X] Wrong: "Using derived classes always makes the program slower because of inheritance overhead."
[OK] Correct: Inheritance itself does not add repeated work or loops; method calls run once, so time stays constant.
Understanding how base and derived classes affect time helps you explain your code clearly and shows you know how object-oriented design impacts performance.
What if the derived class had a loop inside one of its methods? How would the time complexity change?
Practice
Solution
Step 1: Understand base class role
A base class is designed to hold common features that multiple derived classes can share.Step 2: Compare options
To hold common features shared by derived classes correctly states the purpose. Other options describe derived class behavior or incorrect roles.Final Answer:
To hold common features shared by derived classes -> Option DQuick Check:
Base class = common features [OK]
- Confusing base class with derived class
- Thinking base class cannot have data members
- Assuming base class is only for private data
Car inheriting from base class Vehicle in C++?Solution
Step 1: Recall C++ inheritance syntax
In C++, a derived class inherits from a base class using a colon and access specifier likepublic.Step 2: Check options
class Car : public Vehicle {}; uses correct syntax:class Car : public Vehicle {}. Others use incorrect keywords or omit access specifier.Final Answer:
class Car : public Vehicle {} -> Option BQuick Check:
Inheritance syntax = colon + access specifier [OK]
- Omitting access specifier like public
- Using wrong keywords like inherits or extends
- Forgetting colon before base class name
class Base {
public:
void show() { std::cout << "Base"; }
};
class Derived : public Base {
public:
void show() { std::cout << "Derived"; }
};
int main() {
Derived d;
d.show();
return 0;
}Solution
Step 1: Understand method overriding in derived class
The derived classDerivedhas its ownshow()method which hides the base class method.Step 2: Check which method is called
Callingd.show()on aDerivedobject calls the derived class method, printing "Derived".Final Answer:
Derived -> Option AQuick Check:
Derived method overrides base method [OK]
- Assuming base class method runs without virtual keyword
- Expecting compilation error due to method name clash
- Thinking both outputs print
class Base {
public:
void display() { std::cout << "Base"; }
};
class Derived : public Base {
public:
void display() { std::cout << "Derived"; }
};
int main() {
Base* ptr = new Derived();
ptr->display();
return 0;
}Solution
Step 1: Understand polymorphism with base pointers
Callingdisplay()via a base class pointer calls base method unless it is virtual.Step 2: Check for virtual keyword
The base class methoddisplay()lacksvirtual, soptr->display()calls base version, not derived.Final Answer:
Missing virtual keyword in base class method -> Option AQuick Check:
Virtual needed for polymorphic calls [OK]
- Thinking pointer type is wrong
- Believing derived method runs without virtual
- Assuming static keyword fixes this
class Animal {
public:
virtual std::string sound() { return "Unknown"; }
};
class Dog : public Animal {
public:
std::string sound() override { return "Bark"; }
};
class Cat : public Animal {
public:
std::string sound() override { return "Meow"; }
};
Which code snippet correctly creates a list of animals and prints their sounds?
Solution
Step 1: Understand polymorphism with collections
To preserve derived class behavior, store pointers to base class, not objects by value.Step 2: Analyze options
std::vector animals = {new Dog(), new Cat()}; for(auto a : animals) std::cout << a->sound() << ' '; usesstd::vector<Animal*>with pointers toDogandCat, callingsound()polymorphically.Final Answer:
std::vector<Animal*> animals = {new Dog(), new Cat()}; for(auto a : animals) std::cout << a->sound() << ' '; -> Option CQuick Check:
Use base class pointers for polymorphic containers [OK]
- Storing objects by value causes slicing
- Using wrong container type for polymorphism
- Forgetting to use pointers or references
Ready to prove it?
Pass the test and this pattern moves unknown
