Classes, Constructors, and Destructors
Defining a class, and the automatic construction/destruction lifecycle every object gets.
What you'll learn
- Define a class with a constructor that initializes its member data
- Define a destructor and explain when it runs automatically
- Predict the exact order constructor, method calls, and destructor run in for a simple scoped object
Explanation
A class groups data (member variables) and behavior (member functions) together. A constructor is a special member function, named exactly the same as the class, that runs automatically whenever an object is created -- it's where you initialize the object's data: Greeter(std::string name) : name_(name) { ... }. The : name_(name) part is a member initializer list, the preferred way to set a member variable's initial value directly, before the constructor's body even runs.
A destructor, named with a ~ before the class name (~Greeter() { ... }), runs automatically when an object's lifetime ends -- for a local (stack) object, that's the moment it goes out of scope, with no explicit call needed anywhere in your code. This automatic, guaranteed cleanup is central to how C++ manages resources safely, and you'll build directly on it in this course's RAII lesson.
Putting these together: creating a Greeter g("Ada"); calls the constructor immediately, calling g.greet() runs that member function whenever you call it explicitly, and once main reaches its closing brace, g goes out of scope and its destructor runs automatically -- in that exact order, deterministically, which is why you can predict a program's full output including cleanup, not just its "main" logic.
Guided lab
Predict: Constructor and destructor order
Read this program and predict exactly what it prints, in order.
#include <iostream>
#include <string>
class Greeter {
public:
Greeter(std::string name) : name_(name) {
std::cout << "Constructing " << name_ << std::endl;
}
~Greeter() {
std::cout << "Destroying " << name_ << std::endl;
}
void greet() {
std::cout << "Hello, " << name_ << "!" << std::endl;
}
private:
std::string name_;
};
int main() {
Greeter g("Ada");
g.greet();
return 0;
}Stuck? Get a hint.
Common mistakes
- Forgetting a constructor runs automatically on object creation -- there's no separate 'init' call needed or expected.
- Assuming a destructor must be called explicitly, rather than running automatically when a local object goes out of scope.
- Confusing the member initializer list (`: name_(name)`) with an assignment inside the constructor's body -- the initializer list runs first, before the body executes.
Knowledge check
Takeaway
A constructor runs automatically on creation and a destructor runs automatically when a local object leaves scope -- both deterministic, with no explicit call needed for either.
Summary
Classes group data and behavior; constructors initialize objects automatically on creation, and destructors clean up automatically at end of scope, in a predictable, deterministic order.
References
Your notes
Notes save automatically.
Finished this lesson?
Mark it complete to track your progress and schedule a future review.
AI tutor
The optional AI tutor isn't enabled in this deployment. All lessons, exercises, quizzes, and search work fully without it.