Types of constructors in C++

In C++, constructors are special member functions that initialize objects of a class. They are invoked automatically when an object is created. They are special functions as they don’t have a return type and the name of a constructor is the same as the name of its class.

Key takeaways:

  • Constructors in C++ are special member functions that initialize objects and are automatically invoked when an object is created.

  • Default constructors are basic constructors that don’t take arguments. They initialize data members to default values if the user doesn’t specify any.

  • Parameterized constructors allow the initialization of class members with specific values provided by the user.

  • Copy constructors create a new object by copying an existing object, ensuring that both objects have the same data but are independent entities.

  • Move constructors efficiently transfer resources from one object to another, leaving the original object in an unspecified but valid state to optimize performance in situations involving temporary objects.

Constructors in C++

Depending on their usage and functionality, constructors can be classified into several types:

  1. Default constructor

  2. Parameterized constructor

  3. Copy constructor

  4. Move constructor

1. Default constructor

The default constructor is the most basic constructor that doesn’t take any arguments. Programmers can initialize class member variables with appropriate default values.

Note: The compiler can implicitly declare a default constructor if a class has no constructor. However, such a constructor does not initialize any member variable.

C++ implementation of the default constructor

#include <iostream>
using namespace std;
class Rectangle {
private:
int width, height;
public:
// Default constructor
Rectangle(){
width = 0;
height = 0;
cout << "Default constructor is called" << endl;
}
// Display function for rectangle
void display() const {
cout << "Width: " << width << ", Height: " << height << endl;
}
};
int main() {
// Default constructor
Rectangle r1;
r1.display();
return 0;
}

Explanation

  • Line 4: We create a Rectangle class.

  • Lines 6: We create width and height two private data members of the class.

  • Lines 10–14: We create a default constructor, which initializes the data members with 0 value.

  • Lines 17–19: We create a display() function to display the parameters of the rectangle.

  • Line 24: We create an object r1 of the Rectangle class, which will call the default constructor of the class.

  • Line 25: We call the display() function for the r1 object.

2. Parameterized constructor

A parameterized constructor is a constructor that takes one or more arguments. It initializes the object’s data members with specific values provided via the parameterized constructor. It basically overrides the default constructor to initialize the data members of an object with user-defined values.

C++ implementation of the parameterized constructor

#include <iostream>
using namespace std;
class Rectangle {
private:
int width, height;
public:
// Default constructor
Rectangle() : width(0), height(0) {
cout << "\nDefault constructor is called" << endl;
}
// Constructor with one parameter
Rectangle(int w) : width(w), height(0) {
cout << "\nSingle-parameter constructor is called" << endl;
}
// Parameterized constructor with two parameters
Rectangle(int w, int h) : width(w), height(h) {
cout << "\nTwo-parameter constructor is called" << endl;
}
// Display function for rectangle
void display() const {
cout << "Width: " << width << ", Height: " << height << endl;
}
};
int main() {
// Calls default constructor
Rectangle r1;
r1.display();
// Calls single-parameter parameterized constructor
Rectangle r2(5);
r2.display();
// Calls two-parameter parameterized constructor
Rectangle r3(6, 3);
r3.display();
return 0;
}

Explanation

  • Lines 10–22: We create two parameterized constructors, which initializes the data members with the values provided as a parameter.

  • Line 36: We create an object r2 of the Rectangle class with 5 as a parameter for the parameterized constructor of the class.

  • Line 37: We call the display() function for the r2 object.

  • Line 40: We create an object r3 of the Rectangle class with 6 and 3 as parameters for the parameterized constructor of the class.

  • Line 41: We call the display() function for the r3 object.

3. Copy constructor

A copy constructor is a constructor that creates a new object of a class as a copy of an existing object of the same class. A copy constructor takes a reference to an object of the same class as its argument. It is invoked when an object is passed to a function, returned from a function, or used to initialize another object of the same class.

C++ implementation of the copy constructor

#include <iostream>
using namespace std;
class Rectangle {
private:
int width, height;
public:
// Parameterized constructor
Rectangle(int w, int h){
width = w;
height = h;
cout << "\nParameterized constructor is called" << endl;
}
// Copy Constructor
Rectangle(const Rectangle& r){
width = r.width;
height = r.height;
cout << "\nCopy constructor is called" << endl;
}
// Display function for rectangle
void display() const {
cout << "Width: " << width << ", Height: " << height << endl;
}
};
int main() {
// Parameterized constructor
Rectangle r1(5, 3);
r1.display();
// Copy construcotr
Rectangle r2(r1);
r2.display();
return 0;
}

Explanation

  • Lines 17–21: We create a copy constructor that accepts the reference of the object of the same class as a parameter and copies its value to the current object.

  • Line 31: We create an r1 object of the Rectangle class with width and height equal to 5 and 3, respectively.

  • Line 32: We call the display() function for the r1 object.

  • Line 35: We create an r2 object of the Rectangle class with r1 passed as an argument that will call the copy constructor and copy the values of the r1 object to the r2.

  • Line 36: We call the display() function for the r2 object.

4. Move constructor

A move constructor is a constructor that is used to transfer resources from one object to the other, typically when dealing with temporary objects. It accepts the object of the same class type as an argument and unlike the copy constructor, which duplicates the content, it transfers ownership of resources from one object to another. After the transfer, the source object is usually left in a valid but unspecified state.

C++ implementation of the move constructor

#include <iostream>
using namespace std;
class Rectangle {
private:
int width, height;
public:
// Parameterized constructor
Rectangle(int w, int h){
width = w;
height = h;
cout << "\nMove constructor is called" << endl;
}
// Move constructor
Rectangle(Rectangle&& r){
width = r.width;
height = r.height;
r.width = 0;
r.height = 0;
cout << "\nMove constructor is called" << endl;
}
// Display function for rectangle
void display() const {
cout << "Width: " << width << ", Height: " << height << endl;
}
};
int main() {
// Parameterized constructor
Rectangle r1(5,3);
r1.display();
// Move constructor
Rectangle r2 = move(r1);
r2.display();
r1.display();
return 0;
}

Explanation

  • Lines 17–23: We create a move constructor that accepts the rvalue reference of the object of the same class as a parameter and transfers its ownership to the new object. After the transfer, we set the height and width of the temporary object to 0.

  • Line 33: We create an r1 object of the Rectangle class with width and height equal to 5 and 3, respectively.

  • Line 37: We create an r2 object of the Rectangle class using the move constructor. The move() function is used to convert r1 to an rvalue, that will call the move constructor.

  • Line 38: We call the display() function for the r2 object.

  • Line 39: We call the display() function for the r1 object.

Use cases of the constructors

Each type of constructor serves a specific purpose, enabling efficient and appropriate initialization of objects based on the context and requirements of our program.

  • Default constructor: We use the default constructor to create an object without specific initialization.

  • Parameterized constructor: We use the parameterized constructor when initializing an object with particular values at the creation time.

  • Copy constructor: We use the copy constructor when we need to create a new object that is a duplicate of an existing object.

  • Move constructor: We use the move constructor to optimize performance by transferring resources rather than copying them, especially when dealing with temporary objects or objects that manage dynamic memory or other resources.

Ready to master C++?

Our Learn C++ course will help you build practical skills, from basic loops to complex program structures. Join now and start coding your way to success!

Conclusion

The constructors in C++ are important to facilitate the initialization of objects during their creation. Understanding the types and functionalities of constructors will help developers to effectively manage object creation, setting the foundation for well-structured and maintainable code in object-oriented programming.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is the purpose of a constructor in C++?

A constructor initializes member variables of a class and ensures that the class members are set up properly when the object is created.


What happens if no constructor is defined in a class?

If no constructor is defined, the C++ compiler automatically provides a default constructor that initializes members to default values based on their type.


How does a copy constructor differ from a move constructor?

A copy constructor duplicates the content of an existing object, while a move constructor transfers ownership of resources, leaving the original object in an undefined but valid state.


Can you define multiple constructors in a class?

Yes, C++ supports constructor overloading, meaning you can define multiple constructors with different parameters to allow flexibility in object initialization.


When is the move constructor called?

The move constructor is invoked when an object is initialized with a temporary object or when resources need to be transferred rather than copied.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved