Defination of constructor:
Constructors are special class functions which performs initialization of every object.
The Compiler calls the Constructor whenever an object is created.
Constructor’s initialize values to data members after storage is allocated to the object.
While defining a constructor you must remember that the name of constructor will be same as the name of the class, and constructors never have return type.
Constructors can be defined either inside the class definition or outside class definition using class name and scope resolution :: operator.
Constructors are of four types :
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Explicit Constructor
1. Default Constructor in C++
Default constructor is the constructor which doesn’t take any argument. It has no parameters in default constructors
Syntax :
class_name ()
{
Constructor Definition
}
Following program demonstrates the default constructor:
#include
using namespace std;
class areaRect
{
private:
int h, w;
public:
areaRect()
{
h = 0;
w = 0;
}
int area()
{
cout<<"Enter the height of rectangle: ";
cin>>h;
cout<<"Enter the width of the rectangle: ";
cin>>w;
return h*w;
}
};
int main()
{
int result;
areaRect a1;
result = a1.area();
cout <
Output:
Area of Rectangle is: 0
2. Parameterized Constructor in C++
These are the constructors with parameters. Using this Constructor you can provide different values to data members of different objects, by passing the appropriate values as an argument.
Syntax :
class_name ()
{
Constructor Definition
}
Following program demonstrates the parameterized constructor:
#include
using namespace std;
class areaRect
{
private: int h, w;
public:
areaRect()
{
h = 0;
w = 0;
}
areaRect(int x, int y)
{
h = x;
w = y;
}
int area()
{
return h*w;
}
};
int main()
{
int result;
areaRect a1;
result = a1.area();
cout <
Output:
READ C++ program to read and print Student information
Area of Rectangle is: 0
Area of Rectangle is: 200
3. Copy Constructor in C++
Copy constructor is a special type of constructor in which new object is created as a copy of an existing object.
The copy constructor is called whenever a new variable is created from an object.
Syntax :
class_name (class_name &obj)
{
Constructor Definition
}
Following program demonstrates the copy constructor:
#include
using namespace std;
class areaRect
{
private: int h, w;
public:
areaRect()
{
h = 10;
w = 10;
}
areaRect(areaRect &obj)
{
h = obj.h;
w = obj.w;
}
int area()
{
return h*w;
}
};
int main()
{
int result;
areaRect a1;
result = a1.area();
cout <
Output:
Area of Rectangle is: 100
Area of Rectangle is: 100
4. Explicit Constructor in C++
In C++, compiler is allowed to make one time type conversion to resolve the parameters to functions.
READ Friend function in C++ with Programming example
In C++, a constructor with only one required parameter is considered as an implicit conversion function. It converts the parameter type to the class type.
Prefixing explicit keyword prevents the constructor from using the implicit conversion.
Syntax :
explicit class_name (parameters)
{
Constructor Definition
}
Following program demonstrates the explicit constructor:
#include
using namespace std;
class test
{
private:
int val;
public: explicit test(int x)
{
val = x;
}
void display()
{
cout <<"The value of val is "<
Output:
The value of val is 10.