Object interaction in C++ - Time & Space Complexity
When objects work together in a program, it's important to know how the time needed grows as we add more objects.
We want to find out how the number of interactions affects the total work done.
Analyze the time complexity of the following code snippet.
class Item {
public:
void interact() {
// some simple operation
}
};
void processItems(std::vector<Item>& items) {
for (auto &item : items) {
item.interact();
}
}
This code calls a method on each object in a list, making each object do some work once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling
interact()on each object. - How many times: Once for each object in the list.
As the number of objects grows, the total work grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to interact() |
| 100 | 100 calls to interact() |
| 1000 | 1000 calls to interact() |
Pattern observation: Doubling the number of objects doubles the work.
Time Complexity: O(n)
This means the time needed grows directly with the number of objects.
[X] Wrong: "Calling a method on each object is constant time no matter how many objects there are."
[OK] Correct: Each object adds more work, so total time grows with the number of objects.
Understanding how objects interact and how that affects time helps you explain your code clearly and shows you think about efficiency.
"What if each interact() method called another loop over all objects? How would the time complexity change?"
Practice
Solution
Step 1: Understand method calls between objects
When one object calls a method on another, it requests that object to do something or provide information.Step 2: Differentiate from other actions
Copying data, deleting, or creating objects are different operations, not the same as calling a method.Final Answer:
The first object is asking the second object to perform an action. -> Option AQuick Check:
Object method call = request action [OK]
- Confusing method call with copying data
- Thinking method call deletes objects
- Assuming method call creates new objects inside others
obj of class Car to a method drive of another object driver?Solution
Step 1: Identify correct method call syntax
For an objectdriver, calling methoddrivewith argumentobjuses dot notation:driver.drive(obj);Step 2: Eliminate incorrect syntax
drive.driver(obj);reverses object and method,driver->drive(obj);is for pointers, anddrive(obj).driver;is invalid.Final Answer:
driver.drive(obj); -> Option AQuick Check:
Object.method(argument) = driver.drive(obj) [OK]
- Using arrow operator on non-pointer objects
- Swapping method and object names
- Incorrect punctuation or order
#include <iostream>
class Light {
public:
void turnOn() { std::cout << "Light is ON" << std::endl; }
};
class Switch {
public:
void press(Light &l) { l.turnOn(); }
};
int main() {
Light roomLight;
Switch wallSwitch;
wallSwitch.press(roomLight);
return 0;
}Solution
Step 1: Understand method call inside press()
Thepressmethod callsturnOn()on the passedLightobject, which prints "Light is ON".Step 2: Trace main function calls
wallSwitch.press(roomLight);callsturnOn()onroomLight, so output is "Light is ON".Final Answer:
Light is ON -> Option DQuick Check:
Method call prints "Light is ON" [OK]
- Assuming no output because press() returns void
- Confusing reference passing with copying
- Thinking code causes compilation error
#include <iostream>
class Engine {
public:
void start() { std::cout << "Engine started" << std::endl; }
};
class Car {
public:
void ignite(Engine e) { e.start(); }
};
int main() {
Engine myEngine;
Car myCar;
myCar.ignite(myEngine);
return 0;
}Solution
Step 1: Analyze parameter passing in ignite()
The methodignitetakesEngine eby value, which copies the object unnecessarily.Step 2: Suggest improvement
Passing by reference (Engine &e) avoids copying and is more efficient.Final Answer:
Passing Engine by value causes unnecessary copying. -> Option CQuick Check:
Pass objects by reference to avoid copying [OK]
- Thinking method start() is incorrect
- Assuming inheritance is required
- Believing start() cannot be called on Engine
Teacher and Student, how can you design a method in Teacher that lets a teacher assign a grade to a student object? Choose the best approach.Solution
Step 1: Understand object interaction for assigning grades
The Teacher should interact with Student by calling a method on Student to set the grade, passing the grade value.Step 2: Evaluate options
Define a method in Teacher that takes a Student reference and an int grade, then calls a Student method to set the grade. correctly uses a method in Teacher that takes a Student reference and grade, then calls Student's method to set the grade. Other options misuse inheritance, ignore object interaction, or use bad global state.Final Answer:
Define a method in Teacher that takes a Student reference and an int grade, then calls a Student method to set the grade. -> Option BQuick Check:
Teacher calls Student method to assign grade [OK]
- Misusing inheritance for unrelated roles
- Ignoring encapsulation by using globals
- Storing grades only in Teacher without Student update
Ready to prove it?
Pass the test and this pattern moves unknown
