c++编程的一道练习题,就是给复数类重载运算符,要求是用友元函数,我编的编译老是有一个error,求改
#include<iostream>using namespace std;class Complex{ double real,image;public: Complex(double r=0.0,double i=0.0){real=r;image=i;} Complex(Complex &com){ real=com.real;image=com.image; } void print(){ cout<<"Real="<<real<<'\t'<<"Image="<<image<<endl; } friend Complex operator-(Complex,Complex); friend Complex operator-=(Complex,Complex); friend Complex operator*=(Complex,Complex); friend Complex operator/=(Complex,Complex); Complex operator++(); Complex operator++(int);};Complex operator-(Complex c1,Complex c2){ Complex temp(c1.real-c2.real,c1.image-c2.image); return temp;}Complex operator -= (Complex c1,Complex c2){ c1.real=c1.real-c2.real; c1.image=c1.image-c2.image; return c1;}Complex Complex::operator /= (Complex c1,Complex c2){ Complex temp; double d=c2.real*c2.real+c2.image*c2.image; temp.real =(c1.real*c2.real+c1.image*c2.image)/d; temp.image =(c1.image*c2.real-c1.real*c2.image)/d; c1.real=temp.real; c2.image=temp.image; return temp;}Complex Complex::operator *= (Complex c1,Complex c2){ Complex temp; temp.real =c1.real*c2.real-c1.image*c2.image; temp.image=c1.image*c2.real+c1.real*c2.image; c1.real=temp.real; c2.image=temp.image; return temp;}Complex Complex::operator ++ (){ return Complex(++real,image);}Complex Complex::operator ++ (int){ return Complex(real++,image);}int main(){ Complex c1(1.0,1.0),c2(2.0,2.0),c3(4.0,4.0),c; c1.print(); c=c2-c3;c.print (); c-=c1;c.print(); c1++;c1.print (); return 0;} 展开
把第一行的<iostream>改为<iostream.h>
把第二行内容去掉
把以下两行中的complex都去掉一个
Complex Complex::operator /= (Complex c1,Complex c2){
Complex Complex::operator *= (Complex c1,Complex c2){
最终程序为:
#include<iostream.h>
class Complex{
double real,image;
public:
Complex(double r=0.0,double i=0.0){real=r;image=i;}
Complex(Complex &com){
real=com.real;image=com.image;
}
void print(){
cout<<"Real="<<real<<'\t'<<"Image="<<image<<endl;
}
friend Complex operator-(Complex,Complex);
friend Complex operator-=(Complex,Complex);
friend Complex operator*=(Complex,Complex);
friend Complex operator/=(Complex,Complex);
Complex operator++();
Complex operator++(int);
};
Complex operator-(Complex c1,Complex c2){
Complex temp(c1.real-c2.real,c1.image-c2.image);
return temp;
}
Complex operator -= (Complex c1,Complex c2){
c1.real=c1.real-c2.real;
c1.image=c1.image-c2.image;
return c1;
}
Complex operator /= (Complex c1,Complex c2){
Complex temp;
double d=c2.real*c2.real+c2.image*c2.image;
temp.real =(c1.real*c2.real+c1.image*c2.image)/d;
temp.image =(c1.image*c2.real-c1.real*c2.image)/d;
c1.real=temp.real;
c2.image=temp.image;
return temp;
}
Complex operator *= (Complex c1,Complex c2){
Complex temp;
temp.real =c1.real*c2.real-c1.image*c2.image;
temp.image=c1.image*c2.real+c1.real*c2.image;
c1.real=temp.real;
c2.image=temp.image;
return temp;
}
Complex Complex::operator ++ (){
return Complex(++real,image);
}
Complex Complex::operator ++ (int){
return Complex(real++,image);
}
int main(){
Complex c1(1.0,1.0),c2(2.0,2.0),c3(4.0,4.0),c;
c1.print();
c=c2-c3;c.print ();
c-=c1;c.print();
c1++;c1.print ();
return 0;
}
2)
Complex Complex::operator ++ (){
return Complex(++real,image);
}
实现有问题:
Complex Complex::operator ++ (){
real++;
return *this;
}
最好改成如下的形式:
Complex &Complex::operator ++ (){
real++;
return *this;
}