A constructor initializes member variables of a class and ensures that the class members are set up properly when the object is created.
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.
Depending on their usage and functionality, constructors can be classified into several types:
Default constructor
Parameterized 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.
#include <iostream>using namespace std;class Rectangle {private:int width, height;public:// Default constructorRectangle(){width = 0;height = 0;cout << "Default constructor is called" << endl;}// Display function for rectanglevoid display() const {cout << "Width: " << width << ", Height: " << height << endl;}};int main() {// Default constructorRectangle r1;r1.display();return 0;}
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.
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.
#include <iostream>using namespace std;class Rectangle {private:int width, height;public:// Default constructorRectangle() : width(0), height(0) {cout << "\nDefault constructor is called" << endl;}// Constructor with one parameterRectangle(int w) : width(w), height(0) {cout << "\nSingle-parameter constructor is called" << endl;}// Parameterized constructor with two parametersRectangle(int w, int h) : width(w), height(h) {cout << "\nTwo-parameter constructor is called" << endl;}// Display function for rectanglevoid display() const {cout << "Width: " << width << ", Height: " << height << endl;}};int main() {// Calls default constructorRectangle r1;r1.display();// Calls single-parameter parameterized constructorRectangle r2(5);r2.display();// Calls two-parameter parameterized constructorRectangle r3(6, 3);r3.display();return 0;}
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.
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.
#include <iostream>using namespace std;class Rectangle {private:int width, height;public:// Parameterized constructorRectangle(int w, int h){width = w;height = h;cout << "\nParameterized constructor is called" << endl;}// Copy ConstructorRectangle(const Rectangle& r){width = r.width;height = r.height;cout << "\nCopy constructor is called" << endl;}// Display function for rectanglevoid display() const {cout << "Width: " << width << ", Height: " << height << endl;}};int main() {// Parameterized constructorRectangle r1(5, 3);r1.display();// Copy construcotrRectangle r2(r1);r2.display();return 0;}
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.
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.
#include <iostream>using namespace std;class Rectangle {private:int width, height;public:// Parameterized constructorRectangle(int w, int h){width = w;height = h;cout << "\nMove constructor is called" << endl;}// Move constructorRectangle(Rectangle&& r){width = r.width;height = r.height;r.width = 0;r.height = 0;cout << "\nMove constructor is called" << endl;}// Display function for rectanglevoid display() const {cout << "Width: " << width << ", Height: " << height << endl;}};int main() {// Parameterized constructorRectangle r1(5,3);r1.display();// Move constructorRectangle r2 = move(r1);r2.display();r1.display();return 0;}
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.
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!
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.
Haven’t found what you were looking for? Contact Us
Free Resources