C++ Constructors

In C++, a constructor is a special member function of a class that is executed whenever an object of that class is created. Constructors are used to initialize the member variables of the class and allocate memory for the object. They are also used to set default values for the object’s properties. In this article, we will discuss the basics of constructors in C++, including the different types of constructors and how to use them in your code.

Default Constructors

A default constructor is a constructor that takes no arguments. It is automatically generated by the compiler if no other constructors are defined in the class. The default constructor initializes all member variables of the class to their default values, which are:

  • 0 for numeric data types (int, float, double, etc.)
  • false for boolean data types
  • null or empty for string and pointer data types

Here’s an example of a class with a default constructor:

class MyClass {
public:
    int myInt;
    string myString;
    MyClass() {
        // default constructor
    }
};

In this example, the default constructor for the MyClass class initializes the myInt variable to 0 and the myString variable to an empty string. We can create an object of the MyClass class and access its member variables like this:

MyClass myObject;
cout << myObject.myInt << endl;   // Output: 0
cout << myObject.myString << endl; // Output:

Parameterized Constructors

A parameterized constructor is a constructor that takes one or more arguments. It can be used to initialize the member variables of the class with specific values when the object is created. The syntax for creating a parameterized constructor is as follows:

class MyClass {
public:
    int myInt;
    string myString;
    MyClass(int i, string s) {
        myInt = i;
        myString = s;
    }
};

In this example, the MyClass class has a parameterized constructor that takes an integer and a string as arguments. The constructor initializes the myInt and myString member variables with the values of the i and s arguments, respectively. We can create an object of the MyClass class and pass in arguments to the constructor like this:

MyClass myObject(5, "Hello");
cout << myObject.myInt << endl;   // Output: 5
cout << myObject.myString << endl; // Output: Hello

A constructor can also have default values for its parameters, this way if the user does not provide any value for the parameter, the default value is used.

class MyClass {
public:
    int myInt;
    string myString;
    MyClass(int i = 0, string s = "") {
        myInt = i;
        myString = s;
    }
};

In this example, the MyClass class has a parameterized constructor that takes an integer and a string as arguments, but with default values of 0 and “” respectively. This means that if the user does not provide any values for the constructor, the myInt variable will be initialized to 0 and the myString variable will be initialized to an empty string. Here’s an example of how to use this constructor:

MyClass myObject;  // Using the default values
cout << myObject.myInt << endl;   // Output: 0
cout << myObject.myString << endl; // Output: 

MyClass myObject2(5, "Hello");  // Providing values to the constructor
cout << myObject2.myInt << endl;   // Output: 5
cout << myObject2.myString << endl; // Output: Hello

Copy Constructors

A copy constructor is a constructor that takes an object of the same class as its argument and creates a new object with the same values as the original object. The syntax for creating a copy constructor is as follows:

class MyClass {
public:
    int myInt;
    string myString;
    MyClass(const MyClass &obj) {
        myInt = obj.myInt;
        myString = obj.myString;
    }
};

In this example, the MyClass class has a copy constructor that takes a constant reference to an object of the MyClass class as its argument. The constructor initializes the myInt and myString member variables with the values of the obj.myInt and obj.myString member variables of the original object, respectively. We can create a new object of the MyClass class and initialize it with the values of an existing object like this:

MyClass myObject(5, "Hello");
MyClass myObjectCopy(myObject);
cout << myObjectCopy.myInt << endl;   // Output: 5
cout << myObjectCopy.myString << endl; // Output: Hello

It’s important to note that when you create a new object and initialize it with the values of an existing object using a copy constructor, the new object and the original object are two separate objects with two separate memory locations. Any changes made to the member variables of the new object will not affect the member variables of the original object.

Move Constructor

A move constructor is a constructor that takes a rvalue reference of an object as its argument and “moves” the resources of the original object to the new object. The syntax for creating a move constructor is as follows:

class MyClass {
public:
    int myInt;
    string myString;
    MyClass(MyClass &&obj) {
        myInt = obj.myInt;
        myString = obj.myString;
    }
};

In this example, the MyClass class has a move constructor that takes a rvalue reference of an object of the MyClass class as its argument. The constructor initializes the myInt and myString member variables with the values of the obj.myInt and obj.myString member variables of the original object, respectively. We can create a new object of the MyClass class and move the resources of an existing object to the new object like this:

MyClass myObject(5, "Hello");
MyClass myObjectMove(std::move(myObject));
cout << myObjectMove.myInt << endl;   // Output: 5
cout << myObjectMove.myString << endl; // Output: Hello

It’s important to note that when you create a new object and move the resources of an existing object using a move constructor, the original object is left in an unspecified but valid state and should not be used after that. The new object takes over the resources of the original object and is now responsible for managing and releasing them. This is useful in situations where you want to transfer ownership of resources to a new object without incurring the cost of copying them.

Conclusion

In this article, we have learned about the different types of constructors in C++, including default constructors, parameterized constructors, copy constructors, and move constructors. We have also seen examples of how to use these constructors to initialize objects of a class with different values. Default constructors are used when no values are provided to initialize an object, while parameterized constructors allow the user to provide values to initialize an object. Copy constructors are used to create a new object with the same values as an existing object, while move constructors are used to “move” the resources of an existing object to a new object. Understanding the use and implementation of these different types of constructors is important for effective object-oriented programming in C++.

Leave a Reply

Your email address will not be published. Required fields are marked *