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

【C++】类和对象——流插入和流提取运算符重载

目录

  • 前言
  • 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.lryc.cn/news/410925.html

相关文章:

  • Vmware ubuntu20.04 虚拟文件夹
  • 人工智能学习笔记 - 初级篇Ⅱ - 图形可视化 - 第11节: 绘制带填充区域的图表
  • 使用STM32实现一个线性代数计算器
  • 我在高职教STM32——串口通信(4)
  • Redis 缓存中间件 缓存数据库
  • 51、PHP 实现简单的快速排序
  • 如何应对机器视觉软件中时间篡改与许可绕过的挑战?
  • python文件的读写
  • 2024下《网络工程师》案例简答题,刷这些就够了!
  • Astro 实现TodoList网页应用案例
  • 计算机毕业设计Hadoop+Spark旅游景点可视化 旅游景点推荐系统 景区游客满意度预测与优化 Apriori算法 景区客流量预测 旅游大数据 景点规划
  • MySQL存储
  • 手势传感器 - 从零开始认识各种传感器【第十八期】
  • 【未来餐饮】 配送设置
  • 移动式气象设备:灵活应对,精准监测的气象先锋
  • 【AI落地应用实战】DAMODEL深度学习平台部署+本地调用ChatGLM-6B解决方案
  • 英伟达开始引领下一波浪潮:物理AI
  • SQLServer设置端口
  • 诊断技巧分享 | 用WPS500压力传感器测试空调压力波形?
  • W1R3S靶机全通详细教程
  • GitHub Revert Merge Commit的现象观察和对PR的思考
  • 使用JavaFx Fxml笔记
  • 友盟U-APM——优秀的前端性能监控工具
  • 人工智能与机器学习原理精解【10】
  • TypeScript 简介
  • 什么是知识库?为什么我需要一个?
  • MySQL学习(16):视图
  • android13关机按钮 去掉长按事件 去掉启动到安全模式 删除关机长按
  • 递归求数组和
  • MySQL数据库介绍