Constructors
You might remember (I
hope you do) 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 initialising an object? Constructors are used for this
purpose. Basically, objects are created to model real life objects and when an
object is created, you will want some particular data (or properties) of the
object to be initialized. The idea is to prevent an object from having an
uncertain or undetermined state at the time of its creation. So, all
initializations of member data are done using the constructor.
A constructor is a member function with same name as the class. It is used for
the automatic initialisation of object and it is carried out at the time of
creation.
Constructors have no return type.
Syntax :
constructor name (arguments) : member data (value)
{ }
or
constructor-name
{
//body of constructor
}
So, what’s the difference in the two forms? The form used in the program below (the first form) makes use of an initializer list (i.e. count(7) forms the initializer list over here) to initialize the values while the constructor body is empty. In the other method we initialize the members within the constructor body. The initializer list is used for simple assignments.
Beware: When using an initialization list the order in which the members are initialized depends on the order in which you declare them within the class. The first member in the class will be the first member to be initialized. If your initializations depends on the sequence then be careful while using initializer lists.
In the case of assigning values within the constructor body, the order only depends on the order in which you write the statements within the body (and not on the declaration order).
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.
If you do not provide a constructor, the compiler will provide its own default constructor (but this default constructor will not do anything useful – i.e. it will not initialize your member data). The compiler will not provide its default constructor if you provide your own constructor. A constructor with no parameters is also called a default constructor.
The
next section deals with Constructors with arguments
Or
go back to Contents
Copyright © 2004 Sethu Subramanian All rights reserved.