#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
左移、右移运算符重载
最新推荐文章于 2024-05-11 13:45:14 发布
本文详细介绍了C++中如何重载左移<<和右移>>运算符,包括它们在I/O流和自定义类型中的使用,以及重载的语法和注意事项。
摘要由CSDN通过智能技术生成