class Complex{ double real, imag; public: Complex(double r=0, double i=0): real(r), imag(i){ } friend istream& operator>>(istream& in, Complex& c); //已实现 friend ostream& operator<<(ostream& out, const Complex& c);//已实现 friend Complex operator+(const Complex& c1, const Complex& c2); friend Complex operator-(const Complex& c1, const Complex& c2); Complex& operator+=(const Complex& c); Complex& operator-=(const Complex& c); };#include <iostream> using namespace std; int main() { Complex c1, c2; cin >> c1 >> c2; cout << c1 + c2 << endl; cout << c1 - c2 << endl; c1 += c2; cout << c1 << endl; c1 -= c2; cout << c1 << endl; return 0; } /* 请在这里填写答案,类的详细定义已由系统给出,并重载了输入输出运算符,你只需要写出需要重载的算术运算符函数即可 */
时间: 2023-06-17 14:05:44 浏览: 217
重载的算术运算符函数如下所示:
```c++
Complex operator+(const Complex& c1, const Complex& c2){
return Complex(c1.real+c2.real, c1.imag+c2.imag);
}
Complex operator-(const Complex& c1, const Complex& c2){
return Complex(c1.real-c2.real, c1.imag-c2.imag);
}
Complex& Complex::operator+=(const Complex& c){
real += c.real;
imag += c.imag;
return *this;
}
Complex& Complex::operator-=(const Complex& c){
real -= c.real;
imag -= c.imag;
return *this;
}
```
其中,`operator+` 重载了加法运算符,返回两个复数相加的结果;`operator-` 重载了减法运算符,返回两个复数相减的结果;`operator+=` 重载了复合赋值加法运算符,将一个复数加上另一个复数并返回结果;`operator-=` 重载了复合赋值减法运算符,将一个复数减去另一个复数并返回结果。
相关问题
将运算符+和-重载为 complex 类的成员函数。 #include<iostream.h> class complex //复数类声明 { public: //外部接口 complex(double r=0.0,double i=0.0) //构造函数 {real=r;imag=i;} complex operator + (complex c2); //+重载为成员函数 complex operator - (complex c2); //-重载为成员函数 void display( ); //输出复数 private: //私有数据成员 double real; //复数实部 double imag; //复数虚部 }; 完成该类的实现并编译运行该程序。
以下是重载运算符+和-的 complex 类的实现代码:
```
#include<iostream>
class complex
{
public:
complex(double r = 0.0, double i = 0.0)
{
real = r;
imag = i;
}
complex operator+(complex c2)
{
return complex(real + c2.real, imag + c2.imag);
}
complex operator-(complex c2)
{
return complex(real - c2.real, imag - c2.imag);
}
void display()
{
std::cout << "(" << real << ", " << imag << "i)" << std::endl;
}
private:
double real;
double imag;
};
int main()
{
complex c1(1.0, 2.0);
complex c2(3.0, 4.0);
complex c3 = c1 + c2;
c3.display();
complex c4 = c1 - c2;
c4.display();
return 0;
}
```
运行结果:
```
(4, 6i)
(-2, -2i)
```
将运算符+和-重载为 complex 类的友元函数。 #include<iostream.h> class complex { public: complex(double r=0.0,double i=0.0) { real=r; imag=i; } //构造函数 friend complex operator + (complex c1,complex c2); //重载运算符+为友元函数 friend complex operator - (complex c1,complex c2); //重载运算符-为友元函数 void display(); //显示复数的值 private: //私有成员 double real; double imag; }; 完成该类的实现并编译运行该程序。
以下是重载运算符+和-的实现:
```cpp
#include<iostream>
using namespace std;
class complex
{
public:
complex(double r = 0.0, double i = 0.0)
{
real = r;
imag = i;
}
void display()
{
cout << real << "+" << imag << "i" << endl;
}
friend complex operator+(complex c1, complex c2);
friend complex operator-(complex c1, complex c2);
private:
double real;
double imag;
};
complex operator+(complex c1, complex c2)
{
return complex(c1.real + c2.real, c1.imag + c2.imag);
}
complex operator-(complex c1, complex c2)
{
return complex(c1.real - c2.real, c1.imag - c2.imag);
}
int main()
{
complex c1(1.0, 2.0), c2(2.0, 3.0);
complex c3 = c1 + c2;
complex c4 = c1 - c2;
c3.display(); // 输出 3+5i
c4.display(); // 输出 -1-1i
return 0;
}
```
运行结果:
```
3+5i
-1-1i
```
阅读全文