0
0
C++programming~10 mins

Object lifecycle in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Object lifecycle
Object Declaration
Constructor Called
Object in Use
Destructor Called
Object Destroyed
This flow shows how an object is created, used, and then destroyed in C++.
Execution Sample
C++
#include <iostream>
class MyClass {
public:
  MyClass() { std::cout << "Constructor\n"; }
  ~MyClass() { std::cout << "Destructor\n"; }
};

int main() {
  MyClass obj;
}
This code creates an object of MyClass, calls its constructor, then calls its destructor when main ends.
Execution Table
StepActionOutputObject State
1Enter main functionNo object
2Declare obj of MyClassConstructorObject created
3Use obj (no output here)Object alive
4Exit main functionDestructorObject destroyed
💡 Program ends, destructor called, object lifecycle complete
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4
objNot declaredConstructedAliveDestroyed
Key Moments - 3 Insights
Why do we see 'Constructor' printed before 'Destructor'?
The constructor runs when the object is created (Step 2), and the destructor runs when the object goes out of scope at the end of main (Step 4), as shown in the execution_table.
Does the destructor run automatically or do we call it?
The destructor runs automatically when the object is destroyed (Step 4). We do not call it manually, as shown by the output in the execution_table.
What happens if we declare the object inside a block?
The constructor runs when declared, and the destructor runs when the block ends, similar to Steps 2 and 4 but earlier. The lifecycle is the same but scoped to the block.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'obj' after Step 3?
AObject destroyed
BObject alive
CObject not declared
DConstructor running
💡 Hint
Check the 'Object State' column for Step 3 in the execution_table.
At which step does the destructor run according to the execution_table?
AStep 4
BStep 3
CStep 2
DDestructor never runs
💡 Hint
Look at the 'Output' column for the destructor call in the execution_table.
If we remove the object declaration, what output will the program produce?
AConstructor and Destructor
BOnly Constructor
CNo output
DOnly Destructor
💡 Hint
Without object declaration, constructor and destructor are not called, so no output appears.
Concept Snapshot
Object lifecycle in C++:
- Object declared -> constructor runs
- Object used while alive
- Object destroyed -> destructor runs
- Constructor and destructor manage setup and cleanup
- Destructor runs automatically at scope end
Full Transcript
This example shows the lifecycle of an object in C++. When the object 'obj' is declared in main, the constructor runs and prints 'Constructor'. The object stays alive during the main function. When main ends, the destructor runs automatically and prints 'Destructor'. This shows how C++ manages object creation and destruction automatically based on scope.