• C++ iostrream标准库和左右移运算符的重载3

    普通类
    • 支持
    • 批判
    • 提问
    • 解释
    • 补充
    • 删除
    • C++ iostrream标准库和左右移运算符的重载3

    最后看一下,右移运算符的重载,右移运算符我们也常叫它输入运算符号,对于它来说,具体实现和左移运算符的重载差别并不大,对于有多成员对象的类来说,只要保证能够完整输入各成员对象大数据就可以了。

      示例如下:

     C++ 代码 //程序作者:管宁
    //站点: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");
    }




    

    • 标签:
    • cintest
    • 右移
    • iostrream
    • test
    • 左右
    • istream
    • int
    • operator
    • input
    • 标准
    • 运算符
    • public
    • char
    • coutplease
    • age
  • 加入的知识群:
    学习元评论 (0条)

    评论为空
    聪明如你,不妨在这 发表你的看法与心得 ~



    登录之后可以发表学习元评论
      
暂无内容~~
顶部