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 编译器做对象运算的时候,会调用对象的运算符重载函数(优先调用成员方法):如果没有成员方法,就砸全局作用域找合适的运算符重载函数 ++和--运算符是单目运算符,在参数列表里放上一 ...
随机推荐
- 修改已有git仓库的远程仓库指向
$ git remote set-url origin git@github.com:test/thinkphp.git 或者 $ git config -e 直接编辑其中origin的url就行了, ...
- go实现json数组嵌套
go实现json数组嵌套 引用包 "encoding/json" 定义以下结构体 type person struct { Name string `json:"name ...
- Js中常用知识点(typeof、instanceof、动态属性、变量作用域)
1.Js中各类型的常量表示形式:Number:number String:string Object:objec 2.typeof运算符在Js中的使用:用于判断某一对象是何种类型,返回值 ...
- Spring MVC -- Spring框架入门(IoC和DI)
Spring MVC是Spring框架中用于Web应用开发的一个模块.Spring MVC的MVC是Model-View-Controller的缩写.它是一个广泛应用于图像化用户交互开发中的设计模式, ...
- Tomcat 日志文件分割
新到公司, 拿到了前辈们留下的 程序 “病历书” , 上面记载了项目上的一些 经常会犯的毛病, 还有相应的解决方法. 其中有的是因为后台 代码逻辑上的一些原因 , N手代码通病了吧 (这个还是以后再 ...
- openssl命令行将pfx格式转.key和.crt文件,Apache适用
以前使用的windows平台作为php运行的平台,感觉整体速度太慢,于是现在改为centos系统的linux平台.需要把windows中用pfx证书转为appache用到key和crt组成的证书 将* ...
- phpmyadmin低权限getshell
账号:‘localhost’@'@” 密码:为空 可获得一个低权限账号 利用方法: Mysql可以把指定的文件写进表 CREATE TABLE `test`.`a` (`a1` TEXT NOT NU ...
- Docker:网络及数据卷设置 [四]
一.Docker网络设置 默认情况下,docker会创建一个桥接网卡[docker 0],docker有2种映射方式,一种是随机映射,一种是指定映射 提示:生产场景一般不使用随机映射,但是随机映射的好 ...
- C#获取根目录的方法总结
1.控制台应用程序 static void Main(string[] args) { //1.Environment.CurrentDirectory Console.WriteLine(Envir ...
- nnet3 TDNN chunk, left-context, right-context
chunk-width 数据块的宽度 NnetIo name=="input" indexes,left-context+num-frame+right-context=5+7+6 ...