码迷,mamicode.com
首页 > 其他好文 > 详细

重载>>运算符

时间:2017-09-20 16:38:49      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:c/c++

#include <iostream>  
#include <stdio.h>  
using namespace std;  
class Input  
{  
    public:  
       //实际重载是右移运算符  
       Input & operator >> (int &a)  
       {  
           scanf("%d",&a);  
           fflush(stdin);//去掉回车  
           return *this;  
       }  
       Input & operator >> (float &a)  
       {  
           scanf("%f",&a);  
           fflush(stdin);//去掉回车  
           return *this;  
       }  
       //重载输出运算符  
      friend ostream & operator <<(ostream &os,Input &v);  
};  
ostream & operator <<(ostream &os,Input & v)  
{  
     os << v.m_pData->id ;  
     return os;  
}  
  
Input In;  
int main(int argc, char* argv[])  
{  
    int   a;  
    float b;  
    In >>a>>b;  
    cout << a << "," << b <<endl;  
    getchar();  
}


重载>>运算符

标签:c/c++

原文地址:http://9233403.blog.51cto.com/9223403/1967106

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!