0
0
C++programming~10 mins

Constructor overloading in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Constructor overloading
Create object
Call constructor
Match constructor signature
Constructor 1: no params
Constructor 2: 1 param
Constructor 3: 2 params
Initialize object with matched constructor
Object ready to use
When creating an object, the program chooses the constructor that matches the given parameters to initialize the object.
Execution Sample
C++
class Box {
public:
  int length;
  int width;
  Box() { length = 1; }
  Box(int l) { length = l; }
  Box(int l, int w) { length = l; width = w; }
};
Box b1; Box b2(5); Box b3(3,4);
This code creates three Box objects using different constructors based on the number of arguments.
Execution Table
StepObject CreatedConstructor CalledParametersState After Construction
1b1Box()nonelength=1, width=uninitialized
2b2Box(int l)5length=5, width=uninitialized
3b3Box(int l, int w)3, 4length=3, width=4
4EndNo more objectsN/AAll objects initialized
💡 All objects created with matching constructors, execution ends.
Variable Tracker
VariableStartAfter b1After b2After b3
b1.lengthuninitialized111
b1.widthuninitializeduninitializeduninitializeduninitialized
b2.lengthuninitializeduninitialized55
b2.widthuninitializeduninitializeduninitializeduninitialized
b3.lengthuninitializeduninitializeduninitialized3
b3.widthuninitializeduninitializeduninitialized4
Key Moments - 3 Insights
Why does b1 call the constructor with no parameters?
Because b1 is created without any arguments, so the program matches it to the constructor with no parameters as shown in step 1 of the execution_table.
What happens if we create an object with two parameters?
The constructor with two parameters is called, initializing both length and width, as seen in step 3 of the execution_table.
Why is width uninitialized for b1 and b2?
Because the constructors called for b1 and b2 do not set width, so it remains uninitialized as shown in the variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which constructor is called when creating b2?
ABox(int l) with one parameter
BBox() with no parameters
CBox(int l, int w) with two parameters
DNo constructor is called
💡 Hint
Check step 2 in the execution_table where b2 is created.
At which step does the object have both length and width initialized?
AStep 1
BStep 2
CStep 3
DNo step initializes both
💡 Hint
Look at the 'State After Construction' column in the execution_table.
If we create Box b4(7), what will be the value of b4.length according to variable_tracker logic?
A1
B7
Cuninitialized
D4
💡 Hint
Refer to how b2 is initialized with one parameter in variable_tracker.
Concept Snapshot
Constructor Overloading in C++:
- Define multiple constructors with different parameters.
- Object creation calls constructor matching given arguments.
- Allows flexible object initialization.
- Unused members remain uninitialized unless set.
- Helps create objects with different starting states.
Full Transcript
Constructor overloading means having several constructors in a class, each with different parameters. When you create an object, the program picks the constructor that matches the number and type of arguments you provide. For example, if you create an object without arguments, the constructor with no parameters runs. If you provide one or two arguments, the matching constructor runs. This lets you create objects with different initial values easily. Variables not set by a constructor remain uninitialized. This visual trace shows three objects created with different constructors and how their variables change.