运算符重载问题.

 #include<iostream.h>
class com
{
private:
 double re;
 double im;
public:
 com(double i=0.0,double j=0.0){re=i;im=j;}
 com operator +(com c1);
    void display();
};
com com::operator+(com c1)
{
 return com(re+c1.re,im+c1.im);  //能说说编译器是怎么处理?
}
void com::display()
{
 cout<<re<<','<<im<<endl;
}
void main()
{
 com c1(5,4),c2(2,1),c3;
    c3=c1+c2;
 c3.display();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我来为您编写一个 Complex 类实现以下功能: 1. 包含两个整数成员变量实部和虚部; 2. 友元函数实现++运算符重载; 3. 成员函数实现﹣﹣运算符重载; 4. 友元函数实现*运算符重载; 5. 实现输入和输出运算符重载。 代码如下: ```c++ #include <iostream> using namespace std; class Complex { private: int real; // 实部 int imag; // 虚部 public: // 构造函数 Complex(int real = 0, int imag = 0) { this->real = real; this->imag = imag; } // 友元函数实现++运算符重载 friend Complex operator++(Complex &a, int) { Complex old = a; a.real++; a.imag++; return old; } // 成员函数实现﹣﹣运算符重载 Complex operator--() { real--; imag--; return *this; } // 友元函数实现*运算符重载 friend Complex operator*(const Complex &a, const Complex &b) { Complex c; c.real = a.real * b.real - a.imag * b.imag; c.imag = a.real * b.imag + a.imag * b.real; return c; } // 实现输入和输出运算符重载 friend ostream& operator<<(ostream &out, const Complex &c) { out << c.real << "+" << c.imag << "i"; return out; } friend istream& operator>>(istream &in, Complex &c) { cout << "请输入实部和虚部,格式为:实部 虚部" << endl; in >> c.real >> c.imag; return in; } }; int main() { Complex c1, c2; cin >> c1 >> c2; cout << "c1 = " << c1 << endl; cout << "c2 = " << c2 << endl; Complex c3 = c1 * c2; cout << "c1 * c2 = " << c3 << endl; cout << "c1++ = " << c1++ << endl; cout << "--c2 = " << --c2 << endl; return 0; } ``` 您可以在 `main()` 函数中进行测试,可以自行修改输入输出的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值