C++ program for Complex Number Addition using multiple constructor.

#include<iostream.h>
#include<conio.h>
class complex{
float x,y;
public:
complex(){x=y=0;}
complex(float a){
x=y=a;
}
complex(float real,float img){
x=real;
y=img;
}
complex(complex &c){
x=c.x;
y=c.y;
}
complex sum(complex c);
void show();
};
complex complex::sum(complex c){
complex c3;
c3.x=x+c.x;
c3.y=y+c.y;
return c3;
}
void complex:: show(){
cout<<"complex number is :"<<endl<<x<<"+("<<y<<")i"<<endl;
}
void main(){
complex A,B(2),C(3,-5);
complex D(B);
clrscr();
//Display initial values of objects.
A.show();
B.show();
C.show();
D.show();
getch();
A=B.sum(C);
A.show();
getch();
}


No comments:

Post a Comment