Complete Guide to Constructors in C++ with Practical Examples
A beginner-friendly guide to C++ constructors with code examples, memory management concepts, and object lifecycle explanations.
Constructors in C++ Explained: Default, Parameterized, Copy, Move Constructors, Deep Copy & Rule of Five
Photo by Enchanted Tools on Unsplash
Constructors are one of the most important concepts in Object-Oriented Programming. They are responsible for initializing objects and managing resources. In this article, we will learn how constructors work in C++, explore different types of constructors, understand deep copy vs shallow copy, and discuss the Rule of Three and Rule of Five with practical examples.
what you will learn in this Article
- What is constructor
- Types of constructor in c++
- How each type of constructors works in c++

figure 1
A constructor is a special member function of a class that is automatically called when an object is created , and name of this function should be same as the name of the class, constructordoes not return anything. in figure 1 you can see that i have defined a class and name of the class is Student. in this code i haven’t created any constructor and i didn’t assigned value to name but you can see that a randon value appears when i print the name. so the question is who is assiging the value. so the answer is in c++ , when we don’t defined our constructor , so c++ compiler define a constructor for us and attribute get some random value because they never get initialized.

figure 2
In this figure 2 you can clearly see that i have defined a contrutor.
what we have learned till now
- constructor is a function and name of this function is same as the name of the class.
- we do not need to call the constructor , when we call the make an instance of the class, constructor automatically executes.
- if we don’t define a constructor ourselves, the compiler may generate a default constructor for us. Primitive data members are not automatically initialized.
- Constructors do not have a return type, not even
void.
TIME TO MOVE TO NEXT PART
2. TYPES OF THE CONSTRUCTOR
so there are four type of constructor in c++;
- Default constructor
- Parameterized constructor
- copy constructor
- Move constructor (included in c++11)
Now we understand the constructor one by one lets start with the default constructor.
1. Default constructor
most of the things about the default constructor , i have already told you . just one thing i forget to tell you when we define a constructor then it as a override to the constructor created by the c++ compiler.
2. Parameterized Constructor
in parameterized constructor , we define the constructor and we pass the argument to that constructor here is the example
EXAMPLE

figure 3
in the example you can see that i have created a class named Student, and i have also created a Default Constructor and a Parameterized constructor. this is the syntax and you can remember the syntax or you can always refer to official website. let’s come back to the point at the time i have created an instance of the classs at that point our constructor is called. you can see that i have defined two constructor but only one constructor is called. and that is true you can make many constructor as you want, this is also known as constructor overloading. here is the example for you
EXAMPLE

figure 4
Constructor Overloading
you can see that i have defined many constructor. first constructor is called when s1 is created and first parametarized constructor is called when s2 is created and so on.. see the output clearly. lets jump into the another question what if i want to call the multiple constructor for a single instance let see how we can do that . we can do this using the concept constructor chaining here is the example of constructor chaining. using one constructor we can call another constructor and from another we can call another. hence we can chain multiple constructor. it is similar to chain , in real life.
EXAMPLE

figure 5
you can see that i have chained the constructor , for constructor chaining i have use the concept intialization list you read more about this in this article. one more this we can chain multiple constructor together.
EXAMPLE
#include<iostream>
using namespace std;
class Student {
string name ;
int age;
Student(){
cout<<"defaultConstructor is called"<<endl;
};
// this not the example of constructor chaining.
Student(string name, int age){
Student();
this->name= name ;
this->age=age;
}
};
now it’s time to learn most important constructor
3. Copy Constructor
now its time to learn about the copy constructor. using this this we can understand about the copy constructor and here is the example
as you can see that i have created a student s1 then I have create a new student S2.

figure 6.
Student s2=S1
but as you can see that i did not pass any value for the second student. a quetion may be arise in your mind that we didn’t give the name to S2 so what will be the name of S2. in this case a copy constructor start working it create the copy of values of s1 and like name , age, and initialise the value. and the fun part is we do not to do anything , c++ compiler create default copy constructor for us. but now we are going to override the default copy constructor and make our copy lets see the exaple.
EXAMPLE
#include<bits/stdc++.h>
using namespace std;
class Student{
public:
string name ;
int age;
vector<string> subject;
// constructor
Student (string name, int age, vector<string> subject) {
this->name = name;
this->age = age;
this->subject = subject;
}
// copy constructor
Student(const Student &s){
this->name = "deepak";
this->age = 22;
this->subject = {"C++", "Java"};
}
};
int main(){
Student s1("John", 20, {"Math", "Physics"});
cout<<"name of s1 is "<<s1.name<<endl;
cout<<"age of s1 is "<<s1.age<<endl;
cout<<"s1 subjects are "<<endl;
for(const string &sub: s1.subject){
cout<<sub<<endl;
}
cout<<endl;
Student s2= s1; // copy constructor is called
cout<<"s2 is created using copy constructor "<<endl;
cout<<"s2 name is "<<s2.name<<endl;
cout<<"s2 age is "<<s2.age<<endl;
cout<<"s2 subjects are "<<endl;
for(const string &sub: s2.subject){
cout<<sub<<endl;
}
return 0;
}
as you can see that i have created a copy constructor. in the copy constructor i have pass the S1 but i did not utilise i just want to show that we can do whatever we want, when the copy constructor is called then it will show show my instead of passed object name. for creating the exact copy we can use this code.
EXAMPLE
Student(const Student &s){
this->name = s.name;
this->age = s.age;
this->subject = s.subject;
}
you can defined the copy constructor in two type.
1. Shallow copy
2. Deep copy
when we don’t defined the copy constructor and we try to copy the copy at that time c++ compile create a shallow copy constructor. so why do we need of deep copy. if shallow copy is working . shallow copy is fine of when we are dealing with static memory allocation. but when we start working with dynamic memory allocation then output of shallow copy didn’t matxh with expected. lets understand with an example.
EXAMPLE
#include<iostream>
using namespace std;
class Student{
public:
string name ;
int age;
// constructor
Student () {
this->name = "deepak";
this->age = 22;
}
//default copy constructor
};
int main(){
Student s1;
Student s2= s1; // copy constructor is called
cout<<"s1 name is "<<s1.name<<endl;
cout<<"s2 name is "<<s2.name<<endl;
return 0;
}
s1 name is deepak
s2 name is deepak
you can i have created a student s1 then i assign s1=s2. but i did not explicitly defined a copy. here c++ compiler define a copy constructor of use. this is default copy constructor and it is shallow copy. as you already know that we can override this function and make our own function. we will not go on that side. but we will see what is the problem with this shallow copy when we work pointers. lets see and example.

figure 7
in this example you can see that i have changed the age of s2 but age of s1 is also change . but we never changes that age of s1. so this is problem with shallow copy . when we use pointer then in the shallow copy it does not create a new memory location but it directly point the same memory location. means there are two pointer pointing to one memory location. thats the reason when we changed the age of s2 , age of s1 also changes. so what is the solution of this problem , and the solution is DEEP copy.
Student(const Student &s){
this->name = s.name;
this->age = new int(*s.age); // deep copy
}
we just need to assigned the new memory location using ‘new’ keyword in the copy constructor. this is know as deep copy. one more that need to be remember that c++ does not have inbuilt garbage collection. so when we assigned the memory using dyanmic memory allocation then it is the responsibility of programer that it should be freed you can do this in the destructor. here is the example.
~Student(){
cout<<"Destructor is called for "<<name<<endl;
delete age;
}
destructor is used to free up the memory . it name is same as the name of class with starting with ‘~’ this sign. iska use karke hum log memory leak se bach sakte hai.
before moving to next topic that this move constructor , lets understand one more topic.
Rule of three
Rule of three states that if you defined any one of three (copy constructor, destructor,copy assignment operator) then we should defined all three. lets see an example why we need copy assignment operator.
#include<iostream>
using namespace std;
class Student{
public:
string name ;
int* age;
Student (string name, int age) {
this->name = name;
this->age = new int(age);
}
// copy constructor
Student(const Student &s){
this->name = s.name;
this->age = new int(*s.age); // deep copy
}
void display(){
cout<<"name is "<<name<<endl;
cout<<"age is "<<*age<<endl;
}
~Student(){
cout<<"Destructor is called for "<<name<<endl;
delete age;
}
};
int main(){
Student s1("John", 20);
Student s2(s1); // copy constructor is called
Student s3 = s1; // copy constructor is called
return 0;
}
output_window
PS X:\c++> g++ .\copy_constructor.cpp -o output
PS X:\c++> .\output.exe
Destructor is called for John
Destructor is called for John
Destructor is called for John
from the above example you can see that eveything working fine. In the main function you can see that Student s2(s1) is same as Student s2=s1; lets change the code slightly.
Student s4;
s4 = s1;
this change the understanding of the program.
Student s1("Deepak", 20);
//case 1
Student s2 = s1; // Copy Constructor
Student s3(s1); // copy constructor
//case 2
Student s4;
s4 = s1; // Copy Assignment Operator
from the above examlpe, case 1 is not same as case 2. int a=10; is defined in c++ , and it is a default datatype. but Student is made by us so compiler does not what to do when s4=s1; this case use compiler invode a default copy assignment operator. here is the example of copy assigment operator.
Student& operator=(const Student &s){
if(this ==&s){
return *this;
}
cout<<"Copy assignment operator is called "<<endl;
this->name = s.name;
this->age = s.age; // shallow copy
return *this;
}
Copy assignment operator is called
you may be thinging that what is the problem with , everything is working fine. yes , i agree with you here everything is working fine because we are dealing with static memory allocation . but when we work with pointer then will create an issue. it start copying the address instead of value. hence two pointers points the same memory location , when we modifies the one pointer then other will also get modifined. for this we need to perform deep here. by default shallow copy is created shallow copy is good of static memory allocation but it is not good for dynamic memory allcation. for making the program behaivour correct we has to defined deep copy.
lets move to last type of constructor in c++ ;
MOVE CONSTRUCTOR
Move constructor is used for moving / transferring resources from one object to another object without compying it. lets see how we can define a move constructor. here is the example
EXAMPLE
class Student {
private:
int* ptr;
public:
// Move Constructor
Student(int val){
ptr = new int(val);
}
Student(Student&& obj) {
cout << "Move Constructor called\n";
// Steal the pointer
ptr = obj.ptr;
obj.ptr = nullptr;
}
};
int main() {
Student obj1(42);
// Move constructor is called
Student obj2 = std::move(obj1);
return 0;
}
This is the syntax of defining the move constractor in c++. As you can see that we have pointer . we have created a one object obj1 and then we move resources one object to another obj2. here is the complete code for practice and its output.
#include <iostream>
using namespace std;
class Student {
private:
int* ptr;
public:
// Constructor
Student(int value) {
// Dynamically allocate memory
ptr = new int(value);
cout << "Constructor called\n";
}
// Move Constructor
Student(Student&& obj) {
cout << "Move Constructor called\n";
// Steal the pointer
ptr = obj.ptr;
obj.ptr = nullptr;
}
// Destructor
~Student() {
if (ptr != nullptr) {
cout << "Destructor deleting data: " << *ptr << endl;
} else {
cout << "Destructor called on nullptr\n";
}
delete ptr;
}
// Display function
void display() {
if (ptr)
cout << "Value: " << *ptr << endl;
else
cout << "No data\n";
}
};
int main() {
// Constructor is called
Student obj1(42);
// Move constructor is called
Student obj2 = std::move(obj1);
cout << "\nAfter move:\n";
cout << "obj1: ";
// Should show "No data"
obj1.display();
cout << "obj2: ";
// Should show "Value: 42"
obj2.display();
return 0;
}
OUTPUT
PS X:\c++> g++ .\moveConstructor.cpp -o output
PS X:\c++> .\output.exe
Constructor called
Move Constructor called
After move:
obj1: No data
obj2: Value: 42
Destructor deleting data: 42
Destructor called on nullptr
PS X:\c++>
move constructor is done. we should study one more topic.
RULE OF FIVE
IT is a extension of rule of three. it says that if you define any one of five then you have define all. Destructor Copy Constructor Copy Assignment Operator Move Constructor Move Assignement Operator.
for Now this the end of this article we have covered most the things about the constructor(approx 90%). rest of the things we cover later.
if you want to study more about move constructor you can refer this article.
In this article, we explored:
Default Constructor Parameterized Constructor Constructor Overloading Constructor Chaining Copy Constructor Shallow Copy vs Deep Copy Move Constructor Rule of Three Rule of Five
메타데이터
- post_id
- 61af548bebdd
- slug
- complete-guide-to-constructors-in-c-with-practical-examples-61af548bebdd
- url
- https://medium.com/@kumardeeepak392/complete-guide-to-constructors-in-c-with-practical-examples-61af548bebdd
- canonical_url
- https://medium.com/@kumardeeepak392/complete-guide-to-constructors-in-c-with-practical-examples-61af548bebdd
- author_url
- https://medium.com/@kumardeeepak392
- status
- ok
- fetched_at
- 2026-08-02 13:24:08