Constructors and Destructors

Lesson 13 of 20

Objects get born and die: constructors set them up, destructors clean up — automatically, in reverse order.

The big picture

Birth and cleanup

C++ gives every object a birth ritual and a farewell ritual — and runs them without being asked.

Constructor

Same name as the class, no return type, runs automatically the moment an object is created.

Overloaded constructors

Several constructors can coexist — the compiler picks one that matches the arguments you pass.

Destructor ~Player()

A tilde before the class name. It runs automatically when the object dies — ideal for cleanup.

Scope does the work

When a local object goes out of scope, its destructor fires on its own. No manual calls, ever.