Constructors, Destructors and Object
Imagine setting up a new classroom. Before students arrive, you arrange chairs, set up the board, and prepare materials, that’s the construction phase.
After class ends, you clean up, turn off lights, and reset the room, that’s the destruction phase.
Objects in C++ go through the exact same cycle. They must be set up properly when created, and cleaned up when they are no longer needed.
This is where constructors and destructors come in.
Auto-Initializing Student Profile
You’ll build a Student class that:
- Automatically sets initial values using a constructor.
- Prints all student details.
- Shows when an object is deleted using a destructor.
Expected output:
1 2 3 4 5 6 7Constructor called: Student object created. ======================== STUDENT PROFILE Name: Ali Age: 16 ======================== Destructor called: Student object destroyed.
This teaches you how objects start and end their life.
What Is an Object Lifecycle?
The object lifecycle includes creation, usage, and destruction.
C++ controls this automatically for local objects.
Constructors run at creation; destructors run when the object goes out of scope.
What Is a Constructor?
A constructor is a special function that runs automatically when an object is created.
It initializes member variables or prepares resources.
It has the same name as the class and no return type.
Example
1 2 3 4class Student { public: Student() { cout << "Constructor called"; } };
Parameterized Constructor
A constructor can accept arguments, allowing you to set values during object creation. This makes objects more flexible and dynamic.
Example
1Student(string n, int a) { name = n; age = a; }
Default Constructor
A default constructor takes no arguments. It's useful for setting initial values or default states. C++ generates one automatically if you don’t create it.
What Is a Destructor?
A destructor is a special function that runs automatically when an object is destroyed. It cleans up memory or resources. It uses a tilde ~ before the class name.
Example
1~Student() { cout << "Destructor called"; }
When Destructors Are Called
Destructors run when:
- The object goes out of scope
- A dynamically allocated object is deleted
- The program ends
This ensures proper cleanup.
Let’s Build the Student Lifecycle Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35#include <iostream> using namespace std; class Student { public: string name; int age; // Constructor Student(string n, int a) { name = n; age = a; cout << "Constructor called: Student object created." << endl; } // Destructor ~Student() { cout << "Destructor called: Student object destroyed." << endl; } void display() { cout << "========================" << endl; cout << " STUDENT PROFILE" << endl; cout << " Name: " << name << endl; cout << " Age: " << age << endl; cout << "========================" << endl; } }; int main() { Student s("Ali", 16); s.display(); return 0; // Destructor will run here }
How It Works
- The constructor initializes values and prints a message.
- The object is used normally through display().
- When main() ends, the destructor runs automatically.
Run this in the DevsCall AI Code Runner to see lifecycle messages.
Learn Together with AI
Try these AI prompts:
- “Give me a class with multiple constructors in C++.”
- “Explain destructor use in dynamic memory.”
- “Show how constructor overloading works.”
- “Modify my class to print when resources are freed.”
AI can help you test, expand, and understand deeper OOP concepts.
Practice Time
Create a C++ program that:
- Defines a class Car with:
- brand
- model
- year
- Adds a parameterized constructor to set all values.
- Adds a destructor that prints "Car object destroyed."
- Displays:
1 2 3 4 5 6======================== CAR DETAILS Brand: Toyota Model: Corolla Year: 2020 ========================
Ask AI:
“How do constructors initialize object values in C++?”
Frequently Asked Questions
It refers to an object’s journey: creation (constructor), usage, and cleanup (destructor). C++ manages this automatically for most objects.
No. Constructors never return values, not even void. Their sole purpose is to initialize objects.
Yes. This is called constructor overloading. It lets you create objects with different initial setups based on which constructor you call.
Still have questions?Contact our support team