Introduction to Constructor
- Constructor is special member function of a class that initializes an instance of its class.
- It has the same name as the class name and does not have return value, not even void.
- It can have any number of parameters and a class may have any number of overloaded constructors.
- Constructors may have any accessibility, public, private or protected.
- Constructors can be defined either inside or outside class definition.
- Constructors can be defined outside the class definition using class name and scope resolution (::) operator.
Example
class employee
{
private:
//Employee details;
public:
employee(); //Constructor
};
Example
class employee
{
private:
//Employee Details
public:
employee(); //Constructor declared
};
employee::employee() //Constructor definition outside class definition
{
//Statements;
}
Types of Constructors
Following are the types of constructors:1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
1. Default Constructor
- Default constructor does not have any parameter.
- This type of constructor is important for initialization of object members.
{
//Constructor Definition
}
Example : Demonstrating the Default Constructor
#include<iostream>
using namespace std;
class Rectangle
{
public:
float l,b;
Rectangle() //Constructor created
{
l=3;
b=6;
}
};
int main()
{
Rectangle rect;
cout <<"Area of Rectangle : "<<rect.l*rect.b;
}
Output:
Area of Rectangle : 18
In the above program, first object rect belonging to class Rectangle is created and then the constructor that initializes its data members.
Example : Demonstrating how the default constructor is called by the compiler
#include<iostream>
using namespace std;
class Rectangle
{
public: int l=3,b=3;
};
int main()
{
Rectangle rect;
cout<<"Area of Rectangle : "<<rect.l*rect.b;
}
Output:
Area of Rectangle : 9
- In the above program, Default Constructor is called automatically by the compiler which initializes the object data members that is l & b to default value (3,3). So the Area of Rectangle is 9.
- If constructor does not explicitly defined then the compiler will provide a default constructor implicitly.
2. Parameterized Constructor
- As we studied, default constructor does not have any parameters, but if you need to add parameters to the constructor, you can add to this and this constructor is called as Parameterized constructor.
- Parameterized Constructor is defined with the parameters which provide different values to data members of different objects by passing the values as an argument.
Example : Demonstrating the Parameterized Constructor
#include<iostream>
using namespace std;
class Square
{
public:
float side;
Square(float s) //Parameterized Constructor
{
side = s;
}
};
int main()
{
Square sq(5);
cout <<"Area of Square : "<<sq.side*sq.side<<endl;
}
Output:
Area of Square : 25
3. Copy Constructor
- Copy constructor is a special type of constructor which takes an object as an argument.
- It is a member function which initializes an object using another object of the same class.
- Copy constructor creates a new object which is exact copy of the existing constructor, hence it is called as Copy Constructor.
- This type of constructor is used to copy values of data members of one object into other object.
- It is used to create a copy of an already existing object if a class type.
- In copy constructor, for copying the object values, both objects must belong to the same class.

{
//body of constructor;
}
Example : Program demonstrating the Copy Constructor
#include<iostream>
using namespace std;
class CopyConst
{
private:
int a, b;
public:
CopyConst(int a1, int b1)
{
a = a1;
b = b1;
}
CopyConst(const CopyConst &obj2) // Copy constructor
{
a = obj2.a;
b = obj2.b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
};
int main()
{
CopyConst obj1(10, 20); // Normal constructor is called
cout<<"Normal Constructor"<<endl;
cout << "Value of a and b" <<"\n a is : "<< obj1.getA() <<"\n b is : "<< obj1.getB()<<endl; //Access values assigned by constructors
cout<<"\n Copy Constructor";
CopyConst obj2 = obj1; // Copy constructor is called
cout << "\n Value of a and b" <<"\n a is : "<< obj2.getA() <<"\n b is : "<< obj2.getB();
return 0;
}
Output:
Normal Constructor
Value of a and b
a is : 10
b is : 20
Copy Constructor
Value of a and b
a is : 10
b is : 20


