C++ //练习 14.12 你在7.5.1节的练习7.40(第261页)中曾经选择并编写了一个类,为它定义一个输入运算符并确保该运算符可以处理输入错误。

C++ Primer(第5版) 练习 14.12

练习 14.12 你在7.5.1节的练习7.40(第261页)中曾经选择并编写了一个类,为它定义一个输入运算符并确保该运算符可以处理输入错误。

环境:Linux Ubuntu(云服务器)
工具:vim
代码块
cpp 复制代码
class Date{
	public:
	Date();
	Date(size_t y, size_t m, size_t d): year(y), month(m), day(d) {}
	friend ostream &operator<<(ostream &, Date &);
	friend istream &operator>>(istream &, Date &);

	private:
	size_t year;
	size_t month;
	size_t day;
};

ostream &operator<< (ostream &output, Date &d){
	output<<d.year<<"-"<<d.month<<"-"<<d.day<<endl;
	return output;
}

istream &operator>> (istream &input, Date &d){
	input>>d.year>>d.month>>d.day;
	if(!input){
		d = Date(0, 0, 0);
	}
	return input;
}
相关推荐
念恒1230630 分钟前
继承(下) (Inheritance)
c++
小陈工1 小时前
Python Web开发入门(十七):Vue.js与Python后端集成——让前后端真正“握手言和“
开发语言·前端·javascript·数据库·vue.js·人工智能·python
海清河晏1112 小时前
数据结构 | 单循环链表
数据结构·算法·链表
H Journey2 小时前
C++之 CMake、CMakeLists.txt、Makefile
开发语言·c++·makefile·cmake
wuweijianlove6 小时前
算法性能的渐近与非渐近行为对比的技术4
算法
研究点啥好呢6 小时前
Github热门项目推荐 | 创建你的像素风格!
c++·python·node.js·github·开源软件
_dindong6 小时前
cf1091div2 C.Grid Covering(数论)
c++·算法
AI成长日志6 小时前
【Agentic RL】1.1 什么是Agentic RL:从传统RL到智能体学习
人工智能·学习·算法
lly2024066 小时前
C 标准库 - `<stdio.h>`
开发语言
沫璃染墨6 小时前
C++ string 从入门到精通:构造、迭代器、容量接口全解析
c语言·开发语言·c++