标签:style http color io os ar 使用 sp on
操作符重载
? #include?<iostream> #include?<string> using?namespace?std; ? //定义水果类 ?class?Fruit { ??string?name; ??string?colour; public: ?????????friend?istream&?operator>>(istream&,Fruit&);????????????????????//?输入流 ?????????friend?ostream&?operator<<(ostream&?out,const?Fruit&?fruit);???????//?输出流 ?????????void?print() ?????????{ ???????????????????cout<<colour<<"?"<<name<<endl; ?????????} ? ?????????Fruit(const?string?nst="apple",const?string?cst="green"):name(nst),colour(cst) ?????????{ ?????????} ? ?????????~Fruit() ?????????{ ?????????} }; ? ? //重载操作符 ostream&?operator<<(ostream&?out,?const?Fruit?&s) { ?????????out<<s.colour<<"?"<<s.name<<endl; ?????????return?out; ? } ? istream&?operator>>(istream&?in,Fruit?&s) { ?????????in>>s.colour>>s.name; ?????????if?(!in) ?????????{ ???????????????????cerr<<"Wrong?input!"<<endl; ?????????} ?????????return?in; } ? ? ?
主程序:
?????????Fruit?apple; ?????????cin>>apple; ?????????cout<<apple; ?
本文使用?书画小说软件?发布,内容与软件无关,书画小说软件?更惬意的读、更舒心的写、更轻松的发布。
标签:style http color io os ar 使用 sp on
原文地址:http://www.cnblogs.com/shuilan0066/p/4032250.html