博文回顾
C++之个人银行账户管理程序(一)
基于上一篇的基础,本博文要解决总账户金额的存储和维护,以及日期的友好表示。
1.data.h
//date.h
#ifndef __DATE_H__
#define __DATE_H__
class Date {
//日期类
private:
int year; //年
int month; //月
int day; //日
int totalDays; //该日期是从公元元年1月1日开始的第几天
public:
Date(int year, int month, int day); //用年、月、日构造日期
int getYear() const {
return year; }
int getMonth() const {
return month; }
int getDay() const {
return day; }
int getMaxDay() const; //获得当月有多少天
bool isLeapYear() const {
//判断当年是否为闰年
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
void show() const; //输出当前日期
//计算两个日期之间差多少天
int distance(const Date& date) const {
return totalDays - date.totalDays;
}
};
#endif //__DATE_H__
2.data.cpp
//date.cpp
#include "date.h"
#include <iostream>
#include <cstdlib>
using namespace std;
namespace {
//namespace使下面的定义只在当前文件中有效
//存储平年中某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项
const int DAYS_BEFORE_MONTH[] = {
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
}
Date::Date(int year, int month, int day) : year(year)