|
Constructors
Every object created would have a copy
of data members, which require
initialization before they can be
used. In the example discussed
earlier, the object A1 of class add
had to call a member function called
input() to initialize the member data
iNum1 and iNum2. In this case, there
is no possibility of member data
getting initialized automatically
whenever an object is created. Since
this requirement is so common, C++
allows objects to initialize
themselves as and when they are
created. This automatic initialization
is performed through the use of
constructor functions.
Declaration of Constructors
A constructor function is a special
function that is a member of the class
and has the same name as that of the
class. For example, the following code
shows the add class when it is written
to use a constructor function for
initialization:
Class add
{
private:
int iNum1, iNum2,iNum43;
public:
add(); //Constructor
void input(int, int);
void sum(void);
void disp(void);
};
Notice that the constructor add() had
no return type specified. In C++,
constructors cannot return values.
The add() function can be coded in the
following way:
add::add()
{
iNum1=iNum2=iNum3=0;
cout<<”Constructor Invoked”<<endl;
}
The message “Constructor Invoked” is
the output to illustrate that the
constructor is being called. In actual
practice, constructors are used only
for initialization and are not used
for any input or output operations.
If no constructors are declared for a
class, the compiler invokes its own
constructor.
Overloading Constructors
Besides performing the special role of
initialization, constructors are no
different from other functions. This
includes overloading also. It is very
common to find overloaded
constructors. For example, consider
the following program with overloaded
constructors for the add class:
//Program to illustrate the
application of add class
#include<iostream.h>
class add
{
private:
int iNum1, iNum2, iNum3;
public:
add(void); //Default construcors
add(int, int) //Two-Argument
Constructor
void input(int, int);
void sum(void);
void disp(void);
};
add:add(void) //Default constructor
{
iNum1=iNum2=iNum3=0;
}
add:add(int iX, int iY) //Constructor
2
{
iNum1=iX;
iNum2=iY;
iNum3=0;
}
void add::input(int iVar1, int iVar2)
{
iNum1=iVar1;
iNum2=iVar2;
}
void add::sum(void)
{
iNum3=iNum1+iNum2;
}
void add::disp(void)
{
cout<<”The sum of two numbers
is”<<iNum3<<endl;
}
void main()
{
add A1; //Default constructor invoked
add A2(4,3); //Constructor 2 invoked
add *Aptr;
Aptr=new add(5,10); //Pointer is
initialized with address
//and values assigned to member data
A2.sum(); //sum() function invoked to
initialize
//member data
Aptr->sum();
Aptr->disp(); //disp() function
invoked to display
//the value of member data
A2.disp();
}
Here, for the object A1, the
constructor with no argument, also
called the default constructor, will
be invoked because A1 is created with
no arguments. But, for the object A2
and the pointer Aptr, the-two argument
constructor will get invoked because
they are created with two arguments.
The conclusion that follows is that a
class can have many constructors each
differing in the number, type and
order of arguments. The number. Type
and order of arguments are together
referred to as the signature of the
function.
|