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;
}
相关推荐
随意起个昵称5 小时前
【背包dp】输出方案路径
c++·动态规划
言乐65 小时前
Python根据无法识别搜索词找出可能输入内容模型
开发语言·python·django·virtualenv·pygame
weixin_307779135 小时前
有限产能智能排产与动态重排智能体:从需求解构到技术实现
开发语言·人工智能·算法·架构
kakakahahahaha5 小时前
Windows C盘临时文件清理:%temp%、Windows Temp、Prefetch、Windows.old与hiberfil.sys处理指南
c语言·开发语言·windows
朝朝辞暮i5 小时前
C++ 第 27 课:智能指针 shared_ptr
开发语言·c++·算法
by209995 小时前
学会使用std::string类,并理解其内部是如何管理字符串的详细阐述(上)
c++·笔记·字符串·类和对象·string
沙漠之主6 小时前
C++编程教学设计资料:从入门到实战的完整课程方案
java·前端·c++
朝朝辞暮i6 小时前
C++ 第 29 课:回调函数 Callback
开发语言·c++·算法
笨笨饿6 小时前
140_AI新手村MCP与Skills是干嘛的
开发语言·人工智能·python·stm32·单片机·嵌入式硬件·物联网
Godspeed Zhao6 小时前
现代智能雷达技术14——算法(0)
线性代数·算法·雷达