当前位置: 首页 > news >正文

福田专业网站建设公司seo1新地址在哪里

福田专业网站建设公司,seo1新地址在哪里,企业vi包含哪些内容,外国贸易平台目录 前言ostream和istream自定义类型的流插入重载自定义类型的流提取重载解决私有问题日期类总接口 前言 我们在上一节实现日期类时,在输入和输出打印时,经常会调用两个函数: 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 i=1;
cout<<i ——》等价于cout.operator<<(i)
double d=1.1
cout<<d ——》等价于cout.operator<<(d)
cout能自动识别类型,本质上是因为这些流插入重载自动构成函数重载。
cin也一样。

  当我们想要cout一个自定义类型,即cout<<d1,发现代码会报错,因为库里面没有对应写自定义类型的输出,我们要自己重载写一个。

自定义类型的流插入重载

  虽然上图中函数的参数只有一个,但我们要知道的是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;然后再输出。

到此刻,我们调用cout<<d1时发现会报错,这是为什么呢?

我们将调用void Date::operator<<(ostream& out)这个函数的式子写出来其实是: d1.operator<<(cout);写成这样就可以正常调用。
即d1<<cout。

  其实原因很简单:在运算符重载过程中,参数顺序和操作数顺序必须保持一致。
在这里插入图片描述

我们实际想要写成:cout<<d1,
则参数顺序则应该为:ostream Date
但是存在一个问题,这个函数是Date类中的成员函数,它有隐含一个this指针,把第一个参数占用了,ostream则不能作为第一个参数。此时我们只能将其重载成全局函数。

总结起来就是:operator<<想重载为成员函数,可以,但是使用时d1<<cout不符合习惯,建议重载成全局函数。
如下:

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 || _day<0 || _day>GetMonthDay(_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.dt0577.cn/news/191.html

相关文章:

  • 网站建设柒金手指花总15外贸网站谷歌seo
  • 用asp做网站流程游戏推广员好做吗
  • 用JSP做电商网站百度搜索引擎优化怎么做
  • 网站建设后期需要后期做的刷网站软件
  • 建设淘宝网站的市场分析seo 推广怎么做
  • 怎样做diy家具网站微信管理软件
  • 唐山的做网站的企业注册网站查询
  • 化妆品网站建设操作可行性分析旺道网站优化
  • 建设网站物业经理上岗证陈俊华百度指数分析大数据
  • 百度开放云搭建网站网站是如何建立的
  • seo网站优化插件排名优化公司哪家好
  • 简述电子政务网站设计的技术拓客平台有哪些
  • 新加坡网站建设公司在线收录
  • 东莞疫情最新消息今天seo网站排名推广
  • 利用网站制作网页快照网站
  • 宁夏网站建设电话武汉网站关键词推广
  • 旧房翻新装修百度seo优化方案
  • 公司网站建设意见和建议推56论坛
  • 制作网站题材南京网络营销服务
  • wordpress32m网络优化工程师为什么都说坑人
  • o2o网站建设方案讲解运营怎么做
  • 平面设计最常用的网站杭州百度竞价推广公司
  • 网站论坛制作seo优化信
  • 微小旅行社能否做网站百度竞价包年推广公司
  • 最新企业网站模板win7优化
  • 推广你公司网站百度站长工具
  • 网站建设的几个阶段网站推广四个阶段
  • 安康市网页设计培训超级优化
  • 北京开发区建设委员会网站资格宁波seo优化外包公司
  • wordpress网盘外链插件最好的网站优化公司