Constructors in C++

A class constructor is a special block of code that is invoked automatically when an object is created. They are used to initialize the values of class variables with default or user-defined values. A constructor name is defined the same as the name of the class and they do not have the return type.

The syntax for defining constructor inside the class body is as follows:


class CLASSNAME
{ 
  //class body
  public :
     CLASSNAME([parameter_list])  // constructor definition
    {
      //constructor body
    }
             
};

Types of Constructors in C++

There are 3 types of constructors in C++:

  • Default Constructor
  • Parameterized Constructor
  • Copy Constructor

Default Constructor

This type of constructor doesn't take any argument. It has no parameters. It is also called a zero-argument constructor.

Example

#include <iostream>
using namespace std;

class Tower
{
public:
        int height;

        // default constructor
        Tower()
        {
                height = 300;
        }
};

int main()
{
        Tower t;
        // default constructor called when object is created
        cout << "Eiffel Tower height is"
             << " " << t.height << " "
             << "metres";

        return 0;
}
Output
Eiffel Tower height is 300 metres

Parameterized Constructor

This type of constructor can take arguments. Typically, these arguments help initialize the values of class variables when an object is created.

Example

#include <iostream>
using namespace std;

class Rectangle
{
private:
        double length;
        double height;

public:
        // parameterized constructor to initialize variables
        Rectangle(double len, double hgt)
        {
                length = len;
                height = hgt;
        }

        double calculateArea()
        {
                return length * height;
        }
};

int main()
{
        // create object and initialize data members
        Rectangle rectangle1(10.5, 8.6);
        Rectangle rectangle2(8.5, 6.3);

        cout << "Area of First Rectangle: " << rectangle1.calculateArea() << endl;
        cout << "Area of Second Rectangle: " << rectangle2.calculateArea();

        return 0;
}
Output
Area of First Rectangle: 90.3
Area of Second Rectangle: 53.55

Default Constructor

This type of constructor creates an object by initializing it with an object of the same class. The copy constructor is used to copy data of one object to another.

Example

#include <iostream>
using namespace std;

class Rectangle
{
private:
        double length;
        double height;

public:
        // initializing variables with parameterized constructor
        Rectangle(double len, double hgt)
        {
                length = len;
                height = hgt;
        }

        // copying constructor with a Rectangle object as parameter
        // copies data of the obj parameter
        Rectangle(Rectangle &obj)
        {
                length = obj.length;
                height = obj.height;
        }

        double calculateArea()
        {
                return length * height;
        }
};

int main()
{
        // creating an object of Rectangle class
        Rectangle rectangle1(7, 9);

        // copying contents of rectangle1 to rectangle2
        Rectangle rectangle2 = rectangle1;

        // printing areas of rectangle1 and rectangle2
        cout << "Area of Rectangle 1: " << rectangle1.calculateArea() << endl;
        cout << "Area of Rectangle 2: " << rectangle2.calculateArea();

        return 0;
}
Output
Area of Rectangle 1: 63
Area of Rectangle 2: 63