Types of Constructors in C++

Constructors in C++:

A constructor is a special member function whose task is to initialize the objects of its class. The constructor name is the same as the class name. It is called a constructor because it constructs the values of data members of the class.

Example:

class test
{
int a, b;
public:
test(void); // constructor declared
......
......
};
test::test(void) // constructor defined
{
a=0;
b=0;
}

Characteristics of Constructor:

1. A constructor should be declared in the public section.
2. Constructors are invoked automatically when the objects are created.
3. The constructor doesn’t have return types, not even void and So, the constructor can’t return values.
4. A constructor can have default arguments.
5. A constructor can’t be inherited.
6. Constructors can’t be virtual.
7. An object with a constructor can’t be used as a member of a union.

Types of Constructors:

There are 4 Types of Constructors in C++:

    • 1.

Parameterized Constructor

    • 2.

Default Constructor

    • 3.

Copy Constructor

    • 4.

Dynamic Constructor

Parameterized Constructor:

It initializes the data members of different objects with different values when they are created. In C++, it allows us to achieve this objective by passing arguments to the constructor function when the objects are created. This constructor can take the arguments that are called Parameterized constructors.
Example:

class test
{
int a,b;
public:
test(int p, int q); // parameterized constructor
.......
.......
};
test::test(int p, int q)
{
a=p;
b=q;
}

Default Constructor:

Default constructors do not have any parameters. If a default constructor isn’t provided by the programmer explicitly, then the compiler provides an implicit default constructor. It has the following syntax:

A::A()

Example:

#include<iostream.h>
using namespace std;
class demo{
private:
int a, b ;
public:
demo() {
a = 25;
b = 10;
}
void display() {
cout<<"Number 1 = "<< a <<endl;
cout<<"Number 2 = "<< b <<endl;
}
};
int main() {
demo d1;
d1.display();
return 0;
}

Output:
Number 1 = 25
Number 2 = 10

Copy Constructor:

A copy constructor is used to declare and initialize an object from another object. A copy constructor takes a reference to an object of the same class as itself as an argument.
Example:

#include<iostream.h>
#include<conio.h>
using namespace std;
class temp
{
int id;
public:
code(){}
code(int a){id=a;}
code(code & x)
{
id=x.id;
}
void display(void)
{
cout<<id;
}
};
int main()
{
code A(50);
code B(A);
code C=A;
code D;
D=A;
cout<<"id of A:"; A.display();
cout<<"id of B:"; B.display();
cout<<"id of C:"; C.display();
cout<<"id of D:"; D.display();
return 0;
}

Output:
id of A: 50
id of B: 50
id of C: 50
id of D: 50

Note: When no copy constructor is defined, the compiler supplies its copy constructor.

Dynamic Constructor:

A constructor can also be used to allocate memory while creating objects. The allocation of memory to objects at the time of their construction is called a Dynamic Constructor. The memory is allocated with the help of a new operator.

Example:

#include<iostream.h>
#include<conio.h>
class sample
{
int *sk;
public:
sample()
{
sk=new int;
*sk=25;
}
sample(int n)
{
sk=new int;
*sk=n;
}
int display()
{
return(*sk);
}
};
void main()
{
clrscr();
sample s1, s2(10);
cout<<"The value of obj1's ptr is:";
cout<<s1.display();
cout<<"\nThe value of obj2's ptr is:"<<s2.display();
getch();
}

Output:
The value of obj1’s ptr is: 100
The value of obj2’s ptr is: 90