forgiver

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

实验任务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:成绩储存在每个对象里;通过this指针来进行访问;通过this指针调用继承自vector的接口pushi_back()

问题2:除以总人数,求平均值;有影响;因为平均数有可能是一个小数,如果除数与被除数都为整数,‘/’运算符默认结果也为整数

问题3:没有添加,删除,修改成绩的接口

 

 

 

实验任务3:

代码:

GradCalc.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:储存在grades里;通过gradesd的接口

问题2:面向对象编程具有高效的重用性

 

 

 

实验任务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 }

 

运行结果截图:

 

回答问题:

问题1:清空输入流,在这里'\n'表明清空当前行

 

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 }

 

运行结果截图:

 

回答问题:

问题2:清空输入n后的换行

 

 

实验任务5:

代码:

grm.hpp

 1 #pragma once
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 template <typename T>
 8 class GameResourceManager {
 9 public:
10     GameResourceManager(T r = 0);
11     T get() const;
12     void update(T new_r);
13 
14 private:
15     T resource;
16 };
17 
18 template <typename T>
19 GameResourceManager<T>::GameResourceManager(T r) : resource{ r } {}
20 
21 template <typename T>
22 T GameResourceManager<T>::get() const {
23     return resource;
24 }
25 
26 template <typename T>
27 void GameResourceManager<T>::update(T new_r) {
28     resource += new_r;
29     if (resource < 0)
30         resource = 0;
31 }

 

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 
 3 #include <iostream>
 4 #include <iomanip>
 5 
 6 using namespace std;
 7 
 8 class Info {
 9 public:
10     Info(const string &name, const string &contact0, const string &city0, int num);
11     void display() const;
12 
13 private:
14     string nickname;
15     string contact;
16     string city;
17     int n;
18 };
19 
20 Info::Info(const string& name, const string& contact0, const string& city0, int num):
21     nickname{name}, contact{contact0}, city{city0}, n{num}{}
22 
23 void Info::display() const {
24     cout << "昵称:  \t" << nickname << endl
25         << "联系方式:\t" << contact << endl
26         << "所在城市:\t" << city << endl
27         << "预定人数:\t" << n << endl;
28 }

 

task6.cpp

 1 #include "info.hpp"
 2 
 3 #include <iostream>
 4 #include <vector>
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 const int capacity = 100;
10 
11 void test(){
12     vector<Info> audience_list;
13     int num, sum = 0, count = 0;
14     string name, contact, city;
15 
16 
17     while (sum < capacity && cin >> name >> contact >> city >> num) {
18         sum += num;
19         if(sum <= capacity)
20             audience_list.push_back(Info(name, contact, city, num));
21         else {
22             sum -= num;
23             cout << "对不起,只剩" << capacity - sum << "个位置." << endl;
24             cout << "1.输入u,更新预定信息" << endl;
25             cout << "2.输入q,退出预定" << endl;
26             cout << "你的选择:";
27             char choice;
28             cin >> choice;
29 
30             if (choice == 'q') {
31                 continue;
32             }
33             else {
34                 cout << "请重新输入预定信息:" << endl;
35                 cin >> name >> contact >> city >> num;
36                 audience_list.push_back(Info(name, contact, city, num));
37                 sum += num;
38             }
39         }
40     }
41 
42     cout << "截至目前,一共有" << sum << "位听众预约。预约听众信息如下:" << endl;
43     for (auto i : audience_list) {
44         i.display();
45         cout << endl;
46     }
47 
48 }
49 
50 int main() {
51     test();
52 }

 

运行结果截图:

 

 

 

 

 

实验任务7:

代码:

date.h

 1 #pragma once
 2 
 3 class Date {
 4 private:
 5     int year;
 6     int month;
 7     int day;
 8     int totalDays;
 9 
10 public:
11     Date(int year, int month, int day);
12     int getYear() const { return year; }
13     int getMonth() const { return month; }
14     int getDay() const { return day; }
15     int getMaxDay() const;
16     bool isLeapYear() const {
17         return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
18     }
19     void show() const;
20     int distance(const Date& date) const {
21         return totalDays - date.totalDays;
22     }
23 };

 

date.cpp

 1 #include "date.h"
 2 #include <iostream>
 3 #include <cstdlib>
 4 
 5 using namespace std;
 6 
 7 namespace {
 8     const int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
 9 }
10 
11 int Date::getMaxDay() const {
12     if (isLeapYear() && month == 2)
13         return 29;
14     else
15         return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
16 }
17 
18 void Date::show() const {
19     cout << getYear() << "-" << getMonth() << "-" << getDay();
20 }
21 
22 Date::Date(int year, int month, int day) : year{ year }, month{ month }, day{ day } {
23     if (day <= 0 || day > getMaxDay()) {
24         cout << "Invalid date: ";
25         show();
26         cout << endl;
27         exit(1);
28     }
29     int years = year - 1;
30     totalDays = years * 365 + years / 4 - years / 100 + years / 400
31         + DAYS_BEFORE_MONTH[month - 1] + day;
32     if (isLeapYear() && month > 2) totalDays++;
33 }

 

accumulator.h

 1 #pragma once
 2 #include "date.h"
 3 
 4 class Accumulator {
 5 private:
 6     Date lastDate;
 7     double value;
 8     double sum;
 9 
10 public:
11     Accumulator(const Date& date, double value)
12         :lastDate{date}, value{value}, sum{0}{}
13 
14     double getSum(const Date& date) const {
15         return sum + value * date.distance(lastDate);
16     }
17 
18     void change(const Date& date, double value) {
19         sum = getSum(date);
20         lastDate = date;
21         this->value = value;
22     }
23 
24     void reset(const Date& date, double value) {
25         lastDate = date;
26         this->value = value;
27         sum = 0;
28     }
29 
30 };

 

account.h

 1 #pragma once
 2 #include "date.h"
 3 #include "Accumulator.h"
 4 #include <string>
 5 
 6 class Account {  //账户类
 7 private:
 8     std::string id;  //账号
 9     double balance;  //余额
10     static double total;  //所有账户的总金额
11 
12 protected:
13     Account(const Date& date, const std::string& id);  //构造函数
14     void record(const Date& date, double amount, const std::string& desc);  //记账
15     void error(const std::string& msg) const;  //报告错误信息
16 
17 public:
18     const std::string& getId() const { return id; }
19     double getBalance() const { return balance; }
20     static double getTotal() { return total; }
21     void show() const;  //显示账户信息
22 };
23 
24 class SavingsAccount : public Account {  //储蓄类账户
25 private:
26     Accumulator acc;  //辅助计算利息的累加器
27     double rate;  //存款的年利率
28 
29 public:
30     SavingsAccount(const Date& date, const std::string& id, double rate);  //构造函数
31     double getRate() const { return rate; }
32     void deposit(const Date& date, double amount, const std::string& desc);  //存入现金
33     void withdraw(const Date& date, double amount, const std::string& desc);  //取出现金
34     void settle(const Date& date);  //结算利息
35 };
36 
37 class CreditAccount : public Account {  //信用账户类
38 private:
39     Accumulator acc;  //辅助计算利息的累加器
40     double credit;  //信用额度
41     double rate;  //欠款的日利率
42     double fee;  //信用卡年费
43     double getDebt() const {  //获得欠款额
44         double balance = getBalance();
45         return (balance < 0 ? balance : 0);
46     }
47 public:
48     CreditAccount(const Date& date, const std::string& id, double credit, double rate, double fee);
49     double getCredit() const { return credit; }
50     double getRate() const { return rate; }
51     double getFee() const { return fee; }
52     double getAvailableCredit() const {  //获得可用信用
53         if (getBalance() < 0)
54             return credit + getBalance();
55         else
56             return credit;
57     }
58     void deposit(const Date& date, double amount, const std::string& desc);  //存入现金
59     void withdraw(const Date& date, double amount, const std::string& desc);  //取出现金
60     void settle(const Date& date);  //结算利息
61     void show() const;
62 };

 

account.cpp

 1 #include "account.h"
 2 #include <cmath>
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 //Account类的实现
 8 double Account::total = 0;
 9 
10 Account::Account(const Date& date, const std::string& id)
11     : id{ id }, balance{ 0 } {
12     date.show();
13     cout << "\t#" << id << "created" << endl;
14 }
15 
16 void Account::record(const Date& date, double amount, const string& desc) {
17     amount = floor(amount * 100 + 0.5) / 100;
18     balance += amount;
19     total += amount;
20     date.show();
21     cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
22 }
23 
24 void Account::show() const {
25     cout << id << "\tBalance:" << balance;
26 }
27 
28 void Account::error(const string& msg) const {
29     cout << "Error(#" << id << "):" << msg << endl;
30 }
31 
32 
33 //SavingsAccount类的实现
34 SavingsAccount::SavingsAccount(const Date& date, const std::string& id, double rate)
35     : Account{ date, id }, rate{ rate }, acc{date, 0} {}
36 
37 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) {
38     record(date, amount, desc);
39     acc.change(date, getBalance());
40 }
41 
42 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) {
43     if (amount > getBalance())
44         error("not enough money");
45     else {
46         record(date, -amount, desc);
47         acc.change(date, getBalance());
48     }
49 }
50 
51 void SavingsAccount::settle(const Date& date) {
52     double interest = acc.getSum(date) * rate  //计算年息
53         / date.distance(Date(date.getYear() - 1, 1, 1));
54     if (interest != 0)
55         record(date, interest, "interest");
56     acc.reset(date, getBalance());
57 }
58 
59 //CreditAccount类的实现
60 CreditAccount::CreditAccount(const Date& date, const std::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 }

 

7_10.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(); cout << endl;
27     sa2.show(); cout << endl;
28     ca.show(); cout << endl;
29     cout << "Total:" << Account::getTotal() << endl;
30 }

 

运行结果截图:

 

总结:

改进:将SavingsAccount和CreditAccount中相同的操作统一放到了Account基类中

问题:SavingsAccount和CreditAccount派生类中依旧有很多操作大致相同,只是细节上不同

 

posted on 2024-11-25 13:20  Forgiver  阅读(3)  评论(0编辑  收藏  举报