#include
using namespace std;
class Date
{
public:
int GetMonthDay(int year, int month)
{
int monthArray[13] = { 0, 31, 28, 30 };
if (month2&& year%4 == 0&& year%100!=0 || year%400 == 0)
return 29;
else
return monthArray[month];
}
//四个成员函数
Date(int year = 2019, int month = 1, int day = 1)
{
if (year > 0 && month<13 && day>0 && day <= GetMonthDay(year, month))
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << “日期非法” << endl;
}
}
bool operator(const Date& d);
bool operator!=(const Date& d);
bool operator>(const Date& d);
bool operator<(const Date& d);
bool operator>=(const Date& d);
这是一个使用C++编写的Date类,包含获取指定年份2月天数的函数,以及构造函数、比较运算符等成员函数。示例中创建了一个2004年2月29日的对象并打印。
最低0.47元/天 解锁文章
7851

被折叠的 条评论
为什么被折叠?



