How a Constructor Executes Automatically
How a Constructor Executes Automatically
Object Created
Student s1; statement runs in main()── Compiler triggers automatically ──▶
Constructor Runs
Student() sets rollNo = 0, marks = 0.0── Ready for use ──▶
Initialized Object
Zero garbage values! Clean object ready to work.
Explanation
- Object construction selects a matching constructor and initialises the members.
- The constructor body can then complete setup before the object is used.
Constructor Overloading Resolution
Constructor Overloading Resolution
Complex c1;Calls
Sets real = 0, imag = 0
Complex() (0 arguments)Sets real = 0, imag = 0
Complex c2(5.5);Calls
Sets real = 5.5, imag = 5.5
Complex(float) (1 argument)Sets real = 5.5, imag = 5.5
Complex c3(4.0, 9.2);Calls
Sets real = 4.0, imag = 9.2
Complex(float, float) (2 arguments)Sets real = 4.0, imag = 9.2
Explanation
- The compiler compares the supplied arguments with the available constructors.
- Ambiguous matches require a change to the call or the overload set.
Compile-Time vs. Dynamic Runtime Initialization
Compile-Time vs. Dynamic Runtime Initialization
Compile-Time (Static) Initialization
Student s1(101, "Aman", 85.0);Values are hardcoded in the source code. Known before the program even runs.
Dynamic (Runtime) Initialization
cin >> p >> y >> r;FixedDeposit fd1(p, y, r);Values are typed in by the user while the program is actually running!
Explanation
- Some initial values can be determined before the program runs.
- Others depend on a runtime input or calculation used during initialisation.
Shallow Copy vs. Deep Copy Memory Architecture
Shallow Copy vs. Deep Copy Memory Architecture
Shallow Copy (Compiler Default)
Just copies the pointer address. Both objects point to the exact same memory on the heap! When Object 1 is deleted, it destroys that memory, leaving Object 2 with a broken Dangling Pointer. When Object 2 deletes it again, the program crashes with a "Double Free" error!
Deep Copy (Our Custom Solution)
Allocates a brand new, independent block of heap memory using
new, and copies the actual data over. Both objects have their own separate memory and can be safely deleted without any crash!Explanation
- Follow each pointer to see whether the objects share the underlying buffer.
- Separate buffers allow independent updates and avoid competing ownership.
Shallow Copy vs Deep Copy in C++
Shallow Copy vs Deep Copy in C++
Shallow Copy (Dangerous Default)
- Copies raw pointer memory addresses directly.
- Both objects point to the same heap memory chunk.
- Triggers catastrophic Double-Free crash when destructors run!
Deep Copy (Safe Custom Copy)
- Allocates a brand new, distinct heap block via
new. - Copies the underlying data values, not the pointer address.
- Each object independently frees its own memory safely.
Explanation
- A shallow copy of an owning pointer leaves both objects referring to the same allocation.
- A deep copy creates a second allocation and copies the contents.
Lifecycle of an Object
Lifecycle of an Object
Object Created
Student s1;Constructor: Allocates memory & initializes variables.
── Object is used in code ──▶
Object Goes Out of Scope
Closing brace
Destructor: Frees memory & cleans up resources!
} reachedDestructor: Frees memory & cleans up resources!
Explanation
- Construction establishes an object's state at the start of its lifetime.
- Destruction releases owned resources when that lifetime ends.