为了与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++运算符重载——输入/输出运算符的更多相关文章

  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. 常用的flex布局

    演示地址:https://xibushijie.github.io/static/flex.html

  2. python之异常处理和re模块补充

    一.re模块的补充 1.从一个字符串中获取要匹配的内容 findall:返回一个列表 2.search ***** 验证用户输入内容 '^正则规则$':返回一个对象,用group()取值 3.matc ...

  3. Django的View(视图)和路由系统

    一.Django的View(视图) 1.介绍 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一 ...

  4. Thinkphp5.1 ORM UML

    Thinkphp5.1 ORM  UML think-orm

  5. <Android基础>(四) Fragment Part 1

    Fragment 1)Fragment的简单用法 2)动态添加Fragment 3)在Fragment中模拟返回栈 4)Fragment和活动之间通信 第四章 Fragment Fragment是一种 ...

  6. 更换gcc工具链

    title: 更换gcc工具链 date: 2019/1/16 19:27:51 toc: true --- 更换gcc工具链 下载后解压到一个临时目录先看看文件结构 mkdir tmp tar xj ...

  7. Ceph集群搭建及Kubernetes上实现动态存储(StorageClass)

    集群准备 ceph集群配置说明   节点名称 IP地址 配置 作用 ceph-moni-0 10.10.3.150 centos7.5 4C,16G,200Disk 管理节点,监视器 monitor ...

  8. C#windows服务调试技巧

    1.创建项目 2.为了方便调试,设置为控制台程序 3.修改Service1代码 4.修改Main代码 这样当使用-console方式启动时,就是以普通的控制台方式启动,方便调试程序. 5.其它安装之类 ...

  9. [物理学与PDEs]第2章习题2 质量力有势时的能量方程

    试证明: 如果质量力有势, 即存在 $\phi$ 使 ${\bf F}=-\n \phi$, 那么理想流体的能量守恒方程的微分形式可写为 $$\bex \cfrac{\rd}{\rd t}\sex{e ...

  10. spring和mybatis的整合开发(基于MapperScannerConfigurer的整合开发(适用于复杂项目,接口较多的情况))

    在实际项目中,Dao层会包含很多接口,这样会导致spring配置文件过于臃肿.这时就需要采用扫描包的形式来配置mybaits中的映射器. 采用MapperScannerConfigurer来实现. M ...