重载>>运算符

#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();  
}
时间: 2024-08-02 10:57:34

重载>>运算符的相关文章

C++学习之重载运算符1

C++除可重载函数之后,还允许定义已有的运算符,这样通过运算符重载可像处理数据使用它们. 先来个代码 1 #include<iostream> 2 using namespace std; 3 4 class num 5 { 6 public: 7 num(){n=1;} 8 ~num(){} 9 int get() const{return n;} 10 void set(int x){n=x;} 11 private: 12 int n; 13 }; 14 15 int main() 16

矩阵求和--重载运算符

C++习题 矩阵求和--重载运算符 [Submit][Status][Web Board] Description 有两个矩阵a和b,均为2行3列.求两个矩阵之和.重载运算符"+",使之能用于矩阵相加(如c=a+b). 重载流插入运算符"<<"和流提取运算符">>",使之能用于该矩阵的输入和输出. Input 两个2行3列矩阵 Output 矩阵之和 Sample Input 1 2 34 5 67 8 91 2 3 Sa

重载()运算符和重载强制类型转换

// 研究了半宿.最终弄清楚了 // 写了这段測试代码能够非常好的演示效果 class CConvert { public: CConvert(){m_nValue = 10;} // 重载()运算符 int operator ()(); // 重载int强制类型转换 operator int(); protected: private: int m_nValue; }; int CConvert::operator ()() { return m_nValue; } CConvert::ope

C++自学笔记_重载运算符_《C++ Primer》

#include <iostream> #include <stdexcept> using namespace std; class CheckedPtr{ public: CheckedPtr(int *b,int *e,int *c): beg(b),end(e),curr(c){ } CheckedPtr(const CheckedPtr &obj): //复制构造函数 beg(obj.beg),end(obj.end),curr(obj.curr){ } Chec

作为类的成员函数,重载运算符只能有一个参数

1 overload a operator of a class, you can only use one para., this pointer is automatically used. class Rational { public: //not correct since this ponit would be used automatically. //Rational operator+ (const Rational& lhs, const Rational& rhs);

C++习题 复数类--重载运算符+,-,*,/

Description 定义一个复数类Complex,重载运算符"+","-","*","/",使之能用于复数的加.减.乘.除.运算符重载函数作为Complex类的成员函数.编写程序,分别求两个复数之和.差.积和商. Input 两个复数 Output 两个复数之和.差.积和商 Sample Input 3 4 5 -10 Sample Output c1+c2=(8.00,-6.00i) c1-c2=(-2.00,14.00

operator重载运算符

1.重载运算符的函数一般格式如下 函数类型    operator  运算符名称    (形参表列) {对运算符的重载处理} 例如,想将“+”用于Complex(复数)的加法运算,函数的原型可以是这样的: Complex operator + (Complex & c1,Complex &c2); operator+函数表示对运算符+重载.其中,operator是关键字,专门用于定义重载运算符的函数的,运算符名称就是C++提供给用户的预定运算符. 注意:函数名是由operator和运算符组

C++学习27 用全局函数重载运算符

运算符重载函数既可以声明为类的成员函数,也可以声明为所有类之外的全局函数. 运算符重载函数作为类的成员函数 将运算符重载函数声明为类的成员函数时,二元运算符的参数只有一个,一元运算符不需要参数.之所以少一个参数,是因为这个参数是隐含的. 例如,上节的 complex 类中重载了加法运算符: complex operator+(const complex & A)const; 当执行: c3 = c1 + c2; 会被转换为: c3 = c1.operator+(c2); 通过 this 指针隐式

C# 重载运算符

如果你想让自己定义的类型可以用运算符进行运算,那么可以通过重载运算符来实现: 示例: class Salary { public int RMB { get; set; } public static Salary operator +(Salary s1, Salary s2) { return new Salary { RMB = s1.RMB + s2.RMB }; } } //调用 Salary mikeIncome = new Salary { RMB = 10 }; Salary r

C++习题 复数类--重载运算符2+

Description 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算.参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意.例如,c1+c2,i+c1,c1+i均合法(设i为整数,c1,c2为复数).编写程序,分别求两个复数之和.整数和复数之和. Input 两个复数 一个复数和一个整数 一个整数和一个复数 Output 两个复数之和.复数和整数之和,整数和复数之和. Sample Input 3 4 5 -10 3 4 5 5 3 4 Sa