Types of Constructors in C++

Constructors in C++

  • A constructor is a member function of a class which initializes objects of a class.
  • The Compiler calls the Constructor whenever an object is created.
  • It is special member function of the class.
  • Constructors initialize values to object members after storage is allocated to the object.
  • Compiler identifies a given member function is a constructor by its name and the return type.
  • Constructors are always public.

Difference between constructor and member function

  • Constructor name must be same as class name but functions cannot have same name as class name.
  • Constructors does not have return type whereas functions must have return type.
  • Constructors is automatically called when an object is created.
  • Member function can be virtual, but there is no concept of virtual constructors.
  • Constructors are invoked at the time of object creation automatically and cannot be called explicitly using class objects.

Type of Constructors in C++

types of constructor

Default Constructor in C++

  • Default constructor is the constructor which doesn’t take any argument. It has no parameters.
  • 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.
Output

a : 10
b : 20
sum : 30
#include <iostream> 
using namespace std;

class construct {
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};

int main()
{
construct c;
int sum = c.a + c.b;
cout << "a : " << c.a << endl;
cout << "b : " << c.b << endl;
cout << "sum : " << sum << endl;
return 1;
}
Output

a : 10
b : 20
sum : 30

Parameterised Constructor in C++

 

  • Arguments can be passed to parameterised constructor.
  • These arguments help initialize an object when it is created.
  • To create a parameterized constructor, simply add parameters to it the way you would to any other function.
  • When you define the constructor’s body, use the parameters to initialize the object.

we can also have more than one constructor in a class and that concept is called constructor overloading.

Uses of Parameterized constructor:

  • It is used to initialize the various data elements of different objects with different values when they are created.
  • It is used to overload constructors.
Output

a = 10, b = 15
#include <iostream> 
using namespace std;

class PrepInsta {
private:
int a, b;

public:
PrepInsta(int a1, int b1)
{
a = a1;
b = b1;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
};

int main()
{
PrepInsta p1(10, 15);
cout << "a = " << p1.getA() << ", b = " << p1.getB();
return 0;
}
Output 
a = 10, b = 15

Copy Constructor in C++

  • A copy constructor is a member function which initializes an object using another object of the same class.
  • Whenever we define one or more non-default constructors( with parameters ) for a class, a default constructor( without parameters ) should also be explicitly defined as the compiler will not provide a default constructor in this case.
  • An object can be initialized with another object of same type. This is same as copying the contents of a class to another class.
  • For a better understanding of Copy Constructor in C++ (click here).