Home / Cplusplus / Basic Constructors In C++

Basic Constructors In C++

You might remember that we can initialize a variable when declaring it.
 
For example:
 
int variable = 5;
 
This line declares variable to be an integer and assigns the value of 5 to variable. Similarly, how about initializing an object?
 
Constructors are used for this purpose. A constructor is a special member function with same name as the class. It is used for the automatic initialization of object and it is carried out at the time of creation.
 
A constructor initializes an object immediately upon creation. It has same name as a class in which it resides and is syntactically similar to a function. Once created the constructor is automatically called after the object is created.
 
Constructors have no return type.
 
Syntax:
 
constructor name (arguments): member data (value)
 
{ }
 
As usual let’s take a look at a simple program to illustrate the use of constructors.
 
class counter
{
private:
int count;
public:
 
counter ( ) : count (0) // Constructor initializes member data count to zero.
{ }
 
……………………………… // As usual you can write the public functions
};
 
int main ( )
{
counter c1;
return 0;
}
 
When the compiler reads the line
 
counter c1;
it goes to the public part of the class and sees the constructor used. Since counter c1; has no arguments the compiler makes use of the no argument constructor.
 
In this case we have used only the no argument constructor. Hence the count of c1 is initialized to zero.
 
This is what we wanted to do: Create an object and at the time of creation set the member data to a particular value. Instead of zero you can even give a value.
 
For example:
 
counter ( ) : count ( 7 )
{ }
 
If you use the above constructor, then the value of member data count is set to 7.
 
Constructors with argument
 
The constructors that can take arguments are called parameterized constructors. It’s time to see constructors with arguments. They are similar to constructors without arguments.
 
Example is shown below:
 
class counter
{
private :
int count;
public :
counter (int x) : count (x)
{ }
 
………………………………………….// Some other functions in the class
};
 
int main ( )
{
int y;
counter c1(2);
cout<< “What value of count do you want initialize it to?”;
cin>>y;
counter c2(y);
………………………………..// We’re not typing the rest. It could be anything to suit your purpose.
return 0;
}
 
Program is simple. counter c1(2); means that the constructor with one argument is invoked. The argument passed is 2. Its value is assigned to count. count (x) is equal to count = x. Hence count of object c1 is two.
 
counter c2 (y); means that count of c2 will be initialized to the value of y (i.e. the value the user types in).
 
It also possible to define constructors with default argument. For example, the constructor count() can be declared as follows:
 
complex (float real, float imag=0);
 
Dynamic Initialization of objects
 
Classes objects can be initialized dynamically too. That is, the initial value of an object may be provided during run time. One advantage of dynamic initialization is that we can provide various initialization format, using overloaded constructors.