the program is ok(i think)but the data is fixed.i want it able to input your own data.i tried cin>>c1.real>>c1.image;
cin>>c2.real>>c2.image;
but it cannot use them because they are private...
#include<iostream>
using namespace std;
class complex
{
public:
complex(){real=0;image=0;}
complex(double r,double i){real=r;image=i;}
complex complex_add(complex &c2);
complex complex_sub(complex &c2);
complex complex_mult(complex &c2);
complex complex_div(complex &c2);
void display();
private:
double real;
double image;
};
complex complex::complex_add(complex &c2)
{
complex c;
c.real=real+c2.real;
c.image=image+c2.image;
return c;
}
complex complex::complex_sub(complex &c2)
{
complex d;
d.real=real-c2.real;
d.image=image-c2.image;
return d;
}
complex complex::complex_mult(complex &c2)
{
complex f;
f.real=real*c2.real;
f.image=image*c2.image;
return f;
}
complex complex::complex_div(complex &c2)
{
complex g;
g.real=real/c2.real;
g.image=image/c2.image;
return g;
}
void complex::display()
{
cout<<"("<<real<<","<<image<<"i)"<<endl;
}
int main()
{
complex c1(2,5),c2(4,9),c3; //i want input my own complex number.
c3=c1.complex_add(c2);
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c1+c2=";c3.display();
c3=c1.complex_sub(c2);
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c1-c2=";c3.display();
c3=c1.complex_mult(c2);
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c1*c2=";c3.display();
c3=c1.complex_div(c2);
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c1/c2=";c3.display();
return 0;
}