一个问题
流插入和流提取运算符的重载
以流插入运算符为例




两道例题

//补写代码如下
//因为ostream类和istream类在头文件iostream中已经被c++设计者们设计好,无法修改,所以无法
//再写一个新的流插入运算符重载成员函数
//只能写一个新的流插入运算符重载全局函数
ostream& operator<<(ostream &os,const CStudent &c)
{
os<<s.nAge;
return os;
}

#include <bits/stdc++.h>
#define ASCII0 48
using namespace std;
class Complex
{
private:
int real;
int imag;
public:
Complex(int r = 0, int i = 0) : real(r), imag(i){};
friend istream &operator>>(istream &is, Complex &c);
friend ostream &operator<<(ostream &os, const Complex &c);
};
istream &operator>>(istream &is, Complex &c)
{
char str[10];
is >> str;
int x = 0;
bool b = 1;
for (int i = 0; i < strlen(str); i++)
{
if (str[i] >= '0' && str[i] <= '9')
x = x * 10 + (int)(str[i] - ASCII0);
else
{
if (b)
{
c.real = x;
x = b = 0;
}
else
{
c.imag = x;
x = 0;
}
}
}
return is;
}
ostream &operator<<(ostream &os, const Complex &c)
{
os << c.real << " + " << c.imag << "i ";
return os;
}
int main(void)
{
int n = 0;
Complex c;
cin >> c >> n;
cout << c << n;
return 0;
}