实验4
实验任务二
代码
GradeCalc.hpp
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <algorithm> 5 #include <numeric> 6 #include <iomanip> 7 8 using std::vector; 9 using std::string; 10 using std::cin; 11 using std::cout; 12 using std::endl; 13 14 class GradeCalc: public vector<int> { 15 public: 16 GradeCalc(const string &cname, int size); 17 void input(); // 录入成绩 18 void output() const; // 输出成绩 19 void sort(bool ascending = false); // 排序 (默认降序) 20 int min() const; // 返回最低分 21 int max() const; // 返回最高分 22 float average() const; // 返回平均分 23 void info(); // 输出课程成绩信息 24 25 private: 26 void compute(); // 成绩统计 27 28 private: 29 string course_name; // 课程名 30 int n; // 课程人数 31 vector<int> counts = vector<int>(5, 0); // 保存各分数段人数([0, 60), [60, 70), [70, 80), [80, 90), [90, 100] 32 vector<double> rates = vector<double>(5, 0); // 保存各分数段比例 33 }; 34 35 GradeCalc::GradeCalc(const string &cname, int size): course_name{cname}, n{size} {} 36 37 void GradeCalc::input() { 38 int grade; 39 40 for(int i = 0; i < n; ++i) { 41 cin >> grade; 42 this->push_back(grade); 43 } 44 } 45 46 void GradeCalc::output() const { 47 for(auto ptr = this->begin(); ptr != this->end(); ++ptr) 48 cout << *ptr << " "; 49 cout << endl; 50 } 51 52 void GradeCalc::sort(bool ascending) { 53 if(ascending) 54 std::sort(this->begin(), this->end()); 55 else 56 std::sort(this->begin(), this->end(), std::greater<int>()); 57 } 58 59 int GradeCalc::min() const { 60 return *std::min_element(this->begin(), this->end()); 61 } 62 63 int GradeCalc::max() const { 64 return *std::max_element(this->begin(), this->end()); 65 } 66 67 float GradeCalc::average() const { 68 return std::accumulate(this->begin(), this->end(), 0) * 1.0 / n; 69 } 70 71 void GradeCalc::compute() { 72 for(int grade: *this) { 73 if(grade < 60) 74 counts.at(0)++; 75 else if(grade >= 60 && grade < 70) 76 counts.at(1)++; 77 else if(grade >= 70 && grade < 80) 78 counts.at(2)++; 79 else if(grade >= 80 && grade < 90) 80 counts.at(3)++; 81 else if(grade >= 90) 82 counts.at(4)++; 83 } 84 85 for(int i = 0; i < rates.size(); ++i) 86 rates.at(i) = counts.at(i) * 1.0 / n; 87 } 88 89 void GradeCalc::info() { 90 cout << "课程名称:\t" << course_name << endl; 91 cout << "排序后成绩: \t"; 92 sort(); output(); 93 cout << "最高分:\t" << max() << endl; 94 cout << "最低分:\t" << min() << endl; 95 cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl; 96 97 compute(); // 统计各分数段人数、比例 98 99 vector<string> tmp{"[0, 60) ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]"}; 100 for(int i = tmp.size()-1; i >= 0; --i) 101 cout << tmp[i] << "\t: " << counts[i] << "人\t" 102 << std::fixed << std::setprecision(2) << rates[i]*100 << "%" << endl; 103 }
demo2.cpp
1 #include "GradeCalc.hpp" 2 #include <iomanip> 3 4 void test() { 5 int n; 6 cout << "输入班级人数: "; 7 cin >> n; 8 9 GradeCalc c1("OOP", n); 10 11 cout << "录入成绩: " << endl;; 12 c1.input(); 13 cout << "输出成绩: " << endl; 14 c1.output(); 15 16 cout << string(20, '*') + "课程成绩信息" + string(20, '*') << endl; 17 c1.info(); 18 } 19 20 int main() { 21 test(); 22 }
编译结果
问题:
问题一:在派生类 GradeCalc 中,成绩直接存储在基类 std::vector<int> 中。
方法 sort 使用 this->begin() 和 this->end() 迭代器访问成绩集合。
方法 min 和 max 调用了 std::min_element 和 std::max_element,同样通过迭代器访问成绩。
方法 average 利用 std::accumulate 遍历成绩集合。
方法 output 使用 this->begin() 和 this->end() 输出成绩。
方法 input 通过 this->push_back(grade) 将用户输入的每个成绩追加到 GradeCalc(即 std::vector<int>)中。
问题二:功能是计算平均值
有影响,用来确保小数部分精度
问题三:没有对成绩校验、只支持单一的成绩管理
实验任务三
代码
GradeCalc.hpp
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <algorithm> 5 #include <numeric> 6 #include <iomanip> 7 8 using std::vector; 9 using std::string; 10 using std::cin; 11 using std::cout; 12 using std::endl; 13 14 class GradeCalc { 15 public: 16 GradeCalc(const string &cname, int size); 17 void input(); // 录入成绩 18 void output() const; // 输出成绩 19 void sort(bool ascending = false); // 排序 (默认降序) 20 int min() const; // 返回最低分 21 int max() const; // 返回最高分 22 float average() const; // 返回平均分 23 void info(); // 输出课程成绩信息 24 25 private: 26 void compute(); // 成绩统计 27 28 private: 29 string course_name; // 课程名 30 int n; // 课程人数 31 vector<int> grades; // 课程成绩 32 vector<int> counts = vector<int>(5, 0); // 保存各分数段人数([0, 60), [60, 70), [70, 80), [80, 90), [90, 100] 33 vector<double> rates = vector<double>(5, 0); // 保存各分数段比例 34 }; 35 36 GradeCalc::GradeCalc(const string &cname, int size): course_name{cname}, n{size} {} 37 38 void GradeCalc::input() { 39 int grade; 40 41 for(int i = 0; i < n; ++i) { 42 cin >> grade; 43 grades.push_back(grade); 44 } 45 } 46 47 void GradeCalc::output() const { 48 for(int grade: grades) 49 cout << grade << " "; 50 cout << endl; 51 } 52 53 void GradeCalc::sort(bool ascending) { 54 if(ascending) 55 std::sort(grades.begin(), grades.end()); 56 else 57 std::sort(grades.begin(), grades.end(), std::greater<int>()); 58 59 } 60 61 int GradeCalc::min() const { 62 return *std::min_element(grades.begin(), grades.end()); 63 } 64 65 int GradeCalc::max() const { 66 return *std::max_element(grades.begin(), grades.end()); 67 } 68 69 float GradeCalc::average() const { 70 return std::accumulate(grades.begin(), grades.end(), 0) * 1.0 / n; 71 } 72 73 void GradeCalc::compute() { 74 for(int grade: grades) { 75 if(grade < 60) 76 counts.at(0)++; 77 else if(grade >= 60 && grade < 70) 78 counts.at(1)++; 79 else if(grade >= 70 && grade < 80) 80 counts.at(2)++; 81 else if(grade >= 80 && grade < 90) 82 counts.at(3)++; 83 else if(grade >= 90) 84 counts.at(4)++; 85 } 86 87 for(int i = 0; i < rates.size(); ++i) 88 rates.at(i) = counts.at(i) *1.0 / n; 89 } 90 91 void GradeCalc::info() { 92 cout << "课程名称:\t" << course_name << endl; 93 cout << "排序后成绩: \t"; 94 sort(); output(); 95 cout << "最高分:\t" << max() << endl; 96 cout << "最低分:\t" << min() << endl; 97 cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl; 98 99 compute(); // 统计各分数段人数、比例 100 101 vector<string> tmp{"[0, 60) ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]"}; 102 for(int i = tmp.size()-1; i >= 0; --i) 103 cout << tmp[i] << "\t: " << counts[i] << "人\t" 104 << std::fixed << std::setprecision(2) << rates[i]*100 << "%" << endl; 105 }
demo3.cpp
1 #include "GradeCalc.hpp" 2 #include <iomanip> 3 4 void test() { 5 int n; 6 cout << "输入班级人数: "; 7 cin >> n; 8 9 GradeCalc c1("OOP", n); 10 11 cout << "录入成绩: " << endl;; 12 c1.input(); 13 cout << "输出成绩: " << endl; 14 c1.output(); 15 16 cout << string(20, '*') + "课程成绩信息" + string(20, '*') << endl; 17 c1.info(); 18 } 19 20 int main() { 21 test(); 22 }
编译结果、
问题:
问题一:在 GradeCalc 类中,成绩存储在私有成员变量 grades 中
sort 使用了 grades.begin() 和 grades.end(),调用标准库的 std::sort 对成绩进行排序。
min 和 max 使用了 std::min_element 和 std::max_element,直接在 grades 容器范围内查找最小值和最大值。
average 使用了 std::accumulate 累加成绩总和,并通过班级人数计算平均分。
output 直接遍历 grades 容器,通过迭代输出每一个成绩。
与实验任务 2 的区别
成绩存储方式:实验任务 2 中的 GradeCalc 是继承自 std::vector<int>,成绩直接存储在父类 vector 中(GradeCalc)。
访问方式差异:实验任务 2 的方法直接调用 this->begin() 等接口操作成绩,因为类本身就是一个 vector。
问题二:
实验任务 2:直接继承 std::vector<int>,对外暴露了父类的所有接口(如 push_back 等),破坏了封装性。
实验任务 3:将成绩存储细节隐藏在私有变量 grades 中,通过类方法(如 input、output)控制数据访问,更符合封装原则。
实验任务四
4.1
代码
task4.1
1 #include <iostream> 2 #include <string> 3 #include <limits> 4 5 using namespace std; 6 7 void test1() { 8 string s1, s2; 9 cin >> s1 >> s2; // cin: 从输入流读取字符串, 碰到空白符(空格/回车/Tab)即结束 10 cout << "s1: " << s1 << endl; 11 cout << "s2: " << s2 << endl; 12 } 13 14 void test2() { 15 string s1, s2; 16 getline(cin, s1); // getline(): 从输入流中提取字符串,直到遇到换行符 17 getline(cin, s2); 18 cout << "s1: " << s1 << endl; 19 cout << "s2: " << s2 << endl; 20 } 21 22 void test3() { 23 string s1, s2; 24 getline(cin, s1, ' '); //从输入流中提取字符串,直到遇到指定分隔符 25 getline(cin, s2); 26 cout << "s1: " << s1 << endl; 27 cout << "s2: " << s2 << endl; 28 } 29 30 int main() { 31 cout << "测试1: 使用标准输入流对象cin输入字符串" << endl; 32 test1(); 33 cout << endl; 34 35 cin.ignore(numeric_limits<streamsize>::max(), '\n'); 36 37 cout << "测试2: 使用函数getline()输入字符串" << endl; 38 test2(); 39 cout << endl; 40 41 cout << "测试3: 使用函数getline()输入字符串, 指定字符串分隔符" << endl; 42 test3(); 43 }
编译结果
问题:
作用是清除多余内容
4.2
代码
task4.2
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <limits> 5 6 using namespace std; 7 8 void output(const vector<string> &v) { 9 for(auto &s: v) 10 cout << s << endl; 11 } 12 13 void test() { 14 int n; 15 while(cout << "Enter n: ", cin >> n) { 16 vector<string> v1; 17 18 for(int i = 0; i < n; ++i) { 19 string s; 20 cin >> s; 21 v1.push_back(s); 22 } 23 24 cout << "output v1: " << endl; 25 output(v1); 26 cout << endl; 27 } 28 } 29 30 int main() { 31 cout << "测试: 使用cin多组输入字符串" << endl; 32 test(); 33 }
编译结果
问题:
4.3
代码
task4.3
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <limits> 5 6 using namespace std; 7 8 void output(const vector<string> &v) { 9 for(auto &s: v) 10 cout << s << endl; 11 } 12 13 void test() { 14 int n; 15 while(cout << "Enter n: ", cin >> n) { 16 cin.ignore(numeric_limits<streamsize>::max(), '\n'); 17 18 vector<string> v2; 19 20 for(int i = 0; i < n; ++i) { 21 string s; 22 getline(cin, s); 23 v2.push_back(s); 24 } 25 cout << "output v2: " << endl; 26 output(v2); 27 cout << endl; 28 } 29 } 30 31 int main() { 32 cout << "测试: 使用函数getline()多组输入字符串" << endl; 33 test(); 34 }
编译结果
问题:
作用是清空缓存的字符
实验任务五
代码
grm.hpp
1 #ifndef GRM_HPP 2 #define GRM_HPP 3 4 template<typename T> 5 class GameResourceManager { 6 private: 7 T resource; 8 public: 9 GameResourceManager(T initial) : resource(initial) {} 10 11 T get() const { 12 return resource; 13 } 14 15 void update(T change) { 16 resource += change; 17 if (resource < 0) { 18 resource = 0; 19 } 20 } 21 }; 22 23 #endif
task5.cpp
1 #include "grm.hpp" 2 #include <iostream> 3 4 using std::cout; 5 using std::endl; 6 7 void test1() { 8 GameResourceManager<float> HP_manager(99.99); 9 cout << "当前生命值: " << HP_manager.get() << endl; 10 HP_manager.update(9.99); 11 cout << "增加9.99生命值后, 当前生命值: " << HP_manager.get() << endl; 12 HP_manager.update(-999.99); 13 cout <<"减少999.99生命值后, 当前生命值: " << HP_manager.get() << endl; 14 } 15 16 void test2() { 17 GameResourceManager<int> Gold_manager(100); 18 cout << "当前金币数量: " << Gold_manager.get() << endl; 19 Gold_manager.update(50); 20 cout << "增加50个金币后, 当前金币数量: " << Gold_manager.get() << endl; 21 Gold_manager.update(-99); 22 cout <<"减少99个金币后, 当前金币数量: " << Gold_manager.get() << endl; 23 } 24 25 26 int main() { 27 cout << "测试1: 用float类型对类模板GameResourceManager实例化" << endl; 28 test1(); 29 cout << endl; 30 31 cout << "测试2: 用int类型对类模板GameResourceManager实例化" << endl; 32 test2(); 33 }
编译结果
实验任务六
代码
info.hpp
1 #ifndef INFO_HPP 2 #define INFO_HPP 3 4 #include <iostream> 5 #include <string> 6 using std::string; 7 using std::cout; 8 using std::endl; 9 10 class Info { 11 private: 12 string nickname; // 昵称 13 string contact; // 联系方式(邮箱/手机号) 14 string city; // 所在城市 15 int n; // 预定参加人数 16 17 public: 18 // 构造函数 19 Info(const string& nickname, const string& contact, const string& city, int n) 20 : nickname(nickname), contact(contact), city(city), n(n) {} 21 22 // 获取预定人数 23 int getN() const { 24 return n; 25 } 26 27 // 更新预定人数 28 void setN(int new_n) { 29 n = new_n; 30 } 31 32 // 显示用户信息 33 void display() const { 34 cout << "昵称: " << nickname << endl; 35 cout << "联系方式: " << contact << endl; 36 cout << "所在城市: " << city << endl; 37 cout << "预定人数: " << n << endl; 38 cout << "--------------------" << endl; 39 } 40 }; 41 42 #endif // INFO_HPP
task6.cpp
1 #include "info.hpp" 2 #include <iostream> 3 #include <vector> 4 #include <string> 5 using std::cin; 6 using std::cout; 7 using std::endl; 8 using std::string; 9 using std::vector; 10 11 int main() { 12 const int capacity = 100; // livehouse最大容量 13 vector<Info> audience_lst; // 存储听众预约信息 14 int total = 0; // 当前总预约人数 15 16 cout << "请输入听众预约信息:" << endl; 17 18 while (true) { 19 string nickname, contact, city; 20 int n; 21 22 cout << "昵称: "; 23 cin >> nickname; 24 cout << "联系方式: "; 25 cin >> contact; 26 cout << "所在城市: "; 27 cin >> city; 28 cout << "预定参加人数: "; 29 cin >> n; 30 31 // 检查剩余容量 32 if (total + n > capacity) { 33 cout << "对不起,场地仅剩 " << (capacity - total) << " 个位置。" << endl; 34 cout << "1. 输入 u,更新(update)预定信息" << endl; 35 cout << "2. 输入 q,退出预定" << endl; 36 37 char choice; 38 cin >> choice; 39 40 if (choice == 'q') { 41 continue; // 放弃当前用户 42 } else if (choice == 'u') { 43 cout << "请重新输入预定信息:" << endl; 44 cout << "预定参加人数: "; 45 cin >> n; 46 if (n + total > capacity) { 47 cout << "仍超过剩余容量,预约失败。" << endl; 48 continue; 49 } 50 } 51 } 52 53 // 添加听众信息 54 audience_lst.emplace_back(nickname, contact, city, n); 55 total += n; 56 57 // 检查是否达到容量上限 58 if (total >= capacity) { 59 cout << "预约已满,无法继续预约。" << endl; 60 break; 61 } 62 63 // 停止录入(Ctrl+Z 或手动条件设计) 64 cout << "是否继续录入?(Ctrl+Z停止)" << endl; 65 if (cin.eof()) break; 66 } 67 68 // 输出听众预约信息 69 cout << "截至目前,共有 " << total << " 位听众预约。预约听众信息如下:" << endl; 70 cout << "--------------------" << endl; 71 for (const auto& audience : audience_lst) { 72 audience.display(); 73 } 74 75 return 0; 76 }
编译结果
实验任务七
代码
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::Account(const Date& date, const string& id) : id{id}, balance{0} { 9 date.show(); 10 cout << "\t#" << id << " created" << endl; 11 } 12 13 void Account::record(const Date& date, double amount, const string& desc) { 14 amount = floor(amount * 100 + 0.5) / 100; 15 balance += amount; 16 total += amount; 17 date.show(); 18 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 19 } 20 21 void Account::show() const { 22 cout << id << "\tBalance: " << balance; 23 } 24 25 void Account::error(const string& msg) const { 26 cout << "Error(#" << id << "): " << msg << endl; 27 } 28 29 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) 30 : Account(date, id), rate(rate), acc(date, 0) {} 31 32 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 33 record(date, amount, desc); 34 acc.change(date, getBalance()); 35 } 36 37 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 38 if (amount > getBalance()) { 39 error("not enough money"); 40 } else { 41 record(date, -amount, desc); 42 acc.change(date, getBalance()); 43 } 44 } 45 46 void SavingsAccount::settle(const Date& date) { 47 double interest = acc.getSum(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1)); 48 if (interest != 0) record(date, interest, "interest"); 49 acc.reset(date, getBalance()); 50 } 51 52 CreditAccount::CreditAccount(const Date& date, const string& id, double credit, double rate, double fee) 53 : Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) {} 54 55 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 56 record(date, amount, desc); 57 acc.change(date, getDebt()); 58 } 59 60 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 61 if (amount - getBalance() > credit) { 62 error("not enough credit"); 63 } else { 64 record(date, -amount, desc); 65 acc.change(date, getDebt()); 66 } 67 } 68 69 void CreditAccount::settle(const Date& date) { 70 double interest = acc.getSum(date) * rate; 71 if (interest != 0) record(date, interest, "interest"); 72 if (date.getMonth() == 1) record(date, -fee, "annual fee"); 73 acc.reset(date, getDebt()); 74 } 75 76 void CreditAccount::show() const { 77 Account::show(); 78 cout << "\tAvailable credit: " << getAvailableCredit(); 79 }
account.h
1 #pragma once 2 #ifndef ACCOUNT_H 3 #define ACCOUNT_H 4 5 #include "date.h" 6 #include "accumulator.h" 7 #include <string> 8 9 class Account { 10 private: 11 std::string id; 12 double balance; 13 static double total; 14 15 protected: 16 Account(const Date& date, const std::string& id); 17 void record(const Date& date, double amount, const std::string& desc); 18 void error(const std::string& msg) const; 19 20 public: 21 const std::string& getId() const { return id; } 22 double getBalance() const { return balance; } 23 static double getTotal() { return total; } 24 25 void show() const; 26 }; 27 28 class SavingsAccount : public Account { 29 private: 30 Accumulator acc; 31 double rate; 32 33 public: 34 SavingsAccount(const Date& date, const std::string& id, double rate); 35 double getRate() const { return rate; } 36 37 void deposit(const Date& date, double amount, const std::string& desc); 38 void withdraw(const Date& date, double amount, const std::string& desc); 39 void settle(const Date& date); 40 }; 41 42 class CreditAccount : public Account { 43 private: 44 Accumulator acc; 45 double credit; 46 double rate; 47 double fee; 48 49 double getDebt() const { 50 double balance = getBalance(); 51 return (balance < 0 ? balance : 0); 52 } 53 54 public: 55 CreditAccount(const Date& date, const std::string& id, double credit, double rate, double fee); 56 double getCredit() const { return credit; } 57 double getRate() const { return rate; } 58 double getAvailableCredit() const { 59 if (getBalance() < 0) 60 return credit + getBalance(); 61 else 62 return credit; 63 } 64 65 void deposit(const Date& date, double amount, const std::string& desc); 66 void withdraw(const Date& date, double amount, const std::string& desc); 67 void settle(const Date& date); 68 void show() const; 69 }; 70 71 #endif // ACCOUNT_H
accumulator.h
1 #pragma once 2 #ifndef ACCUMULATOR_H 3 #define ACCUMULATOR_H 4 5 #include "date.h" 6 7 class Accumulator { 8 private: 9 Date lastDate; 10 double value; 11 double sum; 12 13 public: 14 Accumulator(const Date& date, double value) : lastDate(date), value(value), sum{0} {} 15 16 double getSum(const Date& date) const { 17 return sum + value * date.distance(lastDate); 18 } 19 20 void change(const Date& date, double value) { 21 sum = getSum(date); 22 lastDate = date; 23 this->value = value; 24 } 25 26 void reset(const Date& date, double value) { 27 lastDate = date; 28 this->value = value; 29 sum = 0; 30 } 31 }; 32 33 #endif // ACCUMULATOR_H
date.cpp
1 #include "date.h" 2 #include <iostream> 3 #include <cstdlib> 4 using namespace std; 5 6 namespace { 7 const int DAYS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 }; 8 } 9 10 Date::Date(int year, int month, int day) : year(year), month(month), day(day) { 11 if (day <= 0 || day > getMaxDay()) { 12 cout << "Invalid date:"; 13 show(); 14 cout << endl; 15 exit(1); 16 } 17 int years = year - 1; 18 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day; 19 if (isLeapYear() && month > 2) totalDays++; 20 } 21 22 int Date::getMaxDay() const { 23 if (isLeapYear() && month == 2) 24 return 29; 25 else 26 return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1]; 27 } 28 29 void Date::show() const { 30 cout << getYear() << "-" << getMonth() << "-" << getDay(); 31 }
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; 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 int distance(const Date& date) const { 22 return totalDays - date.totalDays; 23 } 24 }; 25 26 #endif // __DATE_H__
task7.cpp
1 #include "account.h" 2 #include <iostream> 3 4 using namespace std; 5 6 int main() { 7 Date date(2008, 11, 1); 8 SavingsAccount sa1(date, "S3755217", 0.015); 9 SavingsAccount sa2(date, "02342342", 0.015); 10 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 11 12 sa1.deposit(Date(2008, 11, 5), 5000, "salary"); 13 ca.withdraw(Date(2008, 11, 15), 2000, "buy a cell"); 14 sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); 15 16 ca.settle(Date(2008, 12, 1)); 17 18 ca.deposit(Date(2008, 12, 1), 2016, "repay the credit"); 19 sa1.deposit(Date(2008, 12, 5), 5500, "salary"); 20 21 sa1.settle(Date(2009, 1, 1)); 22 sa2.settle(Date(2009, 1, 1)); 23 ca.settle(Date(2009, 1, 1)); 24 25 cout << endl; 26 sa1.show(); 27 cout << endl; 28 sa2.show(); 29 cout << endl; 30 ca.show(); 31 cout << endl; 32 cout << "Total:" << Account::getTotal() << endl; 33 return 0; 34 }
编译结果