下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
#include
class CDate {
private:
int m_nDay; // Range: 1 - 30 (lets assume all months have 30 days!)
int m_nMonth; // Range: 1 - 12
int m_nYear;
void AddDays(int nDaysToAdd) {
m_nDay += nDaysToAdd;
if (m_nDay > 30) {
AddMonths(m_nDay / 30);
m_nDay %= 30; // rollover 30th -> 1st
}
}
void AddMonths(int nMonthsToAdd) {
m_nMonth += nMonthsToAdd;
if (m_nMonth > 12) {
AddYears(m_nMonth / 12);
m_nMonth %= 12; // rollover dec -> jan
}
}
void AddYears(int m_nYearsToAdd) {
m_nYear += m_nYearsToAdd;
}
public:
// Constructor that initializes the object to a day,month and year