创建一个Date类

这篇博客介绍了如何在C++中创建一个Date类,类的实现直接放在头文件中。由于这个设置,当修改类实现时,Visual Studio可能不会自动检测到变化,需要更新cpp文件以触发重新编译。此外,讨论了只创建string对象时可以不包含<string>头文件的情况,但这将限制使用cout输出string的能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

头文件:1 Data class.h
#ifndef I_DATE_ED
#define I_DATE_ED

#include <iostream>
#include <string>
using namespace std;
// year应当是1800到2200之间的整数;
// month必须是1到12之间的整数;
// day必须是1到给定、月的天数之间的整数;
class Date
{
	int year, month, day;
public:
	Date() {month = 1; day = 1; year = 1800;}
	Date(int, int, int);
	bool isvalid();		// 判断Date是否合法
	void readinto();	// 用读入的month、day和year设置这个Date的值
	Date next();		// 返回Date之后的Date
	Date previous();	// 返回Date之前的Date
	string dayofweek();	// 返回这个Date是星期几
	friend ostream& operator<<(ostream&, Date&);
};

Date::Date(int monthln, int dayln, int yearln)
{
	month = monthln;
	day = dayln;
	year = yearln;
}

bool Date::isvalid()
{
	if (year < 1800 || year > 2200)
		return false;
	else 
		switch(month){
		case 1: case 3: case 5: case 7:
		case 8: case 10: case 12:
			if (day < 1 || day > 31) return false;
		case 4: case 6: case 9: case 11:
			if (day &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值