When you create an object, its data members should start with sensible values. Setting each member by hand after creating the object is slow and easy to forget. A constructor does it automatically at the moment the object is created. A destructor runs automatically when the object is destroyed, which is where you clean up.
This page covers the default, parameterised and copy constructors, the initializer list and destructors, with a program and output for each. The last program shows the exact order in which constructors and destructors run, a common exam and interview question. You should know C++ classes and objects first.
Rules of a constructor
- It has the same name as the class.
- It has no return type, not even
void. - It runs automatically when an object is created. You do not call it by name.
- It is usually
public, so code outside the class can create objects. - A class can have more than one constructor, with different parameters.
Default constructor
A default constructor takes no arguments.
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int marks;
public:
Student() {
name = "Unknown";
marks = 0;
cout << "Default constructor called\n";
}
void show() {
cout << name << " - " << marks << "\n";
}
};
int main() {
Student s;
s.show();
return 0;
}
Output:
Default constructor called
Unknown - 0
The line Student s; creates the object, and C++ calls Student() straight away. By the time show() runs, the data is already set.
If you write no constructor at all, the compiler adds an empty default constructor for you. It does not set int members to 0, so they hold garbage values. That is why you should write your own.
Common mistake: Writing
Student s();with empty brackets. C++ reads this as the declaration of a function calledsthat returns aStudent, not as an object. WriteStudent s;orStudent s{};.
Parameterised constructor
A parameterised constructor takes values, so each object can start with different data.
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int marks;
public:
Student() {
name = "Unknown";
marks = 0;
}
Student(string n, int m) {
name = n;
marks = m;
}
void show() {
cout << name << " - " << marks << "\n";
}
};
int main() {
Student s1;
Student s2("Anjali", 88);
Student s3("Kiran", 74);
s1.show();
s2.show();
s3.show();
return 0;
}
Output:
Unknown - 0
Anjali - 88
Kiran - 74
This class has two constructors. C++ chooses the one whose parameters match the arguments: no arguments calls Student(), and a string plus an int calls Student(string, int). Having more than one constructor is called constructor overloading.
Common mistake: Once you write any constructor, the compiler stops adding the automatic default constructor. If this class had only
Student(string n, int m), the lineStudent s1;would not compile. Add a default constructor yourself if you need one.
Initializer list
Instead of assigning members inside the body, you can set them in an initializer list after a colon. This is the preferred style in C++.
#include <iostream>
#include <string>
using namespace std;
class Book {
private:
string title;
int pages;
public:
Book(string t, int p) : title(t), pages(p) {
}
void show() {
cout << title << " has " << pages << " pages\n";
}
};
int main() {
Book b("Let Us C", 600);
b.show();
return 0;
}
Output:
Let Us C has 600 pages
title(t) sets title to t while the object is being built, instead of building an empty string first and assigning it later. Members that are const or references can only be set this way.
Copy constructor
A copy constructor creates a new object as a copy of an existing object of the same class. Its parameter is a reference to the class, usually const ClassName &.
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int marks;
public:
Student(string n, int m) : name(n), marks(m) {
}
Student(const Student &other) : name(other.name), marks(other.marks) {
cout << "Copy constructor called for " << name << "\n";
}
void setMarks(int m) {
marks = m;
}
void show() {
cout << name << " - " << marks << "\n";
}
};
int main() {
Student s1("Anjali", 88);
Student s2 = s1;
Student s3(s1);
s2.setMarks(95);
s1.show();
s2.show();
s3.show();
return 0;
}
Output:
Copy constructor called for Anjali
Copy constructor called for Anjali
Anjali - 88
Anjali - 95
Anjali - 88
Both Student s2 = s1; and Student s3(s1); call the copy constructor. Changing s2 afterwards does not change s1, because s2 is a separate object with its own data.
The copy constructor is called when:
- You create an object from another:
Student s2 = s1;orStudent s2(s1);. - You pass an object by value to a function.
- A function returns an object by value, although the compiler often skips the copy in this case as an optimisation.
If you do not write a copy constructor, the compiler creates one that copies each member. That is fine for this class. You need your own when the class holds a raw pointer to memory it allocated, because copying only the pointer leaves two objects sharing (and later both deleting) the same memory.
The parameter must be a reference. With Student(Student other), passing other by value would itself need a copy, which would call the copy constructor again, forever. The compiler rejects it.
Destructor
A destructor runs automatically when an object is destroyed. It has the class name with a tilde (~) in front, no parameters and no return type.
#include <iostream>
using namespace std;
class Demo {
public:
Demo() {
cout << "Object created\n";
}
~Demo() {
cout << "Object destroyed\n";
}
};
int main() {
cout << "Start of main\n";
Demo d;
cout << "End of main\n";
return 0;
}
Output:
Start of main
Object created
End of main
Object destroyed
d is a local object, so it is destroyed when main ends, after End of main is printed. Destructors are used to release things the object holds, such as memory taken with new, an open file or a network connection.
| Point | Constructor | Destructor |
|---|---|---|
| Name | ClassName() |
~ClassName() |
| When it runs | When the object is created | When the object is destroyed |
| Parameters | Can take parameters | Never takes parameters |
| How many per class | Many (overloading) | Exactly one |
| Return type | None | None |
| Typical job | Set starting values, acquire resources | Release resources |
Order of constructor and destructor calls
This program shows the order clearly. Each object prints its name when it is built and when it is destroyed.
#include <iostream>
#include <string>
using namespace std;
class Demo {
private:
string id;
public:
Demo(string i) : id(i) {
cout << "Constructor: " << id << "\n";
}
~Demo() {
cout << "Destructor: " << id << "\n";
}
};
int main() {
cout << "main starts\n";
Demo a("A");
Demo b("B");
{
Demo c("C");
cout << "inner block ends\n";
}
Demo *p = new Demo("D");
delete p;
cout << "main ends\n";
return 0;
}
Output:
main starts
Constructor: A
Constructor: B
Constructor: C
inner block ends
Destructor: C
Constructor: D
Destructor: D
main ends
Destructor: B
Destructor: A
Read it in three parts:
- Local objects are built in the order they are declared: A, then B.
- An object is destroyed when its block ends.
clives inside the inner{ }, so its destructor runs right afterinner block ends, long beforemainends. - Objects created with
neware destroyed only when you calldelete. If you forgetdelete p;, the destructor of D never runs and the memory leaks.
At the end of main, the remaining local objects are destroyed in reverse order of creation: B first, then A. Objects are destroyed in reverse because a later object may depend on an earlier one, so the later one must be cleaned up first.
All three constructors together
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int marks;
public:
Student() : name("Unknown"), marks(0) {
cout << "Default constructor: " << name << "\n";
}
Student(string n, int m) : name(n), marks(m) {
cout << "Parameterised constructor: " << name << "\n";
}
Student(const Student &other) : name(other.name + " (copy)"), marks(other.marks) {
cout << "Copy constructor: " << name << "\n";
}
~Student() {
cout << "Destructor: " << name << "\n";
}
void show() {
cout << name << " - " << marks << "\n";
}
};
int main() {
Student s1;
Student s2("Anjali", 88);
Student s3 = s2;
cout << "--- showing ---\n";
s1.show();
s2.show();
s3.show();
cout << "--- main ends ---\n";
return 0;
}
Output:
Default constructor: Unknown
Parameterised constructor: Anjali
Copy constructor: Anjali (copy)
--- showing ---
Unknown - 0
Anjali - 88
Anjali (copy) - 88
--- main ends ---
Destructor: Anjali (copy)
Destructor: Anjali
Destructor: Unknown
The copy constructor here adds " (copy)" to the name only so you can see which object is which in the output. A normal copy constructor copies the name unchanged.
Common mistakes with constructors and destructors
| Mistake | What happens | Fix |
|---|---|---|
| Giving a constructor a return type | void Student() inside the class is a compile error |
Remove the return type |
Student s(); |
Declares a function, not an object | Student s; |
Only a parameterised constructor, then Student s; |
Compile error: no default constructor | Add Student() too |
| Copy constructor taking the object by value | Compile error | Take const Student &other |
new without delete |
Destructor never runs, memory leaks | Pair every new with delete, or use smart pointers later |
| Leaving members unset in a constructor | Garbage values in int and double members |
Set every member, ideally in the initializer list |
Quick revision
- A constructor has the class name, no return type and runs when an object is created.
- Default constructor: no parameters. Parameterised: takes values. Copy: takes
const ClassName &. - Writing any constructor removes the automatic default constructor.
- Prefer the initializer list:
Book(string t, int p) : title(t), pages(p) {}. - The destructor
~ClassName()runs when the object is destroyed. There is only one. - Local objects are destroyed at the end of their block, in reverse order of creation.
Java has constructors too, but no destructors, because its garbage collector frees memory. Compare the two with constructors in Java. For more practice, go through the OOPs interview questions and the C++ interview questions for freshers, where the order of constructor and destructor calls comes up often.
Frequently Asked Questions
What is a constructor in C++ in simple words?
What are the types of constructors in C++?
Can a constructor return a value?
Can a class have more than one constructor?
Can a destructor take parameters or be overloaded?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — Check the Java Full Stack training details

