为了与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. JDK源代码学习-ArrayList、LinkedList、HashMap

    ArrayList.LinkedList.HashMap是Java开发中非常常见的数据类型.它们的区别也非常明显的,在Java中也非常具有代表性.在Java中,常见的数据结构是:数组.链表,其他数据结 ...

  2. MYSQL IN 出现的慢查询问题

    IN = https://blog.csdn.net/solmyr_biti/article/details/54293492 https://www.cnblogs.com/wxw16/p/6105 ...

  3. Java【第三篇】基本语法之--选择结构

    Java分支语句分类 分支语句根据一定的条件有选择地执行或跳过特定的语句,分为两类: if-else 语句 switch 语句 if-else语句语法格式 if(布尔表达式){ 语句或语句块; } i ...

  4. centos安装node环境

    一.安装wget yum install -y wget 二.下载node最新的安装包 wget https://nodejs.org/dist/v10.13.0/node-v10.13.0-linu ...

  5. Django admin修改密码

    django的admin用户被我多动症一样的测试,给密码弄丢了,需要重置. 从数据库重置的可能性为0,因为django对于密码有保护策略.考虑从运行程序的地方进行重置: 1.在程序的文件夹下,执行这样 ...

  6. CMS收集器和G1收集器优缺点

    首先要知道 Stop the world的含义(网易面试):不管选择哪种GC算法,stop-the-world都是不可避免的.Stop-the-world意味着从应用中停下来并进入到GC执行过程中去. ...

  7. “Django用户认证系统”学习资料收集

    首推追梦人物——Django用户认证系统 待续……

  8. Notepad++ --v7.5.8 (64bit) 安装目录显示插件(Explorer)

    https://blog.csdn.net/qq_24153697/article/details/83036761 最近想自己做一个小项目,用Notepad做IDE,但是发现已安装的Notepad没 ...

  9. 迅为IMX6开发板真实产品案例分享-专为研发用芯选择

    迅为IMX6开发板: Android4.4系统 Linux + Qt5.7系统 Ubuntu12.04系统 部分真实案例:HMI:3D打印机:医疗设备:工控机:触控一体机:车载终端 核心板兼容:IMX ...

  10. Coursera, Big Data 3, Integration and Processing (week 4)

    Week 4 Big Data Precessing Pipeline 上图可以generalize 成下图,也就是Big data pipeline some high level processi ...