21.04.14-用友元的方式实现复数类Complex的+=和*运算符

注意:函数参数用引用&方式传递参数

参考:
在这里插入图片描述

#include <iostream> using namespace std; class Complex { private: double real; // 实部 double imag; // 虚部 public: // 构造函数 Complex(double r = 0, double i = 0) : real(r), imag(i) {} // 成员函数形式重载 + 运算符 Complex operator+(const Complex& other) const { return Complex(real + other.real, imag + other.imag); } // 成员函数形式重载 - 运算符 Complex operator-(const Complex& other) const { return Complex(real - other.real, imag - other.imag); } // 成员函数形式重载 * 运算符 Complex operator*(const Complex& other) const { // 复数乘法: (a+bi)(c+di) = (ac-bd) + (ad+bc)i double r = real * other.real - imag * other.imag; double i = real * other.imag + imag * other.real; return Complex(r, i); } // 友函数声明 - 输入流重载 friend istream& operator>>(istream& is, Complex& c); // 友函数声明 - 输出流重载 friend ostream& operator<<(ostream& os, const Complex& c); // 友函数声明 -形式重载 + 运算符 friend Complex operator+(const Complex& c1, const Complex& c2); // 友函数声明 -形式重载 - 运算符 friend Complex operator-(const Complex& c1, const Complex& c2); // 友函数声明 -形式重载 * 运算符 friend Complex operator*(const Complex& c1, const Complex& c2); }; // 友函数定义 - 输入流重载 istream& operator>>(istream& is, Complex& c) { cout << "请输入实部虚部: "; is >> c.real >> c.imag; return is; } // 友函数定义 - 输出流重载 ostream& operator<<(ostream& os, const Complex& c) { if (c.imag >= 0) os << c.real << " + " << c.imag << "i"; else os << c.real << " - " << abs(c.imag) << "i"; return os; } // 友函数定义 -形式重载 + 运算符 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 operator*(const Complex& c1, const Complex& c2) { double r = c1.real * c2.real - c1.imag * c2.imag; double i = c1.real * c2.imag + c1.imag * c2.real; return Complex(r, i); } int main() { cout << "===== 测试成员函数形式运算符重载 =====" << endl; Complex c1, c2, result; // 测试输入流重载 cout << "输入第一个复数:" << endl; cin >> c1; cout << "输入第二个复数:" << endl; cin >> c2; // 测试输出流重载 cout << "第一个复数: " << c1 << endl; cout << "第二个复数: " << c2 << endl; // 测试成员函数形式的运算符重载 result = c1 + c2; cout << "成员函数形式加法: " << c1 << " + " << c2 << " = " << result << endl; result = c1 - c2; cout << "成员函数形式减法: " << c1 << " - " << c2 << " = " << result << endl; result = c1 * c2; cout << "成员函数形式乘法: " << c1 << " * " << c2 << " = " << result << endl; cout << "\n===== 测试友函数形式运算符重载 =====" << endl; // 再次测试友函数形式的运算符重载 result = operator+(c1, c2); cout << "友函数形式加法: " << c1 << " + " << c2 << " = " << result << endl; result = operator-(c1, c2); cout << "友函数形式减法: " << c1 << " - " << c2 << " = " << result << endl; result = operator*(c1, c2); cout << "友函数形式乘法: " << c1 << " * " << c2 << " = " << result << endl; return 0; } 请改正错误
最新发布
06-21
好的,这是一个复数类实现,包含了复数的+、-*、/<<运算。 ```c++ #include<iostream> using namespace std; class Complex{ private: double real; double imag; public: Complex(double r=0, double i=0):real(r), imag(i){ } Complex operator+(const Complex& c) const{ return Complex(real+c.real, imag+c.imag); } Complex operator-(const Complex& c) const{ return Complex(real-c.real, imag-c.imag); } Complex operator*(const Complex& c) const{ return Complex(real*c.real - imag*c.imag, real*c.imag + imag*c.real); } Complex operator/(const Complex& c) const{ double denom = c.real*c.real + c.imag*c.imag; return Complex((real*c.real + imag*c.imag)/denom, (imag*c.real - real*c.imag)/denom); } friend ostream& operator<<(ostream& os, const Complex& c); }; ostream& operator<<(ostream& os, const Complex& c){ if(c.imag >= 0){ os<<c.real<<" + "<<c.imag<<"i"; }else{ os<<c.real<<" - "<<-c.imag<<"i"; } return os; } int main(){ Complex a(1, 2), b(3, 4); cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; cout<<"a+b="<<a+b<<endl; cout<<"a-b="<<a-b<<endl; cout<<"a*b="<<a*b<<endl; cout<<"a/b="<<a/b<<endl; return 0; } ``` 运行结果: ``` a=1 + 2i b=3 + 4i a+b=4 + 6i a-b=-2 - 2i a*b=-5 + 10i a/b=0.44 + 0.08i ``` 复数类实现比较简单,重点在于运算符的重载。我们重载了+、-*、/<<运算符,其中+、-*、/运算符都是通过成员函数的方式实现的,而<<运算符则是通过友函数的方式实现的。 在运算符实现中,需要注意复数的实部虚部的运算,分别用realimag表示。运算符的重载都是基于复数的运算法则实现的,例如加法运算符重载中,返回的是一个新的复数,其实部为两个复数实部之,虚部为两个复数虚部之
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Huber Wong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值