Detailed Explanation of Constructors in C++

In C++, a constructor is a special method that is automatically called when an object is created. It is used to initialize the data members of the new object. The constructor in C++ has the same name as the class or structure.

In short, when an object is created in C++, a specific process called the constructor is automatically invoked. Typically, it is used to create the data members of new entities. In C++, the name of the class or structure also serves as the name of the constructor. When the object is completed, the constructor is called. Since it creates values or provides data for entities, it is called a constructor.

The prototype of a constructor is as follows:

<class-name>(parameter-list);

The following syntax is used to define a class constructor:

<class-name>(parameter-list) { // Constructor definition }

The following syntax is used to define a constructor outside the class:

<class-name>::<class-name>(parameter-list) { // Constructor definition }

A constructor does not have a return type because it does not return a value.

There are two types of constructors in C++:

  • Default Constructor

  • Parameterized Constructor

👇 Click to Claim 👇
👉 C Language Knowledge Material Collection

C++ Default Constructor

The constructor without parameters is called a default constructor. It is called when an object is created.

Let’s look at a simple example of a C++ default constructor.

#include <iostream>
using namespace std;
class Employee{
  public:
  Employee()  {
    cout << "Default constructor called" << endl;
  }
};
int main(void){
  Employee e1; // Create an Employee object
  Employee e2;
  return 0;
}

Output:

Default constructor calledDefault constructor called

C++ Parameterized Constructor

A constructor with parameters is called a parameterized constructor. It is used to provide different values for different objects.

Let’s look at a simple example of a C++ parameterized constructor.

#include <iostream>
using namespace std;
class Employee {
  public:
  int id; // Data member (also known as instance variable)
  string name; // Data member (also known as instance variable)
  float salary;
  Employee(int i, string n, float s)  {
    id = i;
    name = n;
    salary = s;
  }
  void display(){
    cout << id << " " << name << " " << salary << endl;
  }
};
int main(void) {
  Employee e1 = Employee(101, "Sonoo", 890000); // Create an Employee object
  Employee e2 = Employee(102, "Nakul", 59000);
  e1.display();
  e2.display();
  return 0;
}

Output:

101 Sonoo 890000102 Nakul 59000

What is the difference between a constructor and a regular member function?

  1. The name of the constructor is the same as the name of the class it belongs to.

  2. The default constructor has no input parameters. However, copy and parameterized constructors can use input parameters.

  3. A constructor has no return type.

  4. A constructor is automatically called when an object is created.

  5. A constructor must be declared in the public area of the class.

  6. If no constructor is specified (with no expected parameters and an empty function body), the C++ compiler will create a default constructor for the object.

Let’s understand the different types of constructors in C++ through a practical example.

Imagine you go to a store to buy a marker. If you want to buy a marker, what are your options?

In the first case, you simply ask the store to give you a marker because you did not specify the brand or color you want, just requesting a quantity. So when we just say, “I just need a marker,” he will give us the most popular marker available in the market or in his store. The default constructor is exactly what it sounds like!

The second method is to go into the store and specify that you want a red XYZ brand marker. Because you mentioned this topic, he will give you that marker. In this case, parameters have already been set. The parameterized constructor is exactly what it sounds like!

The third method requires you to go to the store and state that you want a marker that looks like this (a physical marker in your hand). Thus, the store owner will take note of that marker. When you say okay, he will give you a brand new marker. Hence, copying that marker. This is what the copy constructor does!

What are the characteristics of constructors?

  1. A constructor has the same name as the class it belongs to.

  2. Although it can be, constructors are typically declared in the public part of the class. But this is not mandatory.

  3. Since constructors do not return values, they have no return type.

  4. A constructor is automatically called when creating class objects.

  5. Overloaded constructors can exist.

  6. Constructors cannot be declared as virtual functions.

  7. Constructors cannot be inherited.

  8. The address of a constructor cannot be referenced.

  9. When allocating memory, the constructor implicitly calls the new and delete operators.

What is a copy constructor?

A copy constructor is a member function known as a copy constructor that initializes an object using another object of the same class – a detailed discussion about copy constructors.

When one or more non-default constructors (with parameters) are specified for a class, we also need to include a default constructor (without parameters), because in this case the compiler will not provide one. Best practice is to always declare a default constructor, even if it is not required.

A copy constructor requires a reference to an object of the same class.

Sample(Sample &t)  {
     id = t.id;
}

What is a destructor?

A destructor is a special member function that corresponds to the constructor. The constructor creates the objects of the class, while the destructor is used to destroy those objects. The name of the destructor is the same as the class name, preceded by a tilde (~). Only one destructor can be defined at a time. Using a destructor is a way to destroy the objects created by the constructor. Therefore, destructors cannot be overloaded. Destructors do not accept any parameters and do not return any values. Once the object leaves its scope, the destructor is immediately called. The destructor releases the memory used by the object created by the constructor. The destructor reverses the process of creating an object by destroying it.

The syntax for defining a class destructor is as follows:

~<class-name>(){    // Destructor definition}

The syntax for defining a class destructor outside the class is as follows:

<class-name>::~<class-name>(){    // Destructor definition}

It is important to note that each class can only have one destructor.

Detailed Explanation of Constructors in C++


 Popular Recommendations
  • CLion Tutorial – Project Settings in CLion

  • C Language Algorithm – “Edit Distance” Algorithm Problem

  • C++ Tutorial – Detailed Explanation of Objects and Classes in C++

Leave a Comment