0
0
C++programming~10 mins

Object creation and destruction flow in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Object creation and destruction flow
Start Program
Call Constructor
Object Created
Use Object
Call Destructor
Object Destroyed
End Program
This flow shows how a C++ object is created by calling its constructor, used, then destroyed by calling its destructor when it goes out of scope.
Execution Sample
C++
#include <iostream>

class MyClass {
public:
  MyClass() { std::cout << "Created\n"; }
  ~MyClass() { std::cout << "Destroyed\n"; }
};

int main() {
  MyClass obj;
  return 0;
}
This code creates an object of MyClass, prints "Created" when constructed, and "Destroyed" when the object goes out of scope at program end.
Execution Table
StepActionOutputObject State
1Enter main functionNo object
2Call MyClass constructorCreatedObject obj created
3Object obj in useObject obj alive
4Exit main function scopeObject obj about to be destroyed
5Call MyClass destructorDestroyedObject obj destroyed
6Program endsNo object
💡 Program ends after object destruction when main function scope ends
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
objNot createdCreatedAliveDestroyedNo object
Key Moments - 3 Insights
Why do we see "Created" before "Destroyed" in the output?
The constructor runs first when the object is created (Step 2), printing "Created". The destructor runs last when the object goes out of scope (Step 5), printing "Destroyed".
When exactly is the destructor called?
The destructor is called automatically when the object goes out of scope, here at the end of main (Step 4 and 5), as shown in the execution_table.
What happens if we create the object inside a block {}?
The object is destroyed as soon as the block ends, calling the destructor immediately after the block, similar to Step 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of 'obj' after Step 3?
AObject obj alive
BObject obj destroyed
CNo object
DObject obj about to be destroyed
💡 Hint
Check the 'Object State' column for Step 3 in the execution_table
At which step is the destructor called according to the execution table?
AStep 2
BStep 5
CStep 3
DStep 6
💡 Hint
Look for the action 'Call MyClass destructor' in the execution_table
If we remove the object creation line, what would be the output?
AOnly Destroyed
BOnly Created
CNo output
DCreated and Destroyed
💡 Hint
Without object creation, constructor and destructor are not called, so no output lines appear
Concept Snapshot
Object creation calls the constructor.
Object destruction calls the destructor.
Constructor runs when object is made.
Destructor runs when object goes out of scope.
Output shows creation then destruction messages.
Full Transcript
This visual trace shows how a C++ object is created and destroyed. The program starts and enters main. At Step 2, the constructor runs, printing "Created" and creating the object. The object is alive and can be used. When main ends, the object goes out of scope at Step 4, and the destructor runs at Step 5, printing "Destroyed". Finally, the program ends with no objects alive. This flow helps understand when constructors and destructors run automatically in C++.