OOP实验四

任务2:

源码:

  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 }
 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方法是通过什么接口实现数
据存入对象的?
基类vector。vector中的接口。vector::push_back。
问题2:代码line68分母的功能是?去掉乘以1.0代码,重新编译、运行,结果有影响吗?为什
么要乘以1.0?
将数据转换为浮点型。有。输出小数而不是整数。
问题3:从真实应用场景角度考虑,GradeCalc类在设计及代码实现细节上,有哪些地方尚未
考虑周全,仍需继续迭代、完善?
缺少查询功能。

任务3:

源码:

  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 }
 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在代码写法细节上
的差别。
成员vector。vector本身的接口。
问题2:对比实验任务2和实验任务3,主体代码逻辑(测试代码)没有变更,类GradeCalc的
接口也没变,变化的是类GradeCalc的设计及接口内部实现细节。你对面向对象编程有什么新
的理解和领悟吗?
封装方式多样。

任务4:

源码:

 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 #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 }
 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在这里的用途是什么?

 消除回车。

问题2:去掉task4_3.cpp的line16,重新编译、运行,给出此时运行结果。查阅资料,回答
line16在这里的用途是什么?

 消除回车。

任务5:

源码:

 1 #include<iostream>
 2 using namespace std;
 3 template <typename T>
 4 class GameResourceManager {
 5 private:
 6     T resource;
 7 public:
 8     GameResourceManager(T data);
 9     T get() const;
10     void update(T date);
11 };
12 template <typename T>
13 GameResourceManager<T>::GameResourceManager(T data) :resource{ data } {};
14 template <typename T>
15 T GameResourceManager<T>::get() const {
16     return resource;
17 }
18 template <typename T>
19 void GameResourceManager<T>::update(T data) {
20     resource += data;
21     if (resource < 0) {
22         resource = 0;
23     }
24 }
25 
26 
27 #include "grm.hpp"
28 #include <iostream>
29 
30 using std::cout;
31 using std::endl;
32 
33 void test1() {
34     GameResourceManager<float> HP_manager(99.99);
35     cout << "当前生命值: " << HP_manager.get() << endl;
36     HP_manager.update(9.99);
37     cout << "增加9.99生命值后, 当前生命值: " << HP_manager.get() << endl;
38     HP_manager.update(-999.99);
39     cout << "减少999.99生命值后, 当前生命值: " << HP_manager.get() << endl;
40 }
41 
42 void test2() {
43     GameResourceManager<int> Gold_manager(100);
44     cout << "当前金币数量: " << Gold_manager.get() << endl;
45     Gold_manager.update(50);
46     cout << "增加50个金币后, 当前金币数量: " << Gold_manager.get() << endl;
47     Gold_manager.update(-99);
48     cout << "减少99个金币后, 当前金币数量: " << Gold_manager.get() << endl;
49 }
50 
51 
52 int main() {
53     cout << "测试1: 用float类型对类模板GameResourceManager实例化" << endl;
54     test1();
55     cout << endl;
56 
57     cout << "测试2: 用int类型对类模板GameResourceManager实例化" << endl;
58     test2();
59 }

 

结果:

 

任务6:

源码:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class Info {
 5 private:
 6     string nikename;
 7     string contact;
 8     string city;
 9     int n;
10     static int sum;
11 public:
12     Info(string name, string ct, string ci, int num);
13     Info(const Info& info);
14     string getName()const;
15     string getContact()const;
16     string getCity()const;
17     int getN()const;
18     void display()const;
19     static int getSum();
20     static void add(int num);
21 };
22 int Info::sum = 0;
23 Info::Info(string name, string ct, string ci, int num) :nikename{ name }, contact{ ct }, city{ ci }, n{ num } {}
24 Info::Info(const Info& info) {
25     nikename = info.getName();
26     contact = info.getContact();
27     city = info.getCity();
28     n = info.getN();
29 }
30 string Info::getName()const {
31     return nikename;
32 }
33 string Info::getContact()const {
34     return contact;
35 }
36 string Info::getCity()const {
37     return city;
38 }
39 int Info::getN()const {
40     return n;
41 }
42 void Info::display()const {
43     cout << "昵称:\t" << nikename << endl
44         << "联系方式:\t" << contact << endl
45         << "所在城市:\t" << city << endl
46         << "预定人数:\t" << n << endl;
47 }
48 int Info::getSum() {
49     return sum;
50 }
51 void Info::add(int num) {
52     sum += num;
53 }
54 
55 
56 #include"Info.hpp"
57 #include<vector>
58 #define capacity 100
59 int main() {
60     vector<Info> audience_lst;
61     cout << "录入用户预约信息:" << endl;
62     cout << "昵称\t\t联系方式(邮箱/手机号)\t\t所在城市\t\t预约参加人数" << endl;
63     while (Info::getSum() < capacity) {
64         string name, ct, ci;
65         int num;
66         if (!(cin >> name >> ct >> ci >> num)) {
67             break;
68         }
69         if (num + Info::getSum() > capacity) {
70             cout << "对不起,只剩" << capacity - Info::getSum() << "个位置" << endl;
71             cout << "1.输入u,更新(update)预定信息" << endl << "2.输入q,放弃预定" << endl << "你的选择:";
72             char choice;
73             cin >> choice;
74             if (choice == 'u') {
75                 cout << "请重新输入预定信息:" << endl;
76                 continue;
77             }
78             else{
79                 break;
80             }
81         }
82         else {
83             Info audience(name, ct, ci, num);
84             Info::add(num);
85             audience_lst.push_back(audience);
86         }
87     }
88     cout << "截至目前为止,一共有" << Info::getSum() << "位听众预约。预约听众信息如下:" << endl;
89     cout << "-------------------------------------" << endl;
90     for (auto i : audience_lst) {
91         cout << "昵称:\t\t" << i.getName() << endl 
92             << "联系方式:\t" << i.getContact() << endl 
93             << "所在城市:\t" << i.getCity() << endl 
94             << "预定人数:\t" << i.getN() << endl;
95     }
96     return 0;
97 }

结果:

 

 

 

任务7:

源码:

  1 #pragma once
  2 #ifndef  ACCOUNT H
  3 #define  ACCOUNT H
  4 #include"date.h"
  5 #include"accumulator.h"
  6 #include<string>
  7 class Account {
  8 private:
  9     std::string id;
 10     double balance;
 11     static double total;
 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  public:
 17     const std::string & getId()const { return id; }
 18     double getBalance()const { return balance; }
 19     static double getTotal() { return total; }
 20 
 21    void show()const;
 22 
 23 };
 24 class SavingsAccount :public Account {
 25 private:
 26      Accumulator acc;
 27      double rate;
 28 public:
 29     SavingsAccount(const Date & date, const std::string & id, double rate);
 30     double getRate()const { return rate; }
 31 
 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 class CreditAccount :public Account {
 37 private:
 38     Accumulator acc;
 39     double credit;
 40     double rate;
 41     double fee;
 42     double getDebt()const {
 43         double balance = getBalance();
 44         return (balance < 0 ? balance : 0);
 45     }
 46 public:
 47     CreditAccount(const Date & date, const std::string & id, double credit, double rate, double fee);
 48     double getCredit()const { return credit; }
 49     double getRate()const { return rate; }
 50     double getAvailableCredit()const {
 51      if (getBalance() < 0)
 52            return credit + getBalance();
 53      else
 54            return credit;
 55     }
 56     void deposit(const Date & date, double amount, const std::string & desc);
 57     void withdraw(const Date & date, double amount, const std::string & desc);
 58     void settle(const Date & date);
 59     void show()const;
 60 };
 61 #endif//ACCOUNT H
 62 
 63 
 64 #pragma once
 65 #ifndef  ACCUMULATOR H
 66 #define  ACCUMULATOR H
 67 #include"date.h"
 68 class Accumulator {
 69 private:
 70     Date lastDate;
 71     double value;
 72     double sum;
 73 public:
 74      Accumulator(const Date & date, double value) :lastDate(date), value(value), sum{ 0 } {}
 75      double getSum(const Date & date)const {
 76          return sum + value * date.distance(lastDate);
 77      }
 78      void change(const Date & date, double value) {
 79          sum = getSum(date);
 80          lastDate = date; this->value = value;
 81      }
 82 
 83      void reset(const Date & date, double value) {
 84          lastDate = date; this->value = value; sum = 0;
 85      }
 86 };
 87 #endif//ACCUMULATOR H
 88 
 89 
 90 #pragma once
 91 #ifndef  DATE H
 92 #define  DATE H
 93 class Date {
 94 private:
 95     int year;
 96     int month;
 97     int day;
 98     int totalDays;
 99 public:
100      Date(int year, int month, int day);
101      int getYear()const { return year; }
102      int getMonth()const { return month; }
103      int getDay()const { return day; }
104      int getMaxDay()const;
105      bool isLeapYear()const {
106          return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
107      }
108      void show()const;
109      int distance(const Date & date)const {
110          return totalDays - date.totalDays;
111      }
112 };
113 #endif//  DATE H
114 
115 
116 
117 #include"account.h"
118 #include<cmath>
119 #include<iostream>
120 using namespace std;
121 double Account::total = 0;
122 
123 Account::Account(const Date & date, const string & id) :id{ id }, balance{ 0 } {
124      date.show(); cout << "\t#" << id << "created" << endl;
125 }
126 void Account::record(const Date & date, double amount, const string & desc) {
127      amount = floor(amount * 100 + 0.5) / 100;
128      balance += amount;
129      total += amount;
130      date.show();
131      cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
132 }
133 void Account::show()const { cout << id << "\tBalance:" << balance; }
134 void Account::error(const string & msg)const {
135      cout << "Error(#" << id << "):" << msg << endl;
136 }
137 
138 SavingsAccount::SavingsAccount(const Date & date, const string & id, double rate) :Account(date, id), rate(rate), acc(date, 0) {}
139 
140 void SavingsAccount::deposit(const Date & date, double amount, const string & desc) {
141      record(date, amount, desc);
142      acc.change(date, getBalance());
143 }
144 void SavingsAccount::withdraw(const Date & date, double amount, const string & desc) {
145      if (amount > getBalance()) {
146          error("not enough money");
147     }
148      else {
149          record(date, -amount, desc);
150          acc.change(date, getBalance());
151     }
152 }
153 
154 void SavingsAccount::settle(const Date & date) {
155     double interest = acc.getSum(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1));
156     if (interest != 0)record(date, interest, "interest");
157     acc.reset(date, getBalance());
158 }
159 
160 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) {}
161 
162 void CreditAccount::deposit(const Date & date, double amount, const string & desc) {
163      record(date, amount, desc);
164      acc.change(date, getDebt());
165 }
166 
167 void CreditAccount::withdraw(const Date & date, double amount, const string & desc) {
168     if (amount - getBalance() > credit) {
169          error("not enough credit");
170     }
171     else {
172          record(date, -amount, desc);
173          acc.change(date, getDebt());
174     }
175 }
176 
177 void CreditAccount::settle(const Date & date) {
178     double interest = acc.getSum(date) * rate;
179     if (interest != 0)record(date, interest, "interest");
180     if (date.getMonth() == 1)
181     record(date, -fee, "annual fee");
182     acc.reset(date, getDebt());
183 }
184 void CreditAccount::show()const {
185      Account::show();
186      cout << "\tAvailable credit:" << getAvailableCredit();
187 }
188 
189 
190 
191 #include"date.h"
192 #include<iostream>
193 #include<cstdlib>
194 using namespace std;
195 namespace {
196      const int DAYS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
197 }
198 Date::Date(int year, int month, int day) :year{ year }, month{ month }, day{ day } {
199     if (day <= 0 || day > getMaxDay()) {
200          cout << "Invalid date:";
201          show();
202          cout << endl;
203          exit(1);
204 
205     }
206     int years = year - 1;
207     totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day;
208     if (isLeapYear() && month > 2)totalDays++;
209 }
210 int Date::getMaxDay()const {
211      if (isLeapYear() && month == 2)
212          return 29;
213      else return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
214 }
215 void Date::show()const {
216      cout << getYear() << "-" << getMonth() << "-" << getDay();
217 }
218 
219 
220 
221 #include"account.h"
222 #include<iostream>
223 
224 using namespace std;
225 
226 int main() {
227     Date date(2008, 11, 1);
228     SavingsAccount sa1(date, "S3755217", 0.015);
229     SavingsAccount sa2(date, "02342342", 0.015);
230     CreditAccount ca(date, "C5392394", 10000, 0.0005, 50);
231     
232     sa1.deposit(Date(2008, 11, 5), 5000, "salary");
233     ca.withdraw(Date(2008, 11, 15), 2000, "buy a cell");
234     sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323");
235     
236     ca.settle(Date(2008, 12, 1));
237     
238     ca.deposit(Date(2008, 12, 1), 2016, "repay the credit");
239     sa1.deposit(Date(2008, 12, 5), 5500, "salary");
240     
241         sa1.settle(Date(2009, 1, 1));
242     sa2.settle(Date(2009, 1, 1));
243     ca.settle(Date(2009, 1, 1));
244     
245         cout << endl;
246     sa1.show(); cout << endl;
247     sa2.show(); cout << endl;
248     ca.show(); cout << endl;
249     cout << "Total:" << Account::getTotal() << endl;
250     return 0;
251 }

结果:

 

posted @ 2024-11-23 19:35  孙一发  阅读(2)  评论(0编辑  收藏  举报