Concept Flow - Default constructor
Create object
Call default constructor
Initialize members with default values
Object ready to use
When an object is created without arguments, the default constructor runs to set initial values.
class Box { public: int length; Box() { length = 5; } }; Box b;
| Step | Action | Member 'length' Value | Output/State |
|---|---|---|---|
| 1 | Object b created | uninitialized | Object b exists, length not set yet |
| 2 | Default constructor called | 5 | length set to 5 |
| 3 | Object b ready | 5 | Object b can be used with length=5 |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| b.length | undefined | undefined | 5 | 5 |
Default constructor: - Special function with no parameters - Called automatically when object created - Initializes members with default values - If not defined, compiler provides one - Ensures object is ready to use