C++运算符重载——输入/输出运算符
为了与IO标准库一致,重载输入输出运算符函数的第一个行参应该是流的引用,第二个行参是对象的引用。
如果重载为类的成员函数,第一个行参应该是对象的引用,第二个行参是流的引用。
使用方式是 ClassObj << cout 这样与标准IO库就不一致了,所以输入输出运算符不能重载为类的成员函数,可以重载为类的友元函数和普通函数。
通常重载输出运算符的第二个行参是const的,因为输出一个类不许要更改它;
但是重载输入运算符的第二个行参必须是非const的,否则无法赋值。
重载的基本方法如下:
- //重载输出运算符
- ostream& operator<<(ostream& out, const ClassType& obj)
- {
- out << /*想要输出的内容1*/ << /*想要输出的内容2*/ <<...;
- return out;
- }
- //重载输入运算符
- istream& operator<<(istream& in, ClassType& obj)
- {
- in >> /*想要输入的内容1*/ >> /*想要输入的内容2*/ >>...;
//检查错误 和 文件结束的可能性- return in;
- }
例子:类Persion使用友元函数的方式重载了输入输出运算符,类PersionA使用了普通函数重载了输入输出运算符。
- #include <cstring>
- #include <iostream>
- using namespace std;
- class Persion
- {
- public:
- //constructor
- Persion(const char *pname, unsigned int ag, double he,double we):age(ag),height(he),weight(we){strcpy(name,pname);}
- //operator overload : <<
- friend ostream& operator<<(ostream& out, const Persion& ref)
- {
- out<<ref.name<<"\t"<<ref.age<<"\t"<<ref.height<<"\t"<< ref.weight;
- return out;
- }
- //operator overload : >>
- friend istream& operator>>(istream& in, Persion& ref)
- {
- char name[] = {};
- unsigned int ag = ;
- double he = ;
- double we = ;
- in>>name>>ag>>he>>we;
- //check that if the inputs succeeded
- if (in)
- {//Input Succeeded
- strcpy(ref.name, name);
- ref.age = ag;
- ref.height = he;
- ref.weight = we;
- }
- else
- {//Input Failed
- }
- return in;
- }
- private:
- char name[];
- unsigned int age;
- double height;
- double weight;
- };
- class PersionA
- {
- public:
- //constructor
- PersionA(const char *pname, unsigned int ag, double he,double we):age(ag),height(he),weight(we){strcpy(name,pname);}
- //GetData
- char* GetName(void){return name;}
- unsigned int& GetAge(void){return age;}
- double& GetHeight(void){return height;}
- double& GetWeight(void){return weight;}
- private:
- char name[];
- unsigned int age;
- double height;
- double weight;
- };
- //operator overload : <<
- ostream& operator<<(ostream& out, PersionA& ref)
- {
- out<<ref.GetName()<<"\t"<<ref.GetAge()<<"\t"<<ref.GetHeight()<<"\t"<<ref.GetWeight();
- return out;
- }
- //operator overload : >>
- istream& operator>>(istream& in, PersionA& ref)
- {
- char name[] = {};
- unsigned int ag = ;
- double he = ;
- double we = ;
- in>>name>>ag>>he>>we;
- //check that if the inputs succeeded
- if (in)
- {//Input Succeeded
- strcpy(ref.GetName(), name);
- ref.GetAge() = ag;
- ref.GetHeight() = he;
- ref.GetWeight() = we;
- }
- else
- {//Input Failed
- }
- return in;
- }
- int main(void)
- {
- Persion per("Jack", , , );
- cout << per << endl;
- cin>>per;
- cout << per << endl;
- PersionA perA("Jack", , , );
- cout << perA << endl;
- cin>>perA;
- cout << perA << endl;
- return ;
- }
C++运算符重载——输入/输出运算符的更多相关文章
- C++的重载流输出运算符
// 下列代码输出什么?#include <iostream>#include <string>// typedef basic_ostream<char> ost ...
- C++重载流插入和流输出运算符
demo: /* Name: 重载输入输出流运算符使力代码 Copyright: qianshou Author: zhaozhe Date: 07/12/13 00:11 Description: ...
- C++中的运算符重载
首先思考以下几个问题: 1.什么是运算符重载? 2.为什么要重载运算符,它有什么用? 3.可以重载哪些运算符? 4.重载运算符有哪些规则? 一.基本概念 我们在程序中使用各种操作符,比如加(+).赋值 ...
- C++重载流插入运算符和流提取运算符【转】
C++的流插入运算符“<<”和流提取运算符“>>”是C++在类库中提供的,所有C++编译系统都在类库中提供输入流类istream和输出流类ostream.cin和cout分别是 ...
- C++学习6-面向对象编程基础(运算符重载、类的派生与继承、命名空间)
运算符重载 重载的运算符是具有特殊名字的函数:它们的名字由关键字operator和其后要定义的运算符号共同组成.重载的运算符是遵循函数重载的选择原则,根据不同类型或不同参数来选择不同的重载运算符. 运 ...
- 新标准C++程序设计读书笔记_运算符重载
形式 返回值类型 operator 运算符(形参表) { …… } 运算符重载 (1)运算符重载的实质是函数重载(2)可以重载为普通函数,也可以重载为成员函数 class Complex { publ ...
- sdut 4-1 复数类的运算符重载
4-1 复数类的运算符重载 Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 通过本题目的练习能够掌握成员运算符重载及友元运算符重载 要求定义一个复数类.重 ...
- C++基础 学习笔记五:重载之运算符重载
C++基础 学习笔记五:重载之运算符重载 什么是运算符重载 用同一个运算符完成不同的功能即同一个运算符可以有不同的功能的方法叫做运算符重载.运算符重载是静态多态性的体现. 运算符重载的规则 重载公式 ...
- 五、C++运算符重载,使面向对象编程更方便
复数类CComplex 编译器做对象运算的时候,会调用对象的运算符重载函数(优先调用成员方法):如果没有成员方法,就砸全局作用域找合适的运算符重载函数 ++和--运算符是单目运算符,在参数列表里放上一 ...
随机推荐
- 洛谷P1188PASTE题解
题目 这个题主要是一个考分类讨论的模拟题,做这个提的时候首先要脑子清醒,才可以清楚地写出怎么模拟来. \(Code\) #include <iostream> #include <a ...
- Mock6 moco框架中如何加入header
新建一个 startupWithHeader.json,这次在request里面添加了headers属性 [ { "description": "这是一个带header的 ...
- ASP.NET概念
ASP.NET :是一个开发框架,用于通过 HTML.CSS.JavaScript 以及服务器脚本来构建网页和网站. ASP.NET两种开发语言:VB C#
- cobbler批量安装操作
打开mirrors.aliyun.com/epel http://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm rpm -ivh h ...
- div高度随浏览器窗口高度变化;
通过实际测试,按照网上的说法通过设置html,body{height: 100%:}, 然后让div以100%继承body的高度,这种做法是错误的,必须得上级有个设置固定的高度. 原生js代码(参照网 ...
- busybox(三)最小根文件系统
目录 busybox(三)最小根文件系统 引入 构建终端 构造inittab 配置应用程序 构建C库 制作映像文件yaffs title: busybox(三)最小根文件系统 tag: arm dat ...
- Docker:Docker 性质及版本选择 [三]
一.Docker的性质 Docker的组成其实很简单.你需要搭建registry,专属于你自己的私有仓库,然后就是docker的镜像和docker的容器.Docker的镜像,就类似与windos的系统 ...
- postgreSql 常用操作总结
0. 启动pgsl数据库 pg_ctl -D /xx/pgdata start 1. 查看pgsl版本 pg_ctl --version 1. 命令行登录数据库 psql -U username -d ...
- Java设计模式之抽象工厂
概述 设计模式(Design Pattern)是一套被反复使用.多数人知晓的.经过分类的.代码设计经验的总结. 使用设计模式的目的:为了代码可重用性.让代码更容易被他人理解.保证代码可靠性. 设计模式 ...
- IIS--互联网信息服务
IIS--互联网信息服务 1.IIS是微软出品的一个服务器插件 2.IIS的功能:1)发布web网站 2)发布ftp站点 WEB服务器:1.监听TCP80端口 --- http://www.baidu ...