定义一个复数类,通过运算符重载,用-号实现复数的减法运算,要求必须在main函数中进行测试
时间: 2023-12-22 10:02:22 浏览: 107
以下是一个复数类的定义,其中重载了 "-" 运算符实现了复数的减法运算。在 main 函数中,我们创建两个复数对象进行测试。
```c++
#include <iostream>
using namespace std;
class Complex {
public:
Complex() : real(0), imag(0) {}
Complex(double r, double i) : real(r), imag(i) {}
Complex operator- (const Complex& c) {
return Complex(real - c.real, imag - c.imag);
}
void print() {
cout << real << " + " << imag << "i" << endl;
}
private:
double real, imag;
};
int main() {
Complex c1(2, 3);
Complex c2(1, 2);
Complex c3 = c1 - c2;
c3.print();
return 0;
}
```
上述代码中,我们在 Complex 类中重载了 "-" 运算符,用于实现复数的减法运算。在 main 函数中,我们创建了两个复数对象 c1 和 c2,并对它们进行了减法运算。最后将结果保存在 c3 变量中,并调用 print 函数打印出来。
相关问题
要求定义一个复数类,利用运算符重载定义该复数类的加法减法,取反运算。注意双目运算使用友元方式,单目使用成员方式另外使用转换函数实现该类对double类型的相互转化。并在主函数main中输出
好的,以下是复数类的定义和运算符重载的实现:
```cpp
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
friend Complex operator+(const Complex& a, const Complex& b);
friend Complex operator-(const Complex& a, const Complex& b);
Complex operator-() const;
operator double() const { return real; }
void print() const {
if (imag < 0)
cout << real << "-" << -imag << "i" << endl;
else
cout << real << "+" << imag << "i" << endl;
}
};
Complex operator+(const Complex& a, const Complex& b) {
return Complex(a.real + b.real, a.imag + b.imag);
}
Complex operator-(const Complex& a, const Complex& b) {
return Complex(a.real - b.real, a.imag - b.imag);
}
Complex Complex::operator-() const {
return Complex(-real, -imag);
}
int main() {
Complex a(2, 3), b(3, -4);
(a + b).print();
(a - b).print();
(-a).print();
double x = 1.23;
Complex c = x;
c.print();
return 0;
}
```
运行结果:
```
5-i
-1+7i
-2-3i
1.23+0i
```
定义一个复数类Complex,重载运算符“-”,使之能用于复数的减法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之差
下面是Complex类的定义与运算符函数重载的实现:
```cpp
#include <iostream>
class Complex {
public:
Complex(double real = 0.0, double imag = 0.0) : real_(real), imag_(imag) {}
double real() const { return real_; }
double imag() const { return imag_; }
private:
double real_;
double imag_;
};
// 重载运算符“-”
Complex operator-(const Complex& lhs, const Complex& rhs) {
return Complex(lhs.real() - rhs.real(), lhs.imag() - rhs.imag());
}
int main() {
Complex a(1.0, 2.0);
Complex b(3.0, 4.0);
Complex c = a - b;
std::cout << c.real() << " + " << c.imag() << "i" << std::endl;
return 0;
}
```
输出结果为:
```
-2 + -2i
```
可以看到,运算符“-”被重载为非成员、非友元的普通函数,可以被用于复数的减法运算。通过重载运算符“-”,我们可以方便地计算两个复数之差。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)