Saturday 2 October 2010

Constructor overloading



A class which consists of more than one constructor is called to be constructor overloaded. The constructors will be distinguished based upon the signatures, which includes the number of arguments, type and sequence of arguments. The compiler invokes the constructor that matches with the signature.

EXAMPLE   SYNTAX:
class sum
{
int a, b;
public:
sum(int, float b, floatc);    // constructors are overloaded
sum(int a, int b);
sum();
};
 EXAMPLE:
class A
{
int a,b,c,d;
public:
A( )
{
a=0;
b=0;
c=0;
d=0;
}
A(int x)
{
a=x;
}
A(int x, int y, int z)
{
b=x;
c=y;
d=z;
}
void put( )
{
cout<<”a=”<<a;
cout<<”\nb=”<<b;
cout<<”\nc=”<<c;
cout<<”\nd=”<<d;
}
};
void main( )
{
A ( );
obj.put();
obj=A(7);
obj.put();
obj=A(8,  9, 10);
obj.put;
}


No comments:

Post a Comment