Monday, March 3, 2008

c program

//PARAMETERIZED CONSTRUCTORS

/*It may be necessary to initialize the various data elements of different

objects with different values when they are created*/

//constructors that can take arguments are called parameterized constructors

#include

#include

class integer

{

int m,n;

public:

integer(int,int);//constructor declared

void display(void)

{

cout<<"m="<

cout<<"n="<

}

};

integer::integer(int x,int y)

{

m=x;

n=y;

}

int main()

{

integer int1(0,100);//implicit call

integer int2=integer(25,75);//explicit call

clrscr();

cout<<"\nObject1"<<"\n";

int1.display();

cout<<"\nObject2"<<"\n";

int2.display();

getch();

}

//OVERLOADED CONSTRUCTORS OR MULTIPLE CONSTRUCTORS IN A CLASS

#include

#include

class complex

{

float x,y;

public:

complex()

{

}

complex(float a)

{

x=y=a;

}

complex(float real,float imag)

{

x=real;

y=imag;

}

friend complex sum(complex,complex);

friend void show(complex);

};

complex sum(complex c1,complex c2)

{

complex c3;

c3.x=c1.x+c2.x;

c3.y=c1.y+c2.y;

return(c3);

}

void show(complex c)

{

cout<

}

int main()

{

complex a(2.7,3.5);

complex b(1.6);

complex c;

clrscr();

c=sum(a,b);

cout<<"a=";

show(a);

cout<<"b=";

show(b);

cout<<"c=";

show(c);

getch();

}