Types of constructors in C++

Share

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.

Note: The name of a constructor must be the same as the name of its class.

Constructors in C++

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

Default constructor

The default constructor is the most basic constructor that doesn’t take any arguments. Depending on the data types, it initializes the object’s data members with the appropriate default values.

Note: The compiler implicitly declares a default constructor if a class has no user-defined constructor.

Implementation

#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 5–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.

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.

Implementation

#include <iostream>
using namespace std;
class Rectangle {
private:
int width, height;
public:
// Parameterized constructor
Rectangle(int w, int h){
width = w;
height = h;
cout << "Parameterized 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();
return 0;
}

Explanation

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

  • Line 24: We create an object r1 of the Rectangle class with 5 and 3 as parameters for the parameterized constructor of the class.

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

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.

Implementation

#include <iostream>
using namespace std;
class Rectangle {
private:
int width, height;
public:
// Copy constructor
Rectangle(int w, int h){
width = w;
height = h;
cout << "Copy constructor is called" << endl;
}
// Copy Constructor
Rectangle(const Rectangle& r){
width = r.width;
height = r.height;
cout << "Copy constructor is called" << endl;
}
// Display function for rectangle
void display() const {
cout << "Width: " << width << ", Height: " << height << endl;
}
};
int main() {
// Default constructor
Rectangle r1(5, 3);
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 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 33: We call the display() function for the r2 object.

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.

Implementation

#include <iostream>
using namespace std;
class Rectangle {
private:
int width, height;
public:
// Move constructor
Rectangle(int w, int h){
width = w;
height = h;
cout << "Move constructor is called" << endl;
}
// Move constructor
Rectangle(Rectangle&& r){
width = r.width;
height = r.height;
r.width = 0;
r.height = 0;
cout << "Move constructor is called" << endl;
}
// Display function for rectangle
void display() const {
cout << "Width: " << width << ", Height: " << height << endl;
}
};
int main() {
Rectangle r1(5,3);
// 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 32: We create an r1 object of the Rectangle class with width and height equal to 5 and 3, respectively.

  • Line 34: 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 35: We call the display() function for the r2 object.

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

Constructor use case

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 or when the default state is sufficient.

  • 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, ensuring that any necessary deep copy logic is applied.

  • 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.

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.

Copyright ©2024 Educative, Inc. All rights reserved