实验4 类的组合、继承、模板类、标准库
实验任务2:
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 }
task2.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 }
运行测试截图:
问题1:派生类GradeCalc定义中,成绩存储在哪里?派生类方法sort, min, max, average,
output都要访问成绩,是通过什么接口访问到每个成绩的?input方法是通过什么接口实现数
据存入对象的?
GradeCalc
对象内部有一个 vector<int>
的数据成员,用于存储成绩。通过
this
指针访问成绩。input
方法通过 this->push_back(grade)
将成绩添加到 GradeCalc
对象中。问题2:代码line68分母的功能是?去掉乘以1.0代码,重新编译、运行,结果有影响吗?为什
么要乘以1.0?
计算所占百分比,无法求出人数百分比,改变数据的类型,由整型变为浮点型。
问题3:从真实应用场景角度考虑,GradeCalc类在设计及代码实现细节上,有哪些地方尚未
考虑周全,仍需继续迭代、完善?
判断输入成绩是否合法,不能对学生多课程进行处理。
实验任务3:
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 }
task3.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 }
运行测试截图:
问题1:组合类GradeCalc定义中,成绩存储在哪里?组合类方法sort, min, max, average,
output都要访问成绩,是通过什么访问到每一个成绩的?观察与实验任务2在代码写法细节上
的差别。
在组合类GradeCalc
的定义中,成绩存储在私有成员变量vector<int> grades
中。
组合类方法sort
, min
, max
, average
, output
都需要访问这些成绩,它们通过直接访问grades
向量来实现。
问题2:对比实验任务2和实验任务3,主体代码逻辑(测试代码)没有变更,类GradeCalc的
接口也没变,变化的是类GradeCalc的设计及接口内部实现细节。你对面向对象编程有什么新
的理解和领悟吗
类设计的区别
实验任务4:
task4_1.cpp源码:
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 }
运行测试截图:
task4_2.cpp源码:
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 }
运行测试截图:
task4_3.cpp源码:
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 }
运行测试截图:
问题1:去掉task4_1.cpp的line35,重新编译、运行,给出此时运行结果截图。查阅资料,回
答line35在这里的用途是什么?
忽略输入流cin
中从当前位置开始直到下一个换行符(包括换行符本身)的所有字符,或者忽略最多numeric_limits<streamsize>::max()
个字符,以先到者为准。这通常用于清除输入缓冲区中的残留字符,特别是在处理连续输入时,避免前一次输入的换行符或多余字符影响到下一次输入。
问题2:去掉task4_3.cpp的line16,重新编译、运行,给出此时运行结果。查阅资料,回答
line16在这里的用途是什么?
忽略输入流中从当前位置开始直到下一个换行符(包括换行符本身)的所有字符,或者忽略最多
numeric_limits<streamsize>::max()
个字符(以先到者为准)。这个语句在处理连续输入时非常有用,可以避免输入流中的残留字符对后续输入造成影响。实验任务5:
grm.hpp源码:
1 #pragma once 2 #ifndef GRM_HPP 3 #define GRM_HPP 4 5 #include <limits> 6 7 template <typename T> 8 class GameResourceManager { 9 private: 10 T resource; 11 12 public: 13 // 带参数的构造函数,初始化当前资源数量 14 GameResourceManager(T initial_resource) : resource(initial_resource) {} 15 16 // 获取当前的资源数量 17 T get() const { 18 return resource; 19 } 20 21 // 更新当前的资源数量(增加、减少),当资源数量减少到<0时,则归0 22 void update(T delta) { 23 resource += delta; 24 if (resource < 0) { 25 resource = 0; 26 } 27 } 28 }; 29 30 #endif // GRM_HPP
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 }
运行测试截图:
实验任务6:
info.hpp源码:
1 #pragma once 2 #ifndef INFO_HPP 3 #define INFO_HPP 4 5 #include <iostream> 6 #include <string> 7 8 class Info { 9 private: 10 std::string nickname; 11 std::string contact; 12 std::string city; 13 int n; 14 15 public: 16 // 带参数的构造函数 17 Info(std::string nick, std::string cont, std::string cit, int num) 18 : nickname(nick), contact(cont), city(cit), n(num) {} 19 20 // 显示信息 21 void display() const { 22 std::cout << "昵称: " << nickname << "\n" 23 << "联系方式: " << contact << "\n" 24 << "所在城市: " << city << "\n" 25 << "预定参加人数: " << n << "\n"; 26 } 27 }; 28 29 #endif // INFO_HPP
task6.cpp源码:
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include "info.hpp" 5 6 const int capacity = 100; 7 8 int main() { 9 std::vector<Info> audience_lst; 10 int remaining_capacity = capacity; 11 std::string nickname, contact, city; 12 int n; 13 14 std::cout << "请输入预约信息(按Ctrl+Z结束输入或人数达到上限时提示):\n"; 15 16 while (true) { 17 std::cout << "昵称: "; 18 std::cin >> nickname; 19 std::cout << "联系方式: "; 20 std::cin >> contact; 21 std::cout << "所在城市: "; 22 std::cin >> city; 23 std::cout << "预定参加人数: "; 24 std::cin >> n; 25 26 if (n > remaining_capacity) { 27 std::cout << "预定人数超过场地剩余容量,请输入q退出预定或输入u更新预定信息。\n"; 28 char choice; 29 std::cin >> choice; 30 if (choice == 'q') { 31 break; 32 } 33 else if (choice == 'u') { 34 continue; // 重新输入预定信息 35 } 36 else { 37 std::cout << "无效选择,请重新输入。\n"; 38 continue; 39 } 40 } 41 else { 42 audience_lst.emplace_back(nickname, contact, city, n); 43 remaining_capacity -= n; 44 if (remaining_capacity <= 0 || std::cin.eof()) { 45 break; 46 } 47 } 48 } 49 50 std::cout << "\n预约参加livehouse的听众信息:\n"; 51 for (const auto& info : audience_lst) { 52 info.display(); 53 std::cout << "----------------------------\n"; 54 } 55 56 return 0; 57 }
运行测试截图:
实验任务7:
date.h:
1 #pragma once 2 class Date { 3 private: 4 int year; 5 int month; 6 int day; 7 int totalDays; 8 public: 9 Date(int year, int month, int day); 10 int getYear()const { return year; } 11 int getMonth()const { return month; } 12 int getDay()const { return day; } 13 int getMaxDay()const; 14 bool isLeapYear()const { 15 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 16 } 17 void show() const; 18 int distance(const Date& date)const { 19 return totalDays - date.totalDays; 20 } 21 };
date.cpp:
1 #pragma once 2 #include"date.h" 3 #include"accumulator.h" 4 #include<string> 5 using namespace std; 6 class Account { 7 private: 8 string id; 9 double balance; 10 static double total; 11 protected: 12 Account(const Date& date, const string &id); 13 void record(const Date& date, double amount, const string& desc); 14 void error(const string& msg) const; 15 public:const string& getId() { return id; } 16 double getBalance()const { return balance; } 17 static double getTotal() { return total; } 18 void show()const; 19 }; 20 class SavingsAccount:public Account { 21 private: 22 Accumulator acc; 23 double rate; 24 public: 25 SavingsAccount(const Date& date, const string& id, double rate); 26 double getRate() const { return rate; } 27 void deposit(const Date& date, double amount, const string& desc); 28 void withdraw(const Date& date, double amount, const string& desc); 29 void settle(const Date& date); 30 }; 31 class CreditAccount :public Account { 32 private: 33 Accumulator acc; 34 double credit; 35 double rate; 36 double fee; 37 double getDebt()const { 38 double balance = getBalance(); 39 return(balance < 0 ? balance : 0); 40 } 41 public:CreditAccount(const Date& date, const string& id, double credit, double rate, double fee); 42 double getCredit()const { return credit; } 43 double getRate()const { return rate; } 44 double getFee() const { return fee; } 45 double getAvailableCredit()const { 46 if (getBalance() < 0)return credit + getBalance(); 47 else return credit; 48 } 49 void deposit(const Date& date, double amount, const string& desc); 50 void withdraw(const Date& date, double amount, const string& desc); 51 void settle(const Date& date); 52 void show()const; 53 };
accumulator.h:
1 #pragma once 2 #include "date.h" 3 class Accumulator { 4 private: 5 Date lastDate; 6 double value; 7 double sum; 8 public: 9 Accumulator(const Date&date,double value):lastDate(date),value(value),sum{0}{} 10 double getSum(const Date& date) const { 11 return sum + value * date.distance(lastDate); 12 } 13 void change(const Date& date, double value) { 14 sum = getSum(date); 15 lastDate = date; 16 this->value = value; 17 } 18 void reset(const Date& date, double value) { 19 lastDate = date; 20 this->value; 21 sum = 0; 22 } 23 };
account.h:
1 #pragma once 2 #include"date.h" 3 #include"accumulator.h" 4 #include<string> 5 using namespace std; 6 class Account { 7 private: 8 string id; 9 double balance; 10 static double total; 11 protected: 12 Account(const Date& date, const string &id); 13 void record(const Date& date, double amount, const string& desc); 14 void error(const string& msg) const; 15 public:const string& getId() { return id; } 16 double getBalance()const { return balance; } 17 static double getTotal() { return total; } 18 void show()const; 19 }; 20 class SavingsAccount:public Account { 21 private: 22 Accumulator acc; 23 double rate; 24 public: 25 SavingsAccount(const Date& date, const string& id, double rate); 26 double getRate() const { return rate; } 27 void deposit(const Date& date, double amount, const string& desc); 28 void withdraw(const Date& date, double amount, const string& desc); 29 void settle(const Date& date); 30 }; 31 class CreditAccount :public Account { 32 private: 33 Accumulator acc; 34 double credit; 35 double rate; 36 double fee; 37 double getDebt()const { 38 double balance = getBalance(); 39 return(balance < 0 ? balance : 0); 40 } 41 public:CreditAccount(const Date& date, const string& id, double credit, double rate, double fee); 42 double getCredit()const { return credit; } 43 double getRate()const { return rate; } 44 double getFee() const { return fee; } 45 double getAvailableCredit()const { 46 if (getBalance() < 0)return credit + getBalance(); 47 else return credit; 48 } 49 void deposit(const Date& date, double amount, const string& desc); 50 void withdraw(const Date& date, double amount, const string& desc); 51 void settle(const Date& date); 52 void show()const; 53 };
account.cpp:
1 #include "account.h" 2 #include <cmath> 3 #include<iostream> 4 using namespace std; 5 double Account::total = 0; 6 Account::Account(const Date& date, const string& id) :id(id), balance(0) { 7 date.show(); 8 cout << "\t#" << id << "created" << endl; 9 } 10 void Account::record(const Date& date, double amount, const string& desc) { 11 amount = floor(amount * 100 + 0.5) / 100; 12 balance += amount; 13 total += amount; 14 date.show(); 15 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 16 } 17 void Account::show()const { cout << id << "\tBalance:" << balance; } 18 void Account::error(const string& msg)const { 19 cout << "Error(#" << id << "):" << msg << endl; 20 } 21 SavingsAccount::SavingsAccount(const Date&date,const string &id,double rate):Account(date,id),rate(rate),acc(date,0){} 22 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 23 record(date, amount, desc); 24 acc.change(date, getBalance()); 25 } 26 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 27 if (amount > getBalance()) { 28 error("not enough money"); 29 } 30 else { 31 record(date, -amount, desc); 32 acc.change(date, getBalance()); 33 } 34 } 35 void SavingsAccount::settle(const Date& date) { 36 double interest = acc.getSum(date) * rate/date.distance(Date(date.getYear()-1,1,1)); 37 if (interest != 0)record(date, interest, "interest"); 38 acc.reset(date, getBalance()); 39 } 40 CreditAccount::CreditAccount(const Date&date,const string &id,double credit,double rate,double fee):Account(date,id),credit(credit),rate(rate),fee(fee),acc(date,0){} 41 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 42 record(date, amount, desc); 43 acc.change(date, getDebt()); 44 } 45 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 46 if (amount - getBalance() > credit) { 47 error("not enouogh credit"); 48 } 49 else { 50 record(date, -amount, desc); 51 acc.change(date, getDebt()); 52 } 53 } 54 void CreditAccount::settle(const Date& date) { 55 double interest = acc.getSum(date) * rate; 56 if (interest != 0) record(date, interest, "interest"); 57 if (date.getMonth() == 1)record(date, -fee, "annual fee"); 58 acc.reset(date, getDebt()); 59 } 60 void CreditAccount::show()const { 61 Account::show(); 62 cout << "\tAvailable credit:" << getAvailableCredit(); 63 }
7_10.cpp源码:
1 #include "account.h" 2 #include<iostream> 3 using namespace std; 4 int main() { 5 Date date(2008, 11, 1); 6 SavingsAccount sa1(date, "S3755217", 0.015); 7 SavingsAccount sa2(date, "02342342", 0.015); 8 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 9 sa1.deposit(Date(2008, 11, 5), 5000, "Salary"); 10 ca.withdraw(Date(2008, 11, 15), 2000, "buy a cell"); 11 sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); 12 ca.settle(Date(2008, 12, 1)); 13 ca.deposit(Date(2008, 12,1), 2016, "repay the credit"); 14 sa1.deposit(Date(2008, 12, 5), 5500, "salary"); 15 sa1.settle(Date(2009, 1, 1)); 16 sa2.settle(Date(2009, 1, 1)); 17 ca.settle(Date(2009, 1, 1)); 18 cout << endl; 19 sa1.show(); cout << endl; 20 sa2.show(); cout << endl; 21 ca.show(); cout << endl; 22 cout << "Total: " << Account::getTotal() << endl; 23 return 0; 24 25 }
运行测试截图: