top of page

What are Constructors and Destructors in C++?

Constructors in C++

Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object.

Whereas, Destructor on the other hand is used to destroy the class object.

Let's start with Constructors first, following is the syntax of defining a constructor function in a class:

class A{
    int x;// constructorA(){// object initialization}};

While defining a contructor you must remeber that the name of constructor will be same as the name of the class, and contructors will never have a return type.

Constructors can be defined either inside the class definition or outside class definition using class name and scope resolution :: operator.

class A{
    int i;A(); // constructor declared};// constructor definition
    i = 1;}

Types of Constructors in C++

Constructors are of three types:

  1. Default Constructor

  2. Parametrized Constructor

  3. Copy COnstructor

Default Constructors

Default constructor is the constructor which doesn't take any argument. It has no parameter.


class_name(parameter1, parameter2, ...){// constructor Definition }

For example:

class Cube{
    int side;Cube(){
        side = 10;}};

int main(){
    Cube c;
    cout << c.side;}



In this case, as soon as the object is created the constructor is called which initializes its data members.

A default constructor is so important for initialization of object members, that even if we do not define a constructor explicitly, the compiler will provide a default constructor implicitly.

class Cube{
    int side;};

int main(){
    Cube c;
    cout << c.side;}


 0 or any random value 

In this case, default constructor provided by the compiler will be called which will initialize the object data members to default value, that will be 0 or any random integer value in this case.

Parameterized Constructors

These are the constructors with parameter. Using this Constructor you can provide different values to data members of different objects, by passing the appropriate values as argument.

For example:

class Cube{
    int side;Cube(int x){

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


 10 20 30 

By using parameterized construcor in above case, we have initialized 3 objects with user defined values. We can have any number of parameters in a constructor.

Copy Constructors

These are special type of Constructors which takes an object as argument, and is used to copy values of data members of one object into other object. We will study copy constructors in detail later.

Destructors in C++

Destructor is a special class function which destroys the object as soon as the scope of object ends. The destructor is called automatically by the compiler when the object goes out of scope.

The syntax for destructor is same as that for the constructor, the class name is used for the name of destructor, with a tilde ~ sign as prefix to it.

class A{
    // defining destructor for class
        // statement

Destructors will never have any arguments.

Source: studytonight

The Tech Platform



bottom of page