C++ Constructors: Default, Copy, Move & Initialization
Module 14: Constructors in C++
A constructor is a special member function that is automatically called when an object is created. Constructors initialize the object's data members and prepare it for use.
Characteristics of Constructors
- Has the same name as the class.
- Does not have a return type.
- Called automatically when an object is created.
- Can be overloaded.
- Helps initialize object data.
1. Default Constructor
A default constructor takes no parameters. If you don't define any constructor, the compiler generates a default constructor automatically.
Syntax
1class Student 2{ 3public: 4 Student() 5 { 6 // Initialization 7 } 8};
Example
1#include <iostream> 2 3using namespace std; 4 5class Student 6{ 7public: 8 Student() 9 { 10 cout << "Default Constructor Called"; 11 } 12}; 13 14int main() 15{ 16 Student s; 17 18 return 0; 19}
Output
1Default Constructor Called
2. Parameterized Constructor
A parameterized constructor accepts one or more arguments to initialize object members.
Example
1#include <iostream> 2 3using namespace std; 4 5class Student 6{ 7public: 8 string name; 9 int age; 10 11 Student(string n, int a) 12 { 13 name = n; 14 age = a; 15 } 16}; 17 18int main() 19{ 20 Student s("Ankit", 22); 21 22 cout << s.name << endl; 23 cout << s.age; 24 25 return 0; 26}
Output
1Ankit 222
3. Copy Constructor
A copy constructor creates a new object by copying an existing object.
Syntax
1ClassName(const ClassName& other);
Example
1#include <iostream> 2 3using namespace std; 4 5class Student 6{ 7public: 8 string name; 9 10 Student(string n) 11 { 12 name = n; 13 } 14 15 Student(const Student& other) 16 { 17 name = other.name; 18 } 19}; 20 21int main() 22{ 23 Student s1("Ankit"); 24 25 Student s2 = s1; 26 27 cout << s2.name; 28 29 return 0; 30}
Output
1Ankit
When is a Copy Constructor Used?
- Passing objects by value.
- Returning objects from functions.
- Creating one object from another.
4. Move Constructor (C++11)
A move constructor transfers ownership of resources from one object to another instead of copying them. It improves performance by avoiding unnecessary copies.
Syntax
1ClassName(ClassName&& other);
Example
1#include <iostream> 2#include <utility> 3 4using namespace std; 5 6class Student 7{ 8public: 9 string name; 10 11 Student(string n) 12 { 13 name = n; 14 } 15 16 Student(Student&& other) 17 { 18 name = move(other.name); 19 } 20}; 21 22int main() 23{ 24 Student s1("Ankit"); 25 26 Student s2(move(s1)); 27 28 cout << s2.name; 29 30 return 0; 31}
Output
1Ankit
Note: Move constructors are especially useful when working with dynamic memory, containers, and large objects.
5. Delegating Constructor (C++11)
A delegating constructor calls another constructor in the same class, reducing code duplication.
Example
1#include <iostream> 2 3using namespace std; 4 5class Student 6{ 7public: 8 string name; 9 int age; 10 11 Student() 12 : Student("Unknown", 0) 13 { 14 } 15 16 Student(string n, int a) 17 { 18 name = n; 19 age = a; 20 } 21}; 22 23int main() 24{ 25 Student s; 26 27 cout << s.name << endl; 28 cout << s.age; 29 30 return 0; 31}
Output
1Unknown 20
6. Constructor Initialization List
An initialization list initializes data members before the constructor body executes. It is the preferred way to initialize members.
Syntax
1ClassName() : member(value) 2{ 3}
Example
1#include <iostream> 2 3using namespace std; 4 5class Student 6{ 7private: 8 string name; 9 int age; 10 11public: 12 Student(string n, int a) 13 : name(n), age(a) 14 { 15 } 16 17 void display() 18 { 19 cout << name << endl; 20 cout << age; 21 } 22}; 23 24int main() 25{ 26 Student s("Ankit", 22); 27 28 s.display(); 29 30 return 0; 31}
Output
1Ankit 222
Initialization lists are required for initializing:
constdata members- Reference members
- Base classes
- Objects without default constructors
Types of Constructors
| Constructor | Purpose |
|---|---|
| Default Constructor | Initializes objects without arguments |
| Parameterized Constructor | Initializes objects using parameters |
| Copy Constructor | Creates a copy of another object |
| Move Constructor | Transfers ownership of resources |
| Delegating Constructor | Calls another constructor in the same class |
| Initialization List | Efficiently initializes data members |
Best Practices
- Use initialization lists instead of assigning values inside the constructor body.
- Pass large objects by
constreference in copy constructors. - Implement move constructors for classes that manage resources.
- Avoid unnecessary copying by using move semantics where appropriate.
- Keep constructors focused on object initialization.
Practice Problems
- Create a class with a default constructor.
- Create a parameterized constructor for an
Employeeclass. - Implement a copy constructor.
- Implement a move constructor using
std::move. - Create a delegating constructor.
- Initialize
constand reference members using an initialization list.
Interview Questions
- What is a constructor in C++?
- What is the difference between a default and parameterized constructor?
- When is a copy constructor called?
- What is a move constructor?
- What are the benefits of move semantics?
- What is a delegating constructor?
- What is a constructor initialization list?
- Why are initialization lists preferred over assignment inside constructors?
- Which members must be initialized using an initialization list?
Summary
In this module, you learned:
- What constructors are and how they initialize objects.
- How default and parameterized constructors work.
- How copy constructors create copies of objects.
- How move constructors improve performance by transferring resources.
- How delegating constructors reduce duplicate initialization code.
- Why constructor initialization lists are the preferred way to initialize members.
Next Module: Module 15: Destructors and Resource Management — Destructors, Rule of Three, Rule of Five, Rule of Zero, Resource Ownership, Copy Assignment Operator, and Move Assignment Operator.