成都记者留言网站,阿里企业邮箱登录,商务网站建设规划流程,网络营销推广微信hyhyk1效果好目录 前言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;
};