C++面向对象程序设计 030:你真的搞清楚为啥 while(cin >> n) 能成立了吗? ---- (北大Mooc)

文章目录


原题题目

在这里插入图片描述

#include <iostream>
using namespace std;
class MyCin
{
    
    
// 在此处补充你的代码
};
int main()
{
    
    
    MyCin m;
    int n1,n2;
    while( m >> n1 >> n2) 
        cout  << n1 << " " << n2 << endl;
    return 0;
}

代码实现

private:
    int judge;
public:
    MyCin():judge(1){
    
    }
    MyCin& operator >>(int& n)
    {
    
    
        cin>>n;
        if(n == -1)
            judge = 0;
        return *this;
    }
    operator int(){
    
    return judge;}
};

猜你喜欢

转载自blog.csdn.net/qq_37500516/article/details/114997433