欢迎来到天天文库
浏览记录
ID:9235537
大小:378.22 KB
页数:13页
时间:2018-04-24
《05_流插入运算符和流提取运算符的重载》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、信息科学技术学院程序设计实习郭炜微博http://weibo.com/guoweiofpkuhttp://blog.sina.com.cn/u/3266490431刘家瑛微博http://weibo.com/pkuliujiaying信息科学技术学院《程序设计实习》郭炜刘家瑛流插入运算符和流提取运算符的重载(教材P218)问题•cout<<5<<“this”;为什么能够成立?•cout是什么?“<<”为什么能用在cout上?流插入运算符的重载cout是在iostream中定义的,ostream类的对象。“<<”能用在c
2、out上是因为,在iostream里对“<<”进行了重载。考虑,怎么重载才能使得cout<<5;和cout<<“this”都能成立?流插入运算符的重载有可能按以下方式重载成ostream类的成员函数:voidostream::operator<<(intn){……//输出n的代码return;}流插入运算符的重载cout<<5;即cout.operator<<(5);cout<<“this”;即cout.operator<<(“this”);怎么重载才能使得cout<<5<<“this”;成立?流插入运算符的重载os
3、tream&ostream::operator<<(intn){……//输出n的代码return*this;}ostream&ostream::operator<<(constchar*s){……//输出s的代码return*this;}流插入运算符的重载cout<<5<<“this”;本质上的函数调用的形式是什么?cout.operator<<(5).operator<<(“this”);流插入运算符的重载•假定下面程序输出为5hello,该补写些什么classCStudent{public:intnAge;};intm
4、ain(){CStudents;s.nAge=5;cout<>c;”,就能从键盘接受“a+bi”形式的输入,并且使得c.real=a,c.imag=b。例题intmain(){Complexc;intn
5、;cin>>c>>n;cout<#include#includeusingnamespacestd;classComplex{doublereal,imag;public:Complex(doubler=0,doublei=0):real(r),imag(i){};friendostream&operator<<(ostream&os,const
6、Complex&c);friendistream&operator>>(istream&is,Complex&c);};ostream&operator<<(ostream&os,constComplex&c){os<
此文档下载收益归作者所有