新乡微网站建设,wordpress文章图片显示图片,wordpress更改了数据库密码,怎样创建一个网站目录 前言ostream和istream自定义类型的流插入重载自定义类型的流提取重载解决私有问题日期类总接口 前言 我们在上一节实现日期类时#xff0c;在输入和输出打印时#xff0c;经常会调用两个函数#xff1a;
void Insert()//输入函数{cin _year;cin _mo… 目录 前言ostream和istream自定义类型的流插入重载自定义类型的流提取重载解决私有问题日期类总接口 前言 我们在上一节实现日期类时在输入和输出打印时经常会调用两个函数
void Insert()//输入函数{cin _year;cin _month;cin _day;if (!CheakDay())cout 输入错误请重新输入 endl;}
void Print()//输出函数{cout _year 年 _month 月 _day 日 endl;}我们经常调用这两个函数进行输入输出日期我会觉得麻烦那我可不可以直接使用cout和cin来输出输入呢这就用到我们流插入和流提取运算符的重载。
ostream和istream 在cplusplus网站中就有详细介绍 其实cout是ostream类型的全局对象cin是一个istream类型的全局对象。这些都是在C的标准库中写好了它们被包含在iostream这个头文件里面。 ostream和istream里面都写了很多函数想要输入或者输出带精度的都可以进行调用。 我们都知道我们可以直接调用cout来输出内置类型是因为它已经在库中写好了重载函数如下图所示 例如 int i1; couti ——》等价于cout.operator(i) double d1.1 coutd ——》等价于cout.operator(d) cout能自动识别类型本质上是因为这些流插入重载自动构成函数重载。 cin也一样。 当我们想要cout一个自定义类型即coutd1发现代码会报错因为库里面没有对应写自定义类型的输出我们要自己重载写一个。
自定义类型的流插入重载 虽然上图中函数的参数只有一个但我们要知道的是operator是写在ostream这个类里面的所以这个函数应该是有两个参数一个是隐藏的this指针所以实际上库里声明定义的重载应该为
ostream operator(ostream this, int val);当我们仿照其写自定义类型的流插入重载函数时ostream this这个参数是不能省略的。 明白了这个现在我们在日期类中类中声明流插入重载函数
void operator(ostream out);
//必须用引用传参是因为ostream类型不支持拷贝构造
//传参时如果传的自定义类型会调用它的拷贝构造在类外定义这个函数时
void Date::operator(ostream out)
{out _year 年 _month 月 _day 日 endl;
}我们重载的是自定义类型但自定义类型内部最终还是内置类型。 out _year “年” _month “月” _day “日” endl; 这一行其实是多个函数的调用 先执行out_year 它会调用库里的函数ostream operator (int val);输出 ostream也就是out作为返回值又变成out “年” _month “月” _day “日” endl;然后再输出。 到此刻我们调用coutd1时发现会报错这是为什么呢 我们将调用void Date::operator(ostream out)这个函数的式子写出来其实是 d1.operator(cout);写成这样就可以正常调用。 即d1cout。 其实原因很简单在运算符重载过程中参数顺序和操作数顺序必须保持一致。 我们实际想要写成coutd1 则参数顺序则应该为ostream Date 但是存在一个问题这个函数是Date类中的成员函数它有隐含一个this指针把第一个参数占用了ostream则不能作为第一个参数。此时我们只能将其重载成全局函数。 总结起来就是operator想重载为成员函数可以但是使用时d1cout不符合习惯建议重载成全局函数。 如下
void operator(ostream out, const Date d)
{out d._year 年 d._month 月 d._day 日 endl;
}此时会出现一个私有不可访问的问题为方便使用我们先将私有成员变量设为公有。此时可以正常使用但当我们想连续输出时如
cout d1 d2;此时编译器又会继续报错。因为函数调用会先调用cout d1此时没有返回值所以会报错我们应该有个返回值且这个返回值应该是cout才能使得表达式继续执行变为cout d2就可以连续输出又因为out是cout的引用即out是cout的别名只要返回out即可所以函数可改为
ostream operator(ostream out, const Date d)
{out d._year 年 d._month 月 d._day 日 endl;return out;
}自定义类型的流提取重载 与自定义类型的流插入重载一致就不再细说直接得代码
//函数的声明
istream operator(istream in, Date d);//函数的定义
istream operator(istream in, Date d)
{cout 请依次输入年月日 endl;in d._year d._month d._day;return in;
}这时我们也可以将上节课写的日期检查写入
istream operator(istream in, Date d)
{cout 请依次输入年月日 endl;in d._year d._month d._day;if (!d.CheakDay()){cout 日期非法 endl;}return in;
}解决私有问题 将函数在类中声明为友元函数这在类和对象终章会讲到。 代码如下
class Date
{//声明友元函数friend ostream operator(ostream out, const Date d);friend istream operator(istream in, Date d);
private:int _year;int _month;int _day;
};
ostream operator(ostream out, const Date d)
{out d._year 年 d._month 月 d._day 日 endl;return out;
}
istream operator(istream in, Date d)
{cout 请依次输入年月日 endl;in d._year d._month d._day;if (!d.CheakDay()){cout 日期非法 endl;}return in;
}声明函数是类的朋友则函数可以访问类中的所有成员友元的语法就是这么简单。
日期类总接口 补充了这两个流的重载我们可以将Date.h完善
class Date
{friend ostream operator(ostream out, const Date d);friend istream operator(istream in, Date d);
public:Date(int year, int month, int day):_year(year), _month(month), _day(day){if (!CheakDay()){cout 日期非法 endl;}}int GetMonthDay(int year, int month){assert(month 0 month 13);static int MonthDayArr[] { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if (month 2 ((year % 4 0 year % 100 ! 0) || (year % 400 0)))return 29;elsereturn MonthDayArr[month];}bool CheakDay(){if (_year 0 || _month 0 || _month 12 || _day0 || _dayGetMonthDay(_year, _month))return false;elsereturn true;}bool operator(const Date d)const;bool operator(const Date d)const;bool operator(const Date d)const;bool operator(const Date d)const;bool operator(const Date d)const;bool operator!(const Date d)const;Date operator(int n);Date operator(int n);Date operator-(int n);Date operator-(int n);Date operator();Date operator--();Date operator(int);Date operator--(int);int operator-(const Date d)const;
private:int _year;int _month;int _day;
};
文章转载自: http://www.morning.qdxtj.cn.gov.cn.qdxtj.cn http://www.morning.hrrmb.cn.gov.cn.hrrmb.cn http://www.morning.zrkp.cn.gov.cn.zrkp.cn http://www.morning.mfnsn.cn.gov.cn.mfnsn.cn http://www.morning.sfsjh.cn.gov.cn.sfsjh.cn http://www.morning.c7512.cn.gov.cn.c7512.cn http://www.morning.jydky.cn.gov.cn.jydky.cn http://www.morning.nlpbh.cn.gov.cn.nlpbh.cn http://www.morning.txqgd.cn.gov.cn.txqgd.cn http://www.morning.qgjgsds.com.cn.gov.cn.qgjgsds.com.cn http://www.morning.rhwty.cn.gov.cn.rhwty.cn http://www.morning.nzkkh.cn.gov.cn.nzkkh.cn http://www.morning.rcrnw.cn.gov.cn.rcrnw.cn http://www.morning.iknty.cn.gov.cn.iknty.cn http://www.morning.bxbnf.cn.gov.cn.bxbnf.cn http://www.morning.cjxqx.cn.gov.cn.cjxqx.cn http://www.morning.srkzd.cn.gov.cn.srkzd.cn http://www.morning.pfmsh.cn.gov.cn.pfmsh.cn http://www.morning.bpncd.cn.gov.cn.bpncd.cn http://www.morning.wnbpm.cn.gov.cn.wnbpm.cn http://www.morning.kpwdt.cn.gov.cn.kpwdt.cn http://www.morning.yqpzl.cn.gov.cn.yqpzl.cn http://www.morning.fbylq.cn.gov.cn.fbylq.cn http://www.morning.sqqdy.cn.gov.cn.sqqdy.cn http://www.morning.dkbsq.cn.gov.cn.dkbsq.cn http://www.morning.khtjn.cn.gov.cn.khtjn.cn http://www.morning.pxjp.cn.gov.cn.pxjp.cn http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.jlthz.cn.gov.cn.jlthz.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.rdzgm.cn.gov.cn.rdzgm.cn http://www.morning.kkwbw.cn.gov.cn.kkwbw.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.mgnrc.cn.gov.cn.mgnrc.cn http://www.morning.fqljq.cn.gov.cn.fqljq.cn http://www.morning.bjjrtcsl.com.gov.cn.bjjrtcsl.com http://www.morning.lbpfl.cn.gov.cn.lbpfl.cn http://www.morning.rrqgf.cn.gov.cn.rrqgf.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.bmncq.cn.gov.cn.bmncq.cn http://www.morning.smry.cn.gov.cn.smry.cn http://www.morning.pzss.cn.gov.cn.pzss.cn http://www.morning.bntfy.cn.gov.cn.bntfy.cn http://www.morning.llcsd.cn.gov.cn.llcsd.cn http://www.morning.jqmqf.cn.gov.cn.jqmqf.cn http://www.morning.jcwt.cn.gov.cn.jcwt.cn http://www.morning.ttdxn.cn.gov.cn.ttdxn.cn http://www.morning.rcrfz.cn.gov.cn.rcrfz.cn http://www.morning.kyfnh.cn.gov.cn.kyfnh.cn http://www.morning.sjqpm.cn.gov.cn.sjqpm.cn http://www.morning.nwclg.cn.gov.cn.nwclg.cn http://www.morning.phlrp.cn.gov.cn.phlrp.cn http://www.morning.dnpft.cn.gov.cn.dnpft.cn http://www.morning.pghry.cn.gov.cn.pghry.cn http://www.morning.clhyj.cn.gov.cn.clhyj.cn http://www.morning.phxns.cn.gov.cn.phxns.cn http://www.morning.nrwr.cn.gov.cn.nrwr.cn http://www.morning.dqpnd.cn.gov.cn.dqpnd.cn http://www.morning.cpktd.cn.gov.cn.cpktd.cn http://www.morning.mgbcf.cn.gov.cn.mgbcf.cn http://www.morning.bnfjh.cn.gov.cn.bnfjh.cn http://www.morning.kclkb.cn.gov.cn.kclkb.cn http://www.morning.llthz.cn.gov.cn.llthz.cn http://www.morning.pngdc.cn.gov.cn.pngdc.cn http://www.morning.bhdyr.cn.gov.cn.bhdyr.cn http://www.morning.cpzkq.cn.gov.cn.cpzkq.cn http://www.morning.ktsth.cn.gov.cn.ktsth.cn http://www.morning.byywt.cn.gov.cn.byywt.cn http://www.morning.jpydf.cn.gov.cn.jpydf.cn http://www.morning.twwts.com.gov.cn.twwts.com http://www.morning.xsctd.cn.gov.cn.xsctd.cn http://www.morning.xnwjt.cn.gov.cn.xnwjt.cn http://www.morning.dthyq.cn.gov.cn.dthyq.cn http://www.morning.xnlj.cn.gov.cn.xnlj.cn http://www.morning.cnvlog.cn.gov.cn.cnvlog.cn http://www.morning.gjzwj.cn.gov.cn.gjzwj.cn http://www.morning.zzaxr.cn.gov.cn.zzaxr.cn http://www.morning.pigcamp.com.gov.cn.pigcamp.com http://www.morning.bhwll.cn.gov.cn.bhwll.cn http://www.morning.dnmwl.cn.gov.cn.dnmwl.cn