Classes and Objects are a major part of any Object Oriented Language. In order to make them more flexible and introduce more functionality, we have various types of Constructors in C++.

Using these different constructors, we can customize the creation of objects under various circumstances, such as during Assignment, and during Copying.


Types of C++ Constructors

  1. Default Constructor
  2. Null Constructor
  3. Parameterized Constructor
  4. Copy Constructor
  5. Assignment Operator

Default Constructor

In order for an object to be created, the presence of a Constructor is compulsory. But how is it that even if you do not define a Constructor, you can still create an object for that Class?

This is due to the presence of the Default Constructor, placed automatically in every class. It is an empty constructor, with no function body, which exists only to create the object which is the base duty of any constructor.

The Default Constructor is removed if any User-Defined Constructor is created.

class Student {
    string name;
    int age;
};

It may not look like it, but the above Class has a Constructor, which of course, is the Default Constructor.


Null Constructor

A Null constructor is the same as a Default Constructor. It has no function body, and does nothing besides creating the Object. Why do we create this then?

Because if we define a Constructor, the Default Constructor gets deleted. Now let’s say we defined a Class like how we did below, with a Constructor that has a parameter.

#include<iostream>

using namespace std;

class Student {
    string name;
    int age;

    public:
    Student(int x) {
        age = x;
    }
};

int main() {
    Student s1(5);  
}

The problem here, is that we can now only create an object using this Constructor. We cannot create an “empty” object anymore as shown below.

Student s2;    // Error

In certain situations, this is actually nessacery. Such as when using Classes as members in other Classes. Hence, when this need arises, you will have to declare a Null Constructor as shown below.

#include<iostream>

using namespace std;

class Student {
    string name;
    int age;

    public:
    Student(int x) {
        age = x;
    }

    Student() {}
};

int main() {
    Student s1(5);
    Student s2;    // Will work now
}

Parameterized Constructor

This type of constructor is probably the most used type in C++. It’s a constructor which takes in a bunch of parameters as well, typically to only initialize the member variables inside of the object.

Like how we did in the previous example, an example for the Parameterized Constructor is shown below.

    Student(int x) {
        age = x;
    }

Copy Constructor

The copy constructor differs slightly from the other types of Constructor, as it is only called under special circumstances.

As the name implies, it’s “Copy Constructor”, which creates an object by copying values over from a different existing object of the same Class. Henceforth, the parameter in this Constructor is a reference to an object of the same Class.

It’s use is slightly advanced, but a simple example might be where you don’t want certain variable values to be copied over when the Object is duplicated.

#include<iostream>
using namespace std;

class Student {
    string name;
    int age;

    public:
    Student(int x, string s) {
        age = x;
        name = s;
    }
 
    Student(Student &s) {   // Copy Constructor
        name = s.name;
        age = s.age;
    }

    Display() {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
    }
};

int main() {
    Student s1(5, "Bob");

    Student s2(s1);
    //Student s2 = s1

    s2.Display();
}

Object duplication happens more often than you think. For example, when placing objects inside Vector groups, or when Vectors expand themselves, destroying the old objects and creating new ones in a larger Vector.

Don’t confuse the Copy Constructor with the Assignment Constructor. The Copy constructor is only called when an Object is being created. Not when copying values between two already existing Objects.


Assignment Operator

The Assignment Operator is called when “assigning” values to an already existing object, from an already existing objects. Besides this simple difference, it’s use is identical to the Copy Constructor.

It’s not a Constructor, but I felt obligated to add it here, due to it’s importance in the rule of three, and how often it’s used.

#include<iostream>
using namespace std;

class Student {
    string name;
    int age;

    public:
    Student(int x, string s) {
        age = x;
        name = s;
    }

    Student(Student &s) {
        name = s.name;
        age = s.age;
    }

    Student() {}

    Student& operator = (const Student &s)
    {
        cout << "Assignment Operator Called" << endl;
        name = s.name;
        age = s.age;
        return *this;
    }

    Display() {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
    }
};

int main() {
    Student s1(5, "Bob");

    Student s2;
    s2 = s1;

    s2.Display();
}

This marks the end of the C++ Types of Constructors Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Marcello Presulli

A real good, clear and concise summarization.