I make a class about Complex addition,but I found it was kinda tedious to write the overload function. my code(c++11) is as follows:
#include <iostream>
class complex{
public:
complex(double real,double imag);
complex();
friend complex operator +(const double &m, const complex &origin);
friend complex operator +(const complex &origin, const double &m);
double real;
double imag;
};
complex::complex(double real,double imag):real(real),imag(imag){;};
complex::complex():real(0),imag(0){;};
complex operator +(const double &m,const complex &origin){
complex p;
p.real=origin.real+m;
p.imag=origin.imag;
return p;
}
complex operator +(const complex &origin,const double &m){
return m+origin;
}
int main() {
complex com(1,2);
com=3.+com;
com=com+3.;
return 0;
}
can I do the addition of Complex with one operator function by using my class(anyway I DON'T WANNA use standard library of )? thanks.
Aucun commentaire:
Enregistrer un commentaire