最后看一下,右移运算符的重载,右移运算符我们也常叫它输入运算符号,对于它来说,具体实现和左移运算符的重载差别并不大,对于有多成员对象的类来说,只要保证能够完整输入各成员对象大数据就可以了。
示例如下:
//程序作者:管宁 //站点:www.cndev-lab.com //所有稿件均有版权,如要转载,请务必著名出处和作者 #include <iostream> using namespace std; class Test { public: Test(int age = 0,char *name = "\0") { Test::age = age; strcpy(Test::name,name); } void inputmembers(istream &out) { cout<<"please input age:"; cin>>Test::age; cout<<"please input name:"; cin>>Test::name; } friend istream& operator >>(istream& ,Test&); public: int age; char name[50]; }; istream& operator >>(istream& input,Test &temp) { temp.inputmembers(input); return input; } int main() { Test a; cin>>a; cout<<a.age<<"|"<<a.name<<endl; system("pause"); } |