Default constructor in C++ - Time & Space Complexity
We want to understand how long it takes to create an object using a default constructor.
Specifically, how does the time change when we create many objects?
Analyze the time complexity of the following code snippet.
class MyClass {
public:
MyClass() { /* default constructor */ }
};
int main() {
const int n = 1000;
MyClass arr[n];
return 0;
}
This code creates an array of n objects using the default constructor.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the default constructor for each object.
- How many times: Exactly
ntimes, once per object.
Each new object requires one constructor call, so the total work grows directly with the number of objects.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 constructor calls |
| 100 | 100 constructor calls |
| 1000 | 1000 constructor calls |
Pattern observation: Doubling the number of objects doubles the total constructor calls.
Time Complexity: O(n)
This means the time to create all objects grows in a straight line with the number of objects.
[X] Wrong: "Creating many objects with a default constructor is instant and does not depend on how many objects there are."
[OK] Correct: Each object still needs its constructor called, so more objects mean more work and more time.
Understanding how object creation scales helps you reason about program speed and resource use in real projects.
"What if the default constructor did some heavy work inside? How would the time complexity change?"
Practice
default constructor in C++?Solution
Step 1: Understand constructor basics
A constructor is a special function to create objects.Step 2: Identify default constructor meaning
A default constructor has no parameters and sets default values.Final Answer:
A constructor with no parameters that initializes objects with default values. -> Option BQuick Check:
Default constructor = no parameters, default values [OK]
- Thinking default constructor requires arguments
- Confusing destructor with constructor
- Assuming default constructor runs at program end
Solution
Step 1: Check constructor syntax
Constructor has the same name as the class and no return type.Step 2: Identify default constructor syntax
Default constructor has empty parentheses and no parameters.Final Answer:
class MyClass { MyClass() {} }; -> Option AQuick Check:
Constructor syntax = class name + () + no return type [OK]
- Adding void return type to constructor
- Using parameters in default constructor
- Incorrect placement of braces
class Box {
public:
int length;
Box() { length = 5; }
};
int main() {
Box b;
std::cout << b.length;
return 0;
}Solution
Step 1: Understand the default constructor
The constructor sets length to 5 when object b is created.Step 2: Check output statement
Printing b.length outputs the value set by constructor, which is 5.Final Answer:
5 -> Option CQuick Check:
Constructor sets length=5, output = 5 [OK]
- Assuming uninitialized int defaults to 0
- Expecting compilation error due to missing initialization
- Thinking output is random without constructor
class Sample {
public:
int x;
Sample() {
x = 10
}
};Solution
Step 1: Check constructor body syntax
Each statement inside constructor must end with a semicolon.Step 2: Locate missing semicolon
The line 'x = 10' lacks a semicolon at the end.Final Answer:
Missing semicolon after x = 10 -> Option DQuick Check:
Statements need semicolons [OK]
- Adding return type to constructor
- Ignoring missing semicolon errors
- Confusing constructor name with return type
class Point {
public:
int x, y;
Point() { x = 0; y = 0; }
Point(int a, int b) { x = a; y = b; }
};
// In main:
Point p1;
Point p2(3, 4);
std::cout << p1.x << "," << p1.y << " " << p2.x << "," << p2.y;What is the output and why?
Solution
Step 1: Understand constructor calls
p1 is created with no arguments, so default constructor sets x=0, y=0.Step 2: Understand parameterized constructor
p2 is created with (3,4), so x=3, y=4 from parameterized constructor.Step 3: Check output statement
Output prints p1's coordinates then p2's coordinates separated by space.Final Answer:
0,0 3,4 because p1 uses default constructor, p2 uses parameterized constructor. -> Option AQuick Check:
Default and parameterized constructors work together [OK]
- Assuming default constructor overrides parameterized
- Expecting compilation error with multiple constructors
- Mixing up which constructor is called
Ready to prove it?
Pass the test and this pattern moves unknown
