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 12:05:44 浏览: 211
重载的算术运算符函数如下所示:
```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-=` 重载了复合赋值减法运算符,将一个复数减去另一个复数并返回结果。
相关问题
#include<iostream> using namespace std; class Complex { private: double real; double imag; 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(); }; Complex Complex:: operator +(Complex c1, Complex c2) { Complex c; c.real = c1.real + c2.real; c.imag = c1.imag + c2.imag; return c; } Complex Complex:: operator -(Complex c1, Complex c2) { Complex c; c.real = c1.real - c2.real; c.imag = c1.imag - c2.imag; return c; } void Complex::display() { cout << "(" << real << "," << imag << ")" << endl; } int main() { Complex c1(4, 3), c2(2, 1), c3; cout << "c1="; c1.display(); cout << "c2="; c2.display(); c3 = c1 - c2; cout << "c3="; c3.display(); c3 = c1 + c2; cout << "c3="; c3.display(); return 0; } 存在的问题
代码中存在以下问题:
1. 缺少头文件:需要添加头文件 `#include<iostream>`。
2. 命名空间使用不规范:应该避免在头文件中使用 `using namespace std;`,而是应该在使用标准库的时候显式地使用 `std::`。
3. 运算符重载函数的定义方式不正确:应该将运算符重载函数定义为类的成员函数,或者定义为友元函数。在代码中,运算符重载函数应该写成 `Complex Complex::operator+(Complex c2)` 和 `Complex Complex::operator-(Complex c2)`。
4. 运算符重载函数的参数传递方式不正确:在运算符重载函数中,需要将当前对象作为一个参数传入。在代码中,应该将运算符重载函数定义为友元函数,并且将两个对象作为参数传入,在函数中分别访问它们的私有成员变量。
5. 类方法的定义方式不规范:在定义类方法时,应该将方法的实现写在类定义的外面,而不是写在类定义内部。
6. 对象的初始化方式不规范:在定义对象时,应该避免使用默认构造函数的方式进行初始化,而是应该使用带参数的构造函数进行初始化。在代码中,应该将 `Complex c1(4, 3), c2(2, 1), c3;` 改为 `Complex c1(4, 3), c2(2, 1), c3(0, 0);`。
下面是修改后的代码:
```c++
#include<iostream>
class Complex {
private:
double real;
double imag;
public:
Complex(double r=0.0, double i=0.0) {
real = r;
imag = i;
}
Complex operator+(Complex c2);
Complex operator-(Complex c2);
void display();
};
Complex Complex::operator+(Complex c2) {
Complex c;
c.real = real + c2.real;
c.imag = imag + c2.imag;
return c;
}
Complex Complex::operator-(Complex c2) {
Complex c;
c.real = real - c2.real;
c.imag = imag - c2.imag;
return c;
}
void Complex::display() {
std::cout << "(" << real << "," << imag << ")" << std::endl;
}
int main() {
Complex c1(4, 3), c2(2, 1), c3(0, 0);
std::cout << "c1=";
c1.display();
std::cout << "c2=";
c2.display();
c3 = c1 - c2;
std::cout << "c3=";
c3.display();
c3 = c1 + c2;
std::cout << "c3=";
c3.display();
return 0;
}
```
#include<iostream> using namespace std; class Complex { double real,imag; public: Complex(double r=0,double i=0):real(r),imag(i) {} //只是让代码看起来简短而已 double get_real() {return real;} double get_imag() {return imag;} void print_complex() {cout<<real<<"+"<<"j"<<imag<<endl;} Complex operator + (double a); Complex operator + (Complex x); friend ostream &operator << (ostream &out,Complex &A ); } ostream &operator << (ostream &out,Complex &A ) { out<<A.real<<"+"<<A.imag<<"j"; return out; } Complex Complex:: operator + (double a) { Complex m; m.real=real+a;//实数 m.imag=imag; return m; } Complex Complex::operator +(Complex x)//加不加都可以,引用调用 { Complex t; t.imag=imag+x.imag; t.real=real+x.real; return t; } int main() { Complex c1(1,2),c2(4,5),c3; c1=c1+4.2; //重载 复数类+整型 cout<<c1<<endl; cout<<c2<<endl; c3=c1+c2; //重载 复述类+复数类 cout<<c3<<endl; return 0; } 哪里有问题以及怎么解决
这段代码中有两个问题:
1. 在重载<<运算符时,应该将Complex对象作为常量引用传递,即:
```
friend ostream &operator << (ostream &out, const Complex &A );
```
2. 在Complex类的构造函数中,应该使用成员初始化列表来初始化real和imag成员变量,即:
```
Complex(double r=0, double i=0): real(r), imag(i) {}
```
修改后的代码如下:
```
#include<iostream>
using namespace std;
class Complex {
double real, imag;
public:
Complex(double r=0, double i=0): real(r), imag(i) {}
double get_real() {return real;}
double get_imag() {return imag;}
void print_complex() {cout<<real<<"+"<<"j"<<imag<<endl;}
Complex operator + (double a);
Complex operator + (Complex x);
friend ostream &operator << (ostream &out, const Complex &A );
};
ostream &operator << (ostream &out, const Complex &A ) {
out<<A.real<<"+"<<A.imag<<"j";
return out;
}
Complex Complex:: operator + (double a) {
Complex m;
m.real=real+a; //实数
m.imag=imag;
return m;
}
Complex Complex::operator +(Complex x) {
Complex t;
t.imag=imag+x.imag;
t.real=real+x.real;
return t;
}
int main() {
Complex c1(1,2),c2(4,5),c3;
c1=c1+4.2; //重载 复数类+整型
cout<<c1<<endl;
cout<<c2<<endl;
c3=c1+c2; //重载 复数类+复数类
cout<<c3<<endl;
return 0;
}
```
阅读全文