#include <iostream>
using namespace std;
class Complex
{
friend ostream &operator <<(ostream &out, Complex &c);
friend istream &operator >>(istream &in, Complex& c);
friend Complex operator +(int num, Complex &c);
public:
Complex(int a = 0, int b = 0)
{
m_a = a;
m_b = b;
}
void show()
{
if (m_b == 0)
cout << m_a << endl;
else if(m_b>0)
cout << m_a << " + " << m_b << "i" << endl;
else
cout << m_a << " - " << m_b*-1 << "i" << endl;
}
Complex operator +(Complex &c)
{
Complex tmp(m_a+c.m_a, m_b+c.m_b);
return tmp;
}
Complex operator +(int num)
{
Complex tmp(m_a+num, m_b);
return tmp;
}
private:
int m_a; // 实部
int m_b; // 虚部
};
ostream &operator <<(ostream &ou
左移、右移运算符重载
最新推荐文章于 2024-02-13 13:55:07 发布