C++ gives every object a birth ritual and a farewell ritual — and runs them without being asked.
1 Constructor
Same name as the class, no return type, runs automatically the moment an object is created.
2 Overloaded constructors
Several constructors can coexist — the compiler picks one that matches the arguments you pass.
3 Destructor ~Player()
A tilde before the class name. It runs automatically when the object dies — ideal for cleanup.
4 Scope does the work
When a local object goes out of scope, its destructor fires on its own. No manual calls, ever.
Birth and cleanup
Constructor1Same name as the class, no return type, runs automatically the moment an object is created. Overloaded constructors2Several constructors can coexist — the compiler picks one that matches the arguments you pass. Destructor ~Player()3A tilde before the class name. It runs automatically when the object dies — ideal for cleanup. Scope does the work4When a local object goes out of scope, its destructor fires on its own. No manual calls, ever. 4 building blocks make up Birth and cleanup
Press play — two constructors build the players, then destructors fire in reverse order.
preprocessThe preprocessor pastes in iostream for cout.
Memory
no variables yet — press play
Console
output appears as the program runs…
Drag to orbit. Constructors land frames, destructors pop them — watch the stack unwind.
Drag to orbit the scene. Press Play to watch memory in action.
Two boxes are built, then main ends — in what order do the messages appear?
Your predicted output — one line per console line
Hint: Constructors run top to bottom; destructors run in reverse order when main ends.
Click a term, then click its description to lock the pair.
5 conceptsTap a card to expand ?Player()drops into place
?Player(std::string n)drops into place
?overloadingdrops into place
?~Player()drops into place
?scope exitdrops into place
01A constructor has the class name, no return type, and runs on creation.
02You can overload constructors — the arguments pick the right one.
03~ClassName is the destructor; it runs automatically at scope exit.
04Objects die in reverse order of construction.
05Automatic cleanup is the superpower behind RAII-style C++ code.