实验任务3
pets.hpp源码
1 #include <iostream> 2 using std::string; 3 class MachinePets { 4 private: 5 string nickname; 6 public: 7 MachinePets(const string s); 8 string get_nickname() const; 9 virtual string talk() = 0; 10 }; 11 MachinePets::MachinePets(const string s) : nickname{s} {} 12 string MachinePets::get_nickname() const {return nickname;}; 13 class PetCats : public MachinePets { 14 public: 15 PetCats(const string s); 16 string talk() override; 17 }; 18 PetCats::PetCats(const string s) : MachinePets{s} {} 19 string PetCats::talk() { 20 return "miao wu~"; 21 } 22 class PetDogs : public MachinePets { 23 public: 24 PetDogs(const string s); 25 string talk() override; 26 }; 27 PetDogs::PetDogs(const string s) : MachinePets{s} {} 28 string PetDogs::talk() { 29 return "wang wang~"; 30 }View Code
task3.cpp源码
1 #include <iostream> 2 #include "pets.hpp" 3 4 void play(MachinePets &obj) { 5 std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl; 6 } 7 8 void test() { 9 PetCats cat("miku"); 10 PetDogs dog("da huang"); 11 12 play( cat ); 13 play( dog ); 14 } 15 16 int main() { 17 test(); 18 }View Code
运行测试截图
实验任务4
Person.hpp源码
task4.cpp源码
运行测试截图
实验任务5
date.h源码
1 #ifndef __DATE_H__ 2 #define __DATE_H__ 3 4 class Date { //日期类 5 private: 6 int year; //年 7 int month; //月 8 int day; //日 9 int totalDays; //该日期是从公元元年1月1日开始的第几天 10 11 public: 12 Date(int year, int month, int day); //用年、月、日构造日期 13 int getYear() const { return year; } 14 int getMonth() const { return month; } 15 int getDay() const { return day; } 16 int getMaxDay() const; //获得当月有多少天 17 bool isLeapYear() const { //判断当年是否为闰年 18 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 19 } 20 void show() const; //输出当前日期 21 //计算两个日期之间差多少天 22 int operator - (const Date& date) const { 23 return totalDays - date.totalDays; 24 } 25 }; 26 27 #endif //__DATE_H__View Code
date.cpp源码
1 #include "date.h" 2 #include <iostream> 3 #include <cstdlib> 4 using namespace std; 5 6 namespace { //namespace使下面的定义只在当前文件中有效 7 //存储平年中某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项 8 const int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; 9 } 10 11 Date::Date(int year, int month, int day) : year(year), month(month), day(day) { 12 if (day <= 0 || day > getMaxDay()) { 13 cout << "Invalid date: "; 14 show(); 15 cout << endl; 16 exit(1); 17 } 18 int years = year - 1; 19 totalDays = years * 365 + years / 4 - years / 100 + years / 400 20 + DAYS_BEFORE_MONTH[month - 1] + day; 21 if (isLeapYear() && month > 2) totalDays++; 22 } 23 24 int Date::getMaxDay() const { 25 if (isLeapYear() && month == 2) 26 return 29; 27 else 28 return DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month - 1]; 29 } 30 31 void Date::show() const { 32 cout << getYear() << "-" << getMonth() << "-" << getDay(); 33 }View Code
accumulator.h源码
1 #ifndef __ACCUMULATOR_H__ 2 #define __ACCUMULATOR_H__ 3 #include "date.h" 4 5 class Accumulator { //将某个数值按日累加 6 private: 7 Date lastDate; //上次变更数值的时期 8 double value; //数值的当前值 9 double sum; //数值按日累加之和 10 public: 11 //构造函数,date为开始累加的日期,value为初始值 12 Accumulator(const Date &date, double value) 13 : lastDate(date), value(value), sum(0) { } 14 15 //获得到日期date的累加结果 16 double getSum(const Date &date) const { 17 return sum + value * (date - lastDate); 18 } 19 20 //在date将数值变更为value 21 void change(const Date &date, double value) { 22 sum = getSum(date); 23 lastDate = date; 24 this->value = value; 25 } 26 27 //初始化,将日期变为date,数值变为value,累加器清零 28 void reset(const Date &date, double value) { 29 lastDate = date; 30 this->value = value; 31 sum = 0; 32 } 33 }; 34 35 #endif //__ACCUMULATOR_H__View Code account.h源码
1 #ifndef __ACCOUNT_H__ 2 #define __ACCOUNT_H__ 3 #include "date.h" 4 #include "accumulator.h" 5 #include <string> 6 7 class Account { //账户类 8 private: 9 std::string id; //帐号 10 double balance; //余额 11 static double total; //所有账户的总金额 12 protected: 13 //供派生类调用的构造函数,id为账户 14 Account(const Date &date, const std::string &id); 15 //记录一笔帐,date为日期,amount为金额,desc为说明 16 void record(const Date &date, double amount, const std::string &desc); 17 //报告错误信息 18 void error(const std::string &msg) const; 19 public: 20 const std::string &getId() const { return id; } 21 double getBalance() const { return balance; } 22 static double getTotal() { return total; } 23 //存入现金,date为日期,amount为金额,desc为款项说明 24 virtual void deposit(const Date &date, double amount, const std::string &desc) = 0; 25 //取出现金,date为日期,amount为金额,desc为款项说明 26 virtual void withdraw(const Date &date, double amount, const std::string &desc) = 0; 27 //结算(计算利息、年费等),每月结算一次,date为结算日期 28 virtual void settle(const Date &date) = 0; 29 //显示账户信息 30 virtual void show() const; 31 }; 32 33 class SavingsAccount : public Account { //储蓄账户类 34 private: 35 Accumulator acc; //辅助计算利息的累加器 36 double rate; //存款的年利率 37 public: 38 //构造函数 39 SavingsAccount(const Date &date, const std::string &id, double rate); 40 double getRate() const { return rate; } 41 virtual void deposit(const Date &date, double amount, const std::string &desc); 42 virtual void withdraw(const Date &date, double amount, const std::string &desc); 43 virtual void settle(const Date &date); 44 }; 45 46 class CreditAccount : public Account { //信用账户类 47 private: 48 Accumulator acc; //辅助计算利息的累加器 49 double credit; //信用额度 50 double rate; //欠款的日利率 51 double fee; //信用卡年费 52 53 double getDebt() const { //获得欠款额 54 double balance = getBalance(); 55 return (balance < 0 ? balance : 0); 56 } 57 public: 58 //构造函数 59 CreditAccount(const Date &date, const std::string &id, double credit, double rate, double fee); 60 double getCredit() const { return credit; } 61 double getRate() const { return rate; } 62 double getFee() const { return fee; } 63 double getAvailableCredit() const { //获得可用信用 64 if (getBalance() < 0) 65 return credit + getBalance(); 66 else 67 return credit; 68 } 69 virtual void deposit(const Date &date, double amount, const std::string &desc); 70 virtual void withdraw(const Date &date, double amount, const std::string &desc); 71 virtual void settle(const Date &date); 72 virtual void show() const; 73 }; 74 75 #endif //__ACCOUNT_H__View Code account.cpp源码
1 #include "account.h" 2 #include <cmath> 3 #include <iostream> 4 using namespace std; 5 6 double Account::total = 0; 7 8 //Account类的实现 9 Account::Account(const Date &date, const string &id) 10 : id(id), balance(0) { 11 date.show(); 12 cout << "\t#" << id << " created" << endl; 13 } 14 15 void Account::record(const Date &date, double amount, const string &desc) { 16 amount = floor(amount * 100 + 0.5) / 100; //保留小数点后两位 17 balance += amount; 18 total += amount; 19 date.show(); 20 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 21 } 22 23 void Account::show() const { 24 cout << id << "\tBalance: " << balance; 25 } 26 27 void Account::error(const string &msg) const { 28 cout << "Error(#" << id << "): " << msg << endl; 29 } 30 31 //SavingsAccount类相关成员函数的实现 32 SavingsAccount::SavingsAccount(const Date &date, const string &id, double rate) 33 : Account(date, id), rate(rate), acc(date, 0) { } 34 35 void SavingsAccount::deposit(const Date &date, double amount, const string &desc) { 36 record(date, amount, desc); 37 acc.change(date, getBalance()); 38 } 39 40 void SavingsAccount::withdraw(const Date &date, double amount, const string &desc) { 41 if (amount > getBalance()) { 42 error("not enough money"); 43 } else { 44 record(date, -amount, desc); 45 acc.change(date, getBalance()); 46 } 47 } 48 49 void SavingsAccount::settle(const Date &date) { 50 if (date.getMonth() == 1) { //每年的一月计算一次利息 51 double interest = acc.getSum(date) * rate 52 / (date - Date(date.getYear() - 1, 1, 1)); 53 if (interest != 0) 54 record(date, interest, "interest"); 55 acc.reset(date, getBalance()); 56 } 57 } 58 59 //CreditAccount类相关成员函数的实现 60 CreditAccount::CreditAccount(const Date& date, const string& id, double credit, double rate, double fee) 61 : Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) { } 62 63 void CreditAccount::deposit(const Date &date, double amount, const string &desc) { 64 record(date, amount, desc); 65 acc.change(date, getDebt()); 66 } 67 68 void CreditAccount::withdraw(const Date &date, double amount, const string &desc) { 69 if (amount - getBalance() > credit) { 70 error("not enough credit"); 71 } else { 72 record(date, -amount, desc); 73 acc.change(date, getDebt()); 74 } 75 } 76 77 void CreditAccount::settle(const Date &date) { 78 double interest = acc.getSum(date) * rate; 79 if (interest != 0) 80 record(date, interest, "interest"); 81 if (date.getMonth() == 1) 82 record(date, -fee, "annual fee"); 83 acc.reset(date, getDebt()); 84 } 85 86 void CreditAccount::show() const { 87 Account::show(); 88 cout << "\tAvailable credit:" << getAvailableCredit(); 89 }View Code 8_8.cpp源码
1 #include "account.h" 2 #include <iostream> 3 using namespace std; 4 5 int main() { 6 Date date(2008, 11, 1); //起始日期 7 //建立几个账户 8 SavingsAccount sa1(date, "S3755217", 0.015); 9 SavingsAccount sa2(date, "02342342", 0.015); 10 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 11 Account *accounts[] = { &sa1, &sa2, &ca }; 12 const int n = sizeof(accounts) / sizeof(Account*); //账户总数 13 14 cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl; 15 char cmd; 16 do { 17 //显示日期和总金额 18 date.show(); 19 cout << "\tTotal: " << Account::getTotal() << "\tcommand> "; 20 21 int index, day; 22 double amount; 23 string desc; 24 25 cin >> cmd; 26 switch (cmd) { 27 case 'd': //存入现金 28 cin >> index >> amount; 29 getline(cin, desc); 30 accounts[index]->deposit(date, amount, desc); 31 break; 32 case 'w': //取出现金 33 cin >> index >> amount; 34 getline(cin, desc); 35 accounts[index]->withdraw(date, amount, desc); 36 break; 37 case 's': //查询各账户信息 38 for (int i = 0; i < n; i++) { 39 cout << "[" << i << "] "; 40 accounts[i]->show(); 41 cout << endl; 42 } 43 break; 44 case 'c': //改变日期 45 cin >> day; 46 if (day < date.getDay()) 47 cout << "You cannot specify a previous day"; 48 else if (day > date.getMaxDay()) 49 cout << "Invalid day"; 50 else 51 date = Date(date.getYear(), date.getMonth(), day); 52 break; 53 case 'n': //进入下个月 54 if (date.getMonth() == 12) 55 date = Date(date.getYear() + 1, 1, 1); 56 else 57 date = Date(date.getYear(), date.getMonth() + 1, 1); 58 for (int i = 0; i < n; i++) 59 accounts[i]->settle(date); 60 break; 61 } 62 } while (cmd != 'e'); 63 return 0; 64 }View Code 运行测试截图
标签:const,string,继承,double,void,多态,Date,实验,date From: https://www.cnblogs.com/ffhfAdjFH7Vr/p/17869309.html