Data members and member functions in C++ - Time & Space Complexity
We want to understand how the time taken by a program changes when it uses data members and member functions in a class.
Specifically, we ask: How does the program's running time grow as the size of data changes?
Analyze the time complexity of the following code snippet.
class Sample {
int data[100];
public:
void fillData() {
for (int i = 0; i < 100; i++) {
data[i] = i * 2;
}
}
int getData(int index) {
return data[index];
}
};
This code defines a class with a fixed-size array and two member functions: one fills the array, the other returns a value at a given position.
Look for loops or repeated actions inside the member functions.
- Primary operation: The for-loop inside
fillData()that sets each element. - How many times: Exactly 100 times, once for each element.
Since the array size is fixed at 100, the number of operations stays the same no matter what.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 100 (fixed) |
| 100 | 100 (fixed) |
| 1000 | 100 (fixed) |
Pattern observation: The time does not grow with input size because the array size is constant.
Time Complexity: O(1)
This means the time to run these member functions stays the same no matter how big the input might be.
[X] Wrong: "Since there is a loop, the time must grow with input size."
[OK] Correct: The loop runs a fixed number of times (100), so it does not depend on input size and the time stays constant.
Understanding how member functions and data members affect time helps you explain how your code behaves in real projects.
"What if the array size was a variable passed to the class instead of fixed? How would the time complexity change?"
Practice
data member in a C++ class?Solution
Step 1: Understand what data members are
Data members are variables inside a class that store information about each object created from that class.Step 2: Differentiate from member functions
Member functions are actions or behaviors, not storage. Keywords and loops are unrelated to data members.Final Answer:
A variable that holds information about an object -> Option CQuick Check:
Data member = variable storing object info [OK]
- Confusing data members with member functions
- Thinking data members are keywords
- Mixing data members with control structures
display inside a C++ class?Solution
Step 1: Recall correct member function syntax
Member functions are declared with return type, name, and parentheses, ending with a semicolon inside the class.Step 2: Check each option
void display(); matches correct syntax: return type 'void', function name 'display', parentheses, and semicolon. Others are invalid syntax.Final Answer:
void display(); -> Option AQuick Check:
Correct function declaration = void display(); [OK]
- Omitting parentheses after function name
- Using wrong order of return type and name
- Missing semicolon after declaration
#include <iostream>
class Box {
public:
int length;
void setLength(int len) {
length = len;
}
int getLength() {
return length;
}
};
int main() {
Box box1;
box1.setLength(10);
std::cout << box1.getLength();
return 0;
}Solution
Step 1: Understand member function calls
The functionsetLength(10)sets the data memberlengthto 10 for objectbox1.Step 2: Check output from
The functiongetLength()getLength()returns the current value oflength, which was set to 10.Final Answer:
10 -> Option DQuick Check:
Set length to 10, get length returns 10 [OK]
- Assuming default value 0 is printed
- Confusing function names
- Expecting compilation error due to missing constructor
#include <iostream>
class Person {
int age;
public:
void setAge(int a) {
age = a;
}
int getAge() {
return age;
}
};
int main() {
Person p;
p.age = 25;
std::cout << p.getAge();
return 0;
}Solution
Step 1: Check access to data member 'age'
The data member 'age' is private by default because no access specifier is given before it.Step 2: Analyze main function usage
In main, code tries to access 'p.age = 25;' directly, which is not allowed for private members, causing a compilation error.Final Answer:
Error: 'age' is private and cannot be accessed directly -> Option AQuick Check:
Private members can't be accessed outside class [OK]
- Trying to access private data members directly
- Ignoring default private access in classes
- Assuming no error if setter is not called
count data member by 1 for an object obj?class Counter {
private:
int count = 0;
public:
void increment() {
count++;
}
};Solution
Step 1: Understand the class design
The class has a private data membercount = 0and provides a public member functionincrement()that increasescountby 1.Step 2: Check each option for correctness
obj.count = obj.count + 1;tries to access privatecountdirectly, compilation error.obj.increment();calls the member function correctly.Counter::increment();tries to call non-static member function without an object, invalid.obj.count++; obj.increment();first accesses privatecount, error.Final Answer:
obj.increment(); -> Option BQuick Check:
Use member function to change data member [OK]
- Calling member function without object
- Manually changing data member instead of using function
- Incrementing count twice accidentally
Ready to prove it?
Pass the test and this pattern moves unknown
