1.流重载 istream ostream
1.class
class MM {friend ostream& operator<<(ostream& out, MM& mm);friend istream& operator>>(istream& in, MM& mm); public:MM() {}MM(int age,string name):age(age),name(name) {} private:int age;string name; };
2.实现函数
ostream& operator<<(ostream& out, MM& mm) {out << mm.age << "岁的" << mm.name << "喜欢我" << endl;return out; } istream& operator>>(istream& in, MM& mm) {in >> mm.age >> mm.name;return in; }
3.main
int main() {//输出流MM mm(23, "王");MM mm1(18, "女神");cout << mm <<mm1<< endl;//输入流MM mm2, mm3;cin >> mm2 >> mm3;cout << mm2 << mm3;return 0; }
结果:
2. 重载的一些基础知识