把下面代码的运算符重载改为友元函数形式#include<iostream> using namespace std; class complex { private: double real; double imag; public: complex(double r = 0.0, double i = 0.0); void print(); complex operator -=(complex c); complex operator *=(complex c); complex operator /=(complex c); complex operator ++(); complex operator ++(int); }; complex::complex(double r, double i) { real = r; imag = i; } complex complex::operator -=(complex c) { complex temp; temp.real = real - c.real; temp.imag = imag - c.imag; real = temp.real; imag = temp.imag; return temp; } complex complex::operator *=(complex c) { complex temp; temp.real = real * c.real - imag * c.imag; temp.imag = real * c.imag + imag * c.real; real = temp.real; imag = temp.imag; return temp; } complex complex::operator /=(complex c) { complex temp; double d; d = c.real * c.real + c.imag * c.imag; temp.real = (real * c.real + imag * c.imag) / d; temp.imag = (c.real * imag - real * c.imag) / d; real = temp.real; imag = temp.imag; return temp; } complex complex::operator ++() { complex temp; temp.real = ++real; temp.imag = ++imag; return temp; } complex complex::operator ++(int) { complex temp(real, imag); real++; imag++; return temp; } void complex::print() { cout << real; if (imag >= 0) cout << '+'; cout << imag << 'i' << endl; } int main() { complex A(30, 40), B(15, 30),C; C = A.operator++(1); cout << "C=A++后,C为:"; C.print(); cout << "A为:"; A.print(); C = A.operator++(); cout << "C=++A后,C为:"; C.print(); cout << "A为:"; A.print(); A *= B; cout << "A*=B后,A为:"; A.print(); A /= B; cout << "A/=B后,A为: "; A.print(); cout << "B为:"; B.print(); return 0; }
时间: 2023-06-16 18:07:23 浏览: 153
重载运算符改为友元函数的形式如下:
```cpp
#include<iostream>
using namespace std;
class complex {
private:
double real;
double imag;
public:
complex(double r = 0.0, double i = 0.0);
void print();
friend complex operator -=(complex& c1, const complex& c2);
friend complex operator *=(complex& c1, const complex& c2);
friend complex operator /=(complex& c1, const complex& c2);
friend complex operator ++(complex& c);
friend complex operator ++(complex& c, int);
};
complex::complex(double r, double i) {
real = r;
imag = i;
}
complex operator -=(complex& c1, const complex& c2) {
complex temp;
temp.real = c1.real - c2.real;
temp.imag = c1.imag - c2.imag;
c1.real = temp.real;
c1.imag = temp.imag;
return temp;
}
complex operator *=(complex& c1, const complex& c2) {
complex temp;
temp.real = c1.real * c2.real - c1.imag * c2.imag;
temp.imag = c1.real * c2.imag + c1.imag * c2.real;
c1.real = temp.real;
c1.imag = temp.imag;
return temp;
}
complex operator /=(complex& c1, const complex& c2) {
complex temp;
double d;
d = c2.real * c2.real + c2.imag * c2.imag;
temp.real = (c1.real * c2.real + c1.imag * c2.imag) / d;
temp.imag = (c2.real * c1.imag - c1.real * c2.imag) / d;
c1.real = temp.real;
c1.imag = temp.imag;
return temp;
}
complex operator ++(complex& c) {
complex temp;
temp.real = ++c.real;
temp.imag = ++c.imag;
return temp;
}
complex operator ++(complex& c, int) {
complex temp(c.real, c.imag);
c.real++;
c.imag++;
return temp;
}
void complex::print() {
cout << real;
if (imag >= 0) cout << '+';
cout << imag << 'i' << endl;
}
int main() {
complex A(30, 40), B(15, 30), C;
C = operator++(A, 1);
cout << "C=A++后,C为:";
C.print();
cout << "A为:";
A.print();
C = operator++(A);
cout << "C=++A后,C为:";
C.print();
cout << "A为:";
A.print();
A *= B;
cout << "A*=B后,A为:";
A.print();
A /= B;
cout << "A/=B后,A为: ";
A.print();
cout << "B为:";
B.print();
return 0;
}
```
需要注意的是,友元函数的参数中第一个参数需要加上引用符号 &,因为需要修改该对象的值。而第二个参数一般为 const 引用形式,以保证不会对该参数进行修改。另外,在调用友元函数时需要在函数名前加上 operator。
阅读全文