在前实验基础上,
设计一个基类Account用来表述所有账户的共性,然后将SavingsAccount类变为其派生类,此外还要再派生出用来表示信用账户的类CreditAccount。
(1)基类中保留账号,余额,和表示账户总金额的静态数据成员,以及用于读取它们的成员函数和用来输出账户信息的show函数。存、取、结算利息的函数分别放到各个派生类中。
(2)信用账户类CreditAccount
class CreditAccount:public Account { double credit; //信用额度 double rate; //欠款的日利率 double fee; //信用卡年费 double getDebt() const; //获得欠款额 public: double getAvailableCredit() const; //获得可用信用额度 };
(3)将整个程序分为5或6个文件:date.h account.h是类定义头文件,date.cpp account.cpp是类实现文件,6.cpp是主函数文件。(可把累加功能单独作为一个类来写,就可增加一个该类的头文件。)
提示:
头文件里先写:
#ifndef _DATE_H_ #define _DATE_H_ #endif (头文件结束时写)
要求:
(1)定义类对象,测试程序的正确性
定义两个储蓄账户s0和s1,年利率都是1.5%,都在2008年11月1日创建账户,随后s0在2008年11月5日和2008年12月5日分别存入5000元和5500元,s1在2008年11月25日和2008年12月20日分别存入10000元和取出4000元。
定义一个信用账户c0,信用额度是1万元,年费是50元,日利率是千分之0.5,在2008年11月1日创建账户,随后在2008年11月15日和2008年12月1日分别取出2000元和还清欠款。
2009年1月1日分别输出s0、s1、c0的信息(账号、余额)及所有账户的总额。
源代码:
main.cpp:
#include <iostream> #include <string.h> #include "date.h" #include "account.h" using namespace std; double Account::total = 0; int main() { //账户1001的相关程序 Date d01(2008,11,5),d02(2008,12,5),d11(2008,11,25),d12(2008,12,20),d2(2009,1,1),dc1(2008,11,15),dc2(2008,12,1); cout<<"账户1001的相关信息:"<<endl; SavingsAccount s0(2008,11,1,1001,0,0.015); s0.deposit(d01,5000); s0.deposit(d02,5500); s0.settle(d2); d2.show(); s0.show(); cout<<endl; //账户1002的相关程序 cout<<"账户1002的相关信息:"<<endl; SavingsAccount s1(2008,11,1,1002,0,0.015); s1.deposit(d11,10000); s1.withdraw(d12,4000); s1.settle(d2); d2.show(); s1.show(); cout<<endl; //账户1003的相关程序 cout<<"账户1003的相关信息:"<<endl; CreditAccount c0(2008,11,1,1003,0,10000,0.0005,50); c0.deposit(dc1,2000); c0.withdraw(dc2,2000); c0.withdraw(dc2,-c0.getDebt(dc2)); c0.getDebt(d2); d2.show(); c0.show(); cout<<endl; //所有账户的相关程序 cout<<"所有账户总金额:"<<endl; d2.show(); c0.getTotal(); return 0; }
account.h:
#ifndef ACCOUNT_H_ #define ACCOUNT_H_ #include "date.h" class Account { protected: int id; double balance; static double total; public: Account(int id,double balance); const int getId() {return id;} const double getBalance() {return balance;} static void getTotal(); void show(); }; class SavingsAccount: public Account { double rate; Date lastDate; double accumulation=0; const double accumulate(Date date); public: SavingsAccount(int year,int month,int day,int id,double balance,double Rate); void deposit(Date date,double amount); void withdraw(Date date,double amount); void settle(Date date); const double getRate() {return rate;} const void show(); }; class CreditAccount:public Account { protected: double credit; double rate; double fee; Date lastDate; public: CreditAccount(int year,int month,int day,int id,double balance,double credit,double rate,double fee); double getAvailableCredit() {return credit;} void deposit(Date date,double amount); void withdraw(Date date,double amount); double getDebt(Date date); void settle(Date date); const void show(); }; #endif // ACCOUNT_H_INCLUDED
account.cpp:
#include <iostream> #include "account.h" using namespace std; Account::Account(int Id,double Balance) { id=Id; balance=Balance; } void Account::show() { cout<<"账号:"<<getId()<<"\t余额: "<<getBalance()<<"元"<<endl; } void Account::getTotal() { cout<<"所有账户总金额: "<<total<<"元"<<endl; } SavingsAccount::SavingsAccount(int year,int month,int day,int id,double balance,double Rate):Account(id,balance),lastDate(year,month,day) { rate=Rate; cout<<year<<"年"<<month<<"月"<<day<<"日\t"; cout<<"ID为:"<<id<<"的账户已创建成功!"<<endl; } const double SavingsAccount::accumulate(Date date) { return lastDate.distance(date)*balance; } void SavingsAccount::deposit(Date date,double amount) { accumulation+=accumulate(date); balance+=amount; lastDate = date; total+=amount; cout<<date.getYear()<<"年"<<date.getMonth()<<"月"<<date.getDay()<<"日\t"; cout<<"账户"<<id<<"存入: "<<amount<<"元"<<"\t\t"<<"账户"<<id<<"余额: "<<balance<<"元"<<endl; } void SavingsAccount::withdraw(Date date,double amount) { accumulation+=accumulate(date); balance-=amount; lastDate = date; total-=amount; cout<<date.getYear()<<"年"<<date.getMonth()<<"月"<<date.getDay()<<"日\t"; cout<<"账户"<<id<<"取出: "<<amount<<"元"<<"\t\t"<<"账户"<<id<<"余额: "<<balance<<"元"<<endl; } void SavingsAccount::settle(Date date) { double settle = 0; accumulation+=accumulate(date); settle = accumulation/356*rate; cout<<date.getYear()<<"年"<<date.getMonth()<<"月"<<date.getDay()<<"日\t"; cout<<"账户"<<id<<"结算利息: "<<settle<<"元"<<endl; deposit(date,settle); } const void SavingsAccount::show() { Account::show(); } CreditAccount::CreditAccount(int year,int month,int day,int id,double balance,double Credit,double Rate,double Fee): Account(id,balance),lastDate(year,month,day) { credit=Credit; fee=Fee; rate=Rate; cout<<year<<"年"<<month<<"月"<<day<<"日\t"; cout<<"ID为:"<<id<<"的账户已创建成功!"<<endl; } double CreditAccount::getDebt(Date date) { settle(date); return balance; } void CreditAccount::deposit(Date date,double amount) { settle(date); if(credit<=0 || amount>credit) { date.show(); cout<<"账户"<<id<<"\t信用卡可用额度不足,请及时还款!"<<endl; } else { balance-=amount; credit-=amount; date.show(); cout<<"账户"<<id<<"\t信用卡已用额度: "<<amount<<"元"<<"\t\t信用卡剩余额度: "<<credit<<"元"<<endl; total-=amount; } } void CreditAccount::withdraw(Date date,double amount) { settle(date); balance+=amount; if((credit+amount)<=10000) { credit=credit+amount; } else { credit=10000; } total+=amount; date.show(); cout<<"账户"<<id<<"\t信用卡还款: "<<amount<<"元"<<"\t\t信用卡剩余额度: "<<credit<<"元"<<endl; } void CreditAccount::settle(Date date) { double settle=0; if((date.getMonth()<lastDate.getMonth())||(date.getYear()>lastDate.getYear())) { for(int i=1;i<=date.getYear()-lastDate.getYear();i++) { settle+=fee; } } if(date.getDay()==1||date.getMonth()>lastDate.getMonth()||date.getYear()>lastDate.getYear()) { if(date.getYear()==lastDate.getYear()) { for(int j=1;j<=(date.getMonth()-lastDate.getMonth());j++) { for(int m=1;m<=lastDate.distance(date);m++) { settle-=balance*rate; } } } else { for(int j=1;j<=(date.getMonth()-lastDate.getMonth())+12;j++) { for(int m=1;m<=lastDate.distance(date);m++) { settle-=balance*rate; } } } } else { settle-=0; } balance-=settle; total-=settle; lastDate=date; } const void CreditAccount::show() { Account::show(); }
date.h:
#ifndef DATE_H_INCLUDED #define DATE_H_INCLUDED class Date { int year, month, day; int totalDays; public: Date(int year, int month, int day); int getYear() const { return year; } int getMonth() const { return month; } int getDay() const { return day; } void show() const; bool isLeapYear(int year) const; int distance(const Date& date) {return date.totalDays-totalDays;} }; #endif // DATE_H_INCLUDED
date.cpp:
#include <iostream> #include "date.h" using namespace std; Date::Date(int Year, int Month, int Day) { year=Year; month=Month; day=Day; int Days=0; for(int i=0;i<year;i++) { if(isLeapYear(i)==true) Days=Days+366; else Days=Days+365; } switch(month){ case 1:Days+=0;break; case 2:Days+=31;break; case 3:Days+=59;break; case 4:Days+=90;break; case 5:Days+=120;break; case 6:Days+=151;break; case 7:Days+=181;break; case 8:Days+=212;break; case 9:Days+=243;break; case 10:Days+=273;break; case 11:Days+=304;break; case 12:Days+=334;break; default:cout<<"month error!"<<endl; } totalDays=Days+day; } void Date::show() const { cout<<getYear()<<"年"<<getMonth()<<"月"<<getDay()<<"日\t"; } bool Date::isLeapYear(int year) const { if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return true; else return false; }
运行截图:
学习掌握
掌握继承的概念,理解不同的继承类型。
继承是连接类与类的一种层次模型。继承使一个既有类(基类或父类)的数据结构和函数被另一个类(派生类或子类)重用,在派生类中只需描述其基类中没有的数据和操作即可。
对前几次实验的进一步发展完善,增加了信用卡部分,学习使用了继承,无需修改已有类,只需在已有类的基础上,通过增加少量代码或修改少量代码的方法得到新的类,提高了项目程序完善的高效性。