Learn 3 Types of Constructors in C++ with best examples

In this article, we will go through the understanding of Constructors in C++

Constructors in C++ are special member functions which are created when the object is created or defined and its task is to initialize the object of its class. It is called constructor because it constructs the values of data members of the class.

A constructor has the same name as the class and it doesn’t have any return type. It is invoked whenever an object of its associated class is created.

Learn 3 Types of Constructors in C++ with best examples 1

When a class is instantiated, even if we don’t declare a constructor, compiler automatically creates one for the program. This compiler created constructor is called default constructor.

Constructors

Constructor is a special member function of a class which enables an object of that class to initialize itself when it is created.
Name of the constructor is same as the class name. Hence called special member function.Whenever an object of its associated class is created,
constructor is invoked. We will go through all 3 types of Constructors in C++ with best examples.

Below is the syntax to declare and define constructor:

Class Employee
{
int x, y;
public:
Employee(void);             // constructor declared
…..
…..

Advertisements

};
Employee :: Employee(void)    //  constructor defined
{
x=0;
y=0;
}

Types of constructors

There are 3 types of constructors in c++.

Default Constructor

A constructor that accepts no parameters is called Default Constructor.Compiler supplies a default constructor if no such constructor is defined.

Characteristics of constructor functions are:
a)It should be declared in public scope.

b)It is invoked automatically whenever an object is created.

c)It doesn’t have any return type, not even void. Hence, it can’t return values.

d)It can’t be inherited, though a derived class can call the base class constructor.

e)It can’t be virtual

f)An object with a constructor can’t be used as a member of a union.

Example:

#include <iostream>
using namespace std;

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

int main()
{

Number e;
cout << "a: " << e.a << endl
<< "b: " << e.b;
return 1;
}

Output:

a: 10
b: 20

Parameterized Constructor

The Constructors that can take arguments are called parameterized constructor. Sometimes, it may be necessary to initialize the data members of different
objects with different values when they are created.Hence parameterized constructors are used.

Example:

#include <iostream>
using namespace std;

class Point {
int a, b;

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

int getA()
{
return a;
}
int getB()
{
return b;
}
};
int main()
{
Point p1(5, 15);            // Constructor called
cout << "p1.a = " << p1.getA() << ", p1.b = " << p1.getB();        // Access values assigned by constructor
return 0;
}

Output:

p1.a = 5, p1.b = 15

Copy Constructor

Copy constructor is used for creating a new object as a copy of an existing object.It is a standard approach of copying objects in C++.
It takes reference to an object of the same class as an argument.

Example:

#include <iostream>
using namespace std;

class code
{
int id;
public:
code()
{}
code(int a)
{
id = a;
}
code(code &x)
{
id = x.id;
}
void display(void)
{
cout<< id;
}
};
int main()
{
code A(100);
code B(A);
code C = A;
code D;
D = A;

cout<< "\n id of A:" ; A.display();
cout<< "\n id of B:" ; B.display();
cout<< "\n id of C:" ; C.display();
cout<< "\n id of D:" ; D.display();

}
Output:

id of A:100
id of B:100
id of C:100
id of D:100

For more Posts in C++, Please go through this link.

For more informations about C++, Please go through this link.

Advertisements

Leave a Reply