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;
}
相关推荐
herinspace22 分钟前
管家婆实用贴-如何分离和附加数据库
开发语言·前端·javascript·数据库·语音识别
ILYT NCTR1 小时前
搭建Golang gRPC环境:protoc、protoc-gen-go 和 protoc-gen-go-grpc 工具安装教程
开发语言·后端·golang
叶小鸡1 小时前
小鸡玩算法-力扣HOT100-堆
数据结构·算法·leetcode
小雅痞1 小时前
[Java][Leetcode simple] 28. 找出字符串中第一个匹配项的下标
java·开发语言·leetcode
likerhood1 小时前
java中的不可变类(Immutable)
java·开发语言
Ulyanov1 小时前
《PySide6 GUI开发指南:QML核心与实践》 第一篇:GUI新纪元——QML与PySide6生态系统全景
开发语言·python·qt·qml·雷达电子对抗
何陋轩2 小时前
【重磅】悟空来了:国产AI编程助手深度测评,能否吊打Copilot?
人工智能·算法·面试
Rust研习社2 小时前
从入门到实践:Rust 异步编程完全指南
开发语言·后端·rust
yaoxin5211232 小时前
389. Java IO API - 获取文件名
java·开发语言·python
逸风尊者2 小时前
XGBoost模型工程使用
java·后端·算法