#include<iostream>
using namespace std;
class CDate
{
public:
CDate(int nYear = 0, int nMon = 0, int nDay = 0); //构造函数
bool SetData(int nYear, int nMon = 0, int nDay = 0);// 设置日期函数
void PrintData();// 打印日期函数
bool AddDay(); // 将日期加1函数
protected:
bool IsTrueDate(); // 判断日期是否有效函数
protected:
int m_nYear;
int m_nMonth;
int m_nDay;
};
CDate::CDate(int nYear, int nMon, int nDay)
: m_nYear(nYear),
m_nMonth(nMon),
m_nDay(nDay)
{}
bool CDate::IsTrueDate()
{
// 将每一个月的天数保存在数组
static unsigned char days[12]
= { 31,0,31,30,31,30,31,31,30,31,30,31 };
// 判断月份是否有效(小于1,大于12都是无效月份)
if (m_nMonth >= 13 || m_nMonth <= 0)
return false;
// 判断年份是否是闰年
if (m_nYear > 0
&& m_nYear % 400 == 0
|| m_nYear % 4 == 0
&& m_nYear % 100 != 0)
{
days[1] = 29;//如果是闰年2月有29天
}
else
days[1] = 28;//不是闰年则2月只有28天
// 判断天数是否在对应月份的天数的范围之内
if (m nDay > days[m nMonth - 1] || m nDay <= 0)
return false;
return true;
}
bool CDate::SetData(int nYear, int nMon, int nDay)
{
m_nYear = nYear;
m_nMonth = nMon;
m_nDay = nDay;
return IsTrueDate();
}
void CDate::PrintData()
{
if (IsTrueDate())
{
cout << "[" ;
}
else
{
cout << "无效日期";
}
}
bool CDate::AddDay()
{
if (IsTrueDate())
{
return false;
}
}