C++ provides a particular member function called the Constructor, which enables an object to initialize itself at the time of its creation. It is known as the automatic initialization of objects. This concept of C++ also provides another member function called destructor, which destroys the objects when they are no longer required. In this tutorial, you will learn how constructors and destructors work, the types of constructors and how they can be implemented within the C++ program.



What Is Constructor in C++?

The process of creating and deleting objects is a vital task in C++. Every time an instance of a class is created, the constructor method is called. A constructor is a class member function used to initialize the objects of the class. It is treated as a special member function because it has the same name as the class name.

The Constructor is called whenever an object of the respective class is created. It is named "constructor" because it constructs the value of the data members of a class. Initial values ​​can be passed as arguments to the constructor function when the object is declared.

This can be done in two ways:
  • By calling Constructor explicitly
  • By calling Constructor implicitly

The Constructor declaration and definition are as follows:

Syntax:

class ExampleClass {    // Class
public:                 // Access specifier
    ExampleClass() {    // Constructor
        cout << "Sample text.";
    }
};

int main() {
    ExampleClass myObj; // Create an object of ExampleClass (this will call the constructor)
    return 0;
}
Unique characteristics of Constructor:
  • A Constructor is declared in the public section.
  • The Constructor has no return type, not even void.
  • The Constructor is called automatically when the class object is created.
  • The Constructor cannot be inherited, although the derived class can call the base class constructor.
  • Like other functions, constructors can have default arguments.
  • You cannot refer to the address of the Constructor.
  • A Constructor cannot be virtual.

Types of Constructor

C++ offers four types of constructors. These are:
  1. Do Nothing Constructor
  2. Default Constructor
  3. Parameterized Constructor
  4. Copy Constructor

Do Nothing Constructor

Do nothing Constructor is the type of Constructor that does not contain any statement. It takes no arguments and has no return type.

Default Constructor

The default constructor takes no arguments. It has no arguments, but the programmer can write some initialization statements within.

Syntax:

class_name()
{
// Constructor definition
}

Example:

#include <iostream>
using namespace std;

class Calc {
    int val;

public:
    Calc()
    {
        val = 20;
    }
};
int main()
{
    Calc c1;
    cout << c1.val;
}

A default constructor is critical to initialize object members, so the compiler automatically provides a default constructor even if we don't explicitly define the Constructor.

Parameterized Constructor

A default constructor has no parameters, but programmers can add and use parameters within a constructor if required. It helps programmers assign initial values to an object at the creation time.

Example:

#include <iostream>
using namespace std; 

class Calc
{
    int val2;
public:
    Calc(int x)
    {
        val2=x;
    }
};

int main()
{
    Calc c1(10);
    Calc c2(20);
    Calc c3(30);
    cout << c1.val2;
    cout << c2.val2;
    cout << c3.val2;
}

Copy Constructor

C++ provides a particular type of Constructor that takes an object as an argument and is used to copy the values of data members of one object to another. In this case, the copy constructor is used to declare and initialize an object from another object.

Example:

Calc C2(C1);
Or
Calc C2 = C1;

The process of initializing through a copy constructor is called copy initialization.

Syntax:

class-name (class-name &)
{
 . . .
}

Example:

#include <iostream>
using namespace std;

class CopyCon {
    int a, b;

public:
    CopyCon(int x, int y)
    {
        a = x;
        b = y;
        cout << "\nHere is the initialization of Constructor";
    }
    void Display()
    {
        cout << "\nValues : \t" << a << "\t" << b;
    }
};

void main()
{
    CopyCon Object(30, 40);
    //Copy Constructor
    CopyCon Object2 = Object;
    Object.Display();
    Object2.Display();
}

What Is Destructor in C++?

As the name implies, the destructor destroys objects that the Constructor has created within a C++ program. The name of the destructor is the same as the name of the class, except that it has a tilde (~) before its name. It is a good practice to declare the destructor after the Constructor has finished using it.

Here's the basic declaration procedure of a destructor:

Syntax:

~ExampleClass()
{

}

The destructor neither takes an argument nor returns any value, and the compiler implicitly invokes upon the exit from the program to clean up storage that is no longer accessible.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram