为了与IO标准库一致,重载输入输出运算符函数的第一个行参应该是流的引用,第二个行参是对象的引用。

如果重载为类的成员函数,第一个行参应该是对象的引用,第二个行参是流的引用。

使用方式是 ClassObj << cout 这样与标准IO库就不一致了,所以输入输出运算符不能重载为类的成员函数,可以重载为类的友元函数和普通函数。

通常重载输出运算符的第二个行参是const的,因为输出一个类不许要更改它;

但是重载输入运算符的第二个行参必须是非const的,否则无法赋值。

重载的基本方法如下:

  1. //重载输出运算符
  2. ostream& operator<<(ostream& out, const ClassType& obj)
  3. {
  4. out << /*想要输出的内容1*/ << /*想要输出的内容2*/ <<...;
  5. return out;
  6. }
  7.  
  8. //重载输入运算符
  9. istream& operator<<(istream& in, ClassType& obj)
  10. {
  11. in >> /*想要输入的内容1*/ >> /*想要输入的内容2*/ >>...;
    //检查错误 和 文件结束的可能性
  12. return in;
  13. } 

例子:类Persion使用友元函数的方式重载了输入输出运算符,类PersionA使用了普通函数重载了输入输出运算符。

  1. #include <cstring>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Persion
  6. {
  7. public:
  8. //constructor
  9. Persion(const char *pname, unsigned int ag, double he,double we):age(ag),height(he),weight(we){strcpy(name,pname);}
  10.  
  11. //operator overload : <<
  12. friend ostream& operator<<(ostream& out, const Persion& ref)
  13. {
  14. out<<ref.name<<"\t"<<ref.age<<"\t"<<ref.height<<"\t"<< ref.weight;
  15. return out;
  16. }
  17. //operator overload : >>
  18. friend istream& operator>>(istream& in, Persion& ref)
  19. {
  20. char name[] = {};
  21. unsigned int ag = ;
  22. double he = ;
  23. double we = ;
  24.  
  25. in>>name>>ag>>he>>we;
  26.  
  27. //check that if the inputs succeeded
  28. if (in)
  29. {//Input Succeeded
  30. strcpy(ref.name, name);
  31. ref.age = ag;
  32. ref.height = he;
  33. ref.weight = we;
  34. }
  35. else
  36. {//Input Failed
  37. }
  38.  
  39. return in;
  40. }
  41. private:
  42. char name[];
  43. unsigned int age;
  44. double height;
  45. double weight;
  46. };
  47.  
  48. class PersionA
  49. {
  50. public:
  51. //constructor
  52. PersionA(const char *pname, unsigned int ag, double he,double we):age(ag),height(he),weight(we){strcpy(name,pname);}
  53. //GetData
  54. char* GetName(void){return name;}
  55. unsigned int& GetAge(void){return age;}
  56. double& GetHeight(void){return height;}
  57. double& GetWeight(void){return weight;}
  58.  
  59. private:
  60. char name[];
  61. unsigned int age;
  62. double height;
  63. double weight;
  64. };
  65.  
  66. //operator overload : <<
  67. ostream& operator<<(ostream& out, PersionA& ref)
  68. {
  69. out<<ref.GetName()<<"\t"<<ref.GetAge()<<"\t"<<ref.GetHeight()<<"\t"<<ref.GetWeight();
  70. return out;
  71. }
  72. //operator overload : >>
  73. istream& operator>>(istream& in, PersionA& ref)
  74. {
  75. char name[] = {};
  76. unsigned int ag = ;
  77. double he = ;
  78. double we = ;
  79.  
  80. in>>name>>ag>>he>>we;
  81.  
  82. //check that if the inputs succeeded
  83. if (in)
  84. {//Input Succeeded
  85. strcpy(ref.GetName(), name);
  86. ref.GetAge() = ag;
  87. ref.GetHeight() = he;
  88. ref.GetWeight() = we;
  89. }
  90. else
  91. {//Input Failed
  92. }
  93.  
  94. return in;
  95. }
  96. int main(void)
  97. {
  98. Persion per("Jack", , , );
  99. cout << per << endl;
  100. cin>>per;
  101. cout << per << endl;
  102.  
  103. PersionA perA("Jack", , , );
  104. cout << perA << endl;
  105. cin>>perA;
  106. cout << perA << endl;
  107. return ;
  108. }

C++运算符重载——输入/输出运算符的更多相关文章

  1. C++的重载流输出运算符

    // 下列代码输出什么?#include <iostream>#include <string>// typedef basic_ostream<char> ost ...

  2. C++重载流插入和流输出运算符

    demo: /* Name: 重载输入输出流运算符使力代码 Copyright: qianshou Author: zhaozhe Date: 07/12/13 00:11 Description: ...

  3. C++中的运算符重载

    首先思考以下几个问题: 1.什么是运算符重载? 2.为什么要重载运算符,它有什么用? 3.可以重载哪些运算符? 4.重载运算符有哪些规则? 一.基本概念 我们在程序中使用各种操作符,比如加(+).赋值 ...

  4. C++重载流插入运算符和流提取运算符【转】

    C++的流插入运算符“<<”和流提取运算符“>>”是C++在类库中提供的,所有C++编译系统都在类库中提供输入流类istream和输出流类ostream.cin和cout分别是 ...

  5. C++学习6-面向对象编程基础(运算符重载、类的派生与继承、命名空间)

    运算符重载 重载的运算符是具有特殊名字的函数:它们的名字由关键字operator和其后要定义的运算符号共同组成.重载的运算符是遵循函数重载的选择原则,根据不同类型或不同参数来选择不同的重载运算符. 运 ...

  6. 新标准C++程序设计读书笔记_运算符重载

    形式 返回值类型 operator 运算符(形参表) { …… } 运算符重载 (1)运算符重载的实质是函数重载(2)可以重载为普通函数,也可以重载为成员函数 class Complex { publ ...

  7. sdut 4-1 复数类的运算符重载

    4-1 复数类的运算符重载 Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 通过本题目的练习能够掌握成员运算符重载及友元运算符重载 要求定义一个复数类.重 ...

  8. C++基础 学习笔记五:重载之运算符重载

    C++基础 学习笔记五:重载之运算符重载 什么是运算符重载 用同一个运算符完成不同的功能即同一个运算符可以有不同的功能的方法叫做运算符重载.运算符重载是静态多态性的体现. 运算符重载的规则 重载公式 ...

  9. 五、C++运算符重载,使面向对象编程更方便

    复数类CComplex 编译器做对象运算的时候,会调用对象的运算符重载函数(优先调用成员方法):如果没有成员方法,就砸全局作用域找合适的运算符重载函数 ++和--运算符是单目运算符,在参数列表里放上一 ...

随机推荐

  1. 洛谷P1188PASTE题解

    题目 这个题主要是一个考分类讨论的模拟题,做这个提的时候首先要脑子清醒,才可以清楚地写出怎么模拟来. \(Code\) #include <iostream> #include <a ...

  2. Mock6 moco框架中如何加入header

    新建一个 startupWithHeader.json,这次在request里面添加了headers属性 [ { "description": "这是一个带header的 ...

  3. ASP.NET概念

    ASP.NET :是一个开发框架,用于通过 HTML.CSS.JavaScript 以及服务器脚本来构建网页和网站. ASP.NET两种开发语言:VB C#

  4. cobbler批量安装操作

    打开mirrors.aliyun.com/epel http://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm rpm -ivh h ...

  5. div高度随浏览器窗口高度变化;

    通过实际测试,按照网上的说法通过设置html,body{height: 100%:}, 然后让div以100%继承body的高度,这种做法是错误的,必须得上级有个设置固定的高度. 原生js代码(参照网 ...

  6. busybox(三)最小根文件系统

    目录 busybox(三)最小根文件系统 引入 构建终端 构造inittab 配置应用程序 构建C库 制作映像文件yaffs title: busybox(三)最小根文件系统 tag: arm dat ...

  7. Docker:Docker 性质及版本选择 [三]

    一.Docker的性质 Docker的组成其实很简单.你需要搭建registry,专属于你自己的私有仓库,然后就是docker的镜像和docker的容器.Docker的镜像,就类似与windos的系统 ...

  8. postgreSql 常用操作总结

    0. 启动pgsl数据库 pg_ctl -D /xx/pgdata start 1. 查看pgsl版本 pg_ctl --version 1. 命令行登录数据库 psql -U username -d ...

  9. Java设计模式之抽象工厂

    概述 设计模式(Design Pattern)是一套被反复使用.多数人知晓的.经过分类的.代码设计经验的总结. 使用设计模式的目的:为了代码可重用性.让代码更容易被他人理解.保证代码可靠性. 设计模式 ...

  10. IIS--互联网信息服务

    IIS--互联网信息服务 1.IIS是微软出品的一个服务器插件 2.IIS的功能:1)发布web网站 2)发布ftp站点 WEB服务器:1.监听TCP80端口 --- http://www.baidu ...