//date.h#ifndef __DATE_H__#define __DATE_H__classDate{//日期类private:int year;//年int month;//月int day;//日int totalDays;//该日期是从公元元年1月1日开始的第几天public:Date(int year,int month,int day);//用年、月、日构造日期intgetYear()const{return year;}intgetMonth()const{return month;}intgetDay()const{return day;}intgetMaxDay()const;//获得当月有多少天boolisLeapYear()const{//判断当年是否为闰年return year %4==0&& year %100!=0|| year %400==0;}voidshow()const;//输出当前日期//计算两个日期之间差多少天 intoperator-(const Date& date)const{return totalDays - date.totalDays;}};#endif//__DATE_H__
//date.cpp#include"date.h"#include<iostream>#include<cstdlib>usingnamespace std;namespace{//namespace使下面的定义只在当前文件中有效//存储平年中某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项constint 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),month(month),day(day){if(day <=0|| day >getMaxDay()){
cout <<"Invalid date: ";show();
cout << endl;exit(1);}int years = year -1;
totalDays = years *365+ years /4- years /100+ years /400+ DAYS_BEFORE_MONTH[month -1]+ day;if(isLeapYear()&& month >2) totalDays++;}int Date::getMaxDay()const{if(isLeapYear()&& month ==2)return29;elsereturn DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month -1];}void Date::show()const{
cout <<getYear()<<"-"<<getMonth()<<"-"<<getDay();}
//accumulator.h#ifndef __ACCUMULATOR_H__#define __ACCUMULATOR_H__#include"date.h"classAccumulator{//将某个数值按日累加private:
Date lastDate;//上次变更数值的时期double value;//数值的当前值double sum;//数值按日累加之和public://构造函数,date为开始累加的日期,value为初始值Accumulator(const Date &date,double value):lastDate(date),value(value),sum(0){}//获得到日期date的累加结果doublegetSum(const Date &date)const{return sum + value *(date - lastDate);}//在date将数值变更为valuevoidchange(const Date &date,double value){
sum =getSum(date);
lastDate = date;this->value = value;}//初始化,将日期变为date,数值变为value,累加器清零voidreset(const Date &date,double value){
lastDate = date;this->value = value;
sum =0;}};#endif//__ACCUMULATOR_H__
//account.h#ifndef __ACCOUNT_H__#define __ACCOUNT_H__#include"date.h"#include"accumulator.h"#include<string>classAccount{//账户类private:
std::string id;//帐号double balance;//余额staticdouble total;//所有账户的总金额protected://供派生类调用的构造函数,id为账户Account(const Date &date,const std::string &id);//记录一笔帐,date为日期,amount为金额,desc为说明voidrecord(const Date &date,double amount,const std::string &desc);//报告错误信息voiderror(const std::string &msg)const;public:const std::string &getId()const{return id;}doublegetBalance()const{return balance;}staticdoublegetTotal(){return total;}//存入现金,date为日期,amount为金额,desc为款项说明virtualvoiddeposit(const Date &date,double amount,const std::string &desc)=0;//取出现金,date为日期,amount为金额,desc为款项说明virtualvoidwithdraw(const Date &date,double amount,const std::string &desc)=0;//结算(计算利息、年费等),每月结算一次,date为结算日期virtualvoidsettle(const Date &date)=0;//显示账户信息virtualvoidshow()const;};classSavingsAccount:public Account {//储蓄账户类private:
Accumulator acc;//辅助计算利息的累加器double rate;//存款的年利率public://构造函数SavingsAccount(const Date &date,const std::string &id,double rate);doublegetRate()const{return rate;}virtualvoiddeposit(const Date &date,double amount,const std::string &desc);virtualvoidwithdraw(const Date &date,double amount,const std::string &desc);virtualvoidsettle(const Date &date);};classCreditAccount:public Account {//信用账户类private:
Accumulator acc;//辅助计算利息的累加器double credit;//信用额度double rate;//欠款的日利率double fee;//信用卡年费doublegetDebt()const{//获得欠款额double balance =getBalance();return(balance <0? balance :0);}public://构造函数CreditAccount(const Date &date,const std::string &id,double credit,double rate,double fee);doublegetCredit()const{return credit;}doublegetRate()const{return rate;}doublegetFee()const{return fee;}doublegetAvailableCredit()const{//获得可用信用if(getBalance()<0)return credit +getBalance();elsereturn credit;}virtualvoiddeposit(const Date &date,double amount,const std::string &desc);virtualvoidwithdraw(const Date &date,double amount,const std::string &desc);virtualvoidsettle(const Date &date);virtualvoidshow()const;};#endif//__ACCOUNT_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 mo...