Constructor in C++

Constructor in C++

When an object is created in C++, a specific function called the constructor is immediately called. In general, it is utilised to initialise new objects' data members. In C++, the class or structure name serves as the name of the constructor. When creating an object, the constructor is called. It creates the values, i.e., supplies the object with data, which is why it is referred to as a constructor.

Characteristics of the constructor:

  • The name of the constructor is the same as its class name.

  • Constructors are mostly declared in the public section of the class though it can be declared in the private section of the class.

  • Constructors do not return values; hence they do not have a return type.

  • A constructor gets called automatically when we create the object of the class.

  • Constructors can be overloaded.

  • Constructor can not be declared virtual.

  • Constructor cannot be inherited.

  • Addresses of Constructor cannot be referred.

  • Constructor make implicit calls to new and delete operators during memory allocation.

Types Of Constructors :

  1. Default constructor

  2. Parameterized constructor

  3. Copy constructor

1. Default Constructor : Default constructor is the constructor which doesn’t take any argument. It has no parameters. It is also called a zero-argument constructor.

#include <iostream>
using namespace std;

class construct {
public:
    int a, b;

    // Default Constructor
    construct()
    {
        a = 10;
        b = 20;
    }
};

int main()
{
    // Default constructor called automatically
    // when the object is created
    construct c;
    cout << "a: " << c.a << endl << "b: " << c.b;
    return 1;
}
Output
a: 10
b: 20

2.Parameterized Constructors: Constructors allow the passing of arguments. These parameters often aid in initialising an object when it is formed. Simply add parameters to it as you would to any other function to create a parameterized constructor. You should initialise the object using the arguments when defining the constructor's body.

#include <iostream>
using namespace std;

class Point {
private:
    int x, y;

public:
    // Parameterized Constructor
    Point(int x1, int y1)
    {
        x = x1;
        y = y1;
    }

    int getX() { return x; }
    int getY() { return y; }
};

int main()
{
    // Constructor called
    Point p1(10, 15);

    // Access values assigned by constructor
    cout << "p1.x = " << p1.getX()
         << ", p1.y = " << p1.getY();

    return 0;
}
Output
p1.x = 10, p1.y = 15

When an object is declared in a parameterized constructor, the initial values have to be passed as arguments to the constructor function. The normal way of object declaration may not work. The constructors can be called explicitly or implicitly.

3.Copy Constructor: A copy constructor is a member function that uses another object from the same class to initialise a new object. a thorough discussion of Copy Constructor.

A default constructor (without arguments) should always be explicitly declared if one or more non-default constructors (with parameters) are specified for a class, as the compiler won't supply one in this circumstance. Even though it is not required, it is recommended that you always create a default constructor.

#include<iostream>
using namespace std;

class Sample
{          int id;
    public:
    void init(int x)
    {
        id=x;   
    }   
    void display()
    {
        cout<<endl<<"ID="<<id;
    }
};

int main()
{
    Sample obj1;
    obj1.init(10);
    obj1.display();

    Sample obj2(obj1); //or obj2=obj1;
    obj2.display();
    return 0;
}
Output
ID=10
ID=10

When is the copy constructor called?

In C++, a Copy Constructor may be called in the following cases:

  • When an object of the class is returned by value.

  • When an object of the class is passed (to a function) by value as an argument.

  • When an object is constructed based on another object of the same class.

  • When the compiler generates a temporary object.

We appreciate you reading our blog, and we hope to welcome you again soon for more interesting posts. We appreciate your participation in our community and look forward to hearing from you.