Getter and setter methods in C++ - Time & Space Complexity
We want to see how the time it takes to run getter and setter methods changes as we use them more.
How does calling these methods affect the program's speed when the input size grows?
Analyze the time complexity of the following code snippet.
class Box {
private:
int value;
public:
int getValue() { return value; }
void setValue(int v) { value = v; }
};
void updateValues(Box boxes[], int n) {
for (int i = 0; i < n; i++) {
boxes[i].setValue(i);
}
}
This code defines simple getter and setter methods for a class and uses the setter in a loop to update many objects.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the setter method inside a loop.
- How many times: The setter is called once for each element in the array, so n times.
As the number of boxes (n) grows, the number of setter calls grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 setter calls |
| 100 | 100 setter calls |
| 1000 | 1000 setter calls |
Pattern observation: The work grows directly with the number of items; doubling n doubles the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of objects we update.
[X] Wrong: "Getter and setter methods always add extra hidden loops making the code slower."
[OK] Correct: These methods usually just read or write a value directly, so each call takes the same small time, no extra loops inside.
Understanding how simple methods like getters and setters affect performance helps you explain your code clearly and shows you know how to think about efficiency.
"What if the setter method included a loop inside it? How would the time complexity change?"
Practice
Solution
Step 1: Understand encapsulation in classes
Getter and setter methods are used to access and modify private variables safely without exposing them directly.Step 2: Identify the role of getters and setters
Getters return the value, setters allow controlled modification, often with validation.Final Answer:
To control access to private variables safely -> Option AQuick Check:
Getter and setter = control access [OK]
- Thinking getters/setters make variables public
- Confusing setters with printing functions
- Assuming getters modify data
age?Solution
Step 1: Identify setter method signature
A setter usually returns void and takes a parameter to set the private variable.Step 2: Match correct syntax
void setAge(int a) { age = a; } matches: void return type, parameter int a, and sets age = a.Final Answer:
void setAge(int a) { age = a; } -> Option BQuick Check:
Setter = void + parameter + assign [OK]
- Using return type int for setter
- Naming setter as getAge
- Setter returning a value
#include <iostream>
class Person {
private:
int age;
public:
void setAge(int a) { if(a >= 0) age = a; else age = 0; }
int getAge() { return age; }
};
int main() {
Person p;
p.setAge(-5);
std::cout << p.getAge();
return 0;
}Solution
Step 1: Analyze setter behavior with negative input
The setter checks if the input is >= 0; if not, it sets age to 0.Step 2: Check what happens with -5
Since -5 < 0, age is set to 0.Final Answer:
0 -> Option CQuick Check:
Negative input sets age to 0 [OK]
- Assuming negative value is stored
- Expecting garbage value from uninitialized variable
- Thinking code won't compile
class Box {
private:
int length;
public:
void setLength() { length = l; }
};Solution
Step 1: Check setter method parameters
The setter tries to assign 'l' but has no parameter 'l' defined.Step 2: Fix by adding parameter
Adding 'int l' as a parameter allows passing the value to set length.Final Answer:
Add parameter int l to setLength method -> Option DQuick Check:
Setter needs parameter to assign value [OK]
- Forgetting to add parameter
- Changing return type unnecessarily
- Making variable public instead of fixing setter
Temperature that stores temperature in Celsius but only allows values between -273 and 1000. Which setter method correctly enforces this rule?Solution
Step 1: Understand the value range requirement
Temperature must be between -273 and 1000 inclusive.Step 2: Check which setter enforces this range
void setTemp(int t) { if(t >= -273 && t <= 1000) temp = t; else temp = -273; } checks the range and sets temp accordingly; others do not enforce limits properly.Final Answer:
void setTemp(int t) { if(t >= -273 && t <= 1000) temp = t; else temp = -273; } -> Option AQuick Check:
Setter validates range before setting [OK]
- Not validating input range
- Setter without parameters
- Setter returning int instead of void
Ready to prove it?
Pass the test and this pattern moves unknown
