• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
nuist0415
博客园    首页    新随笔    联系   管理    订阅  订阅
实验4 类的组合、继承、模板类、标准库

Task2

GradeClac.cpp

  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 }
View Code

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 }
View Code

result2

问题1:派生类GradeCalc定义中,成绩存储在哪里?派生类方法sort, min, max, average,
output都要访问成绩,是通过什么接口访问到每个成绩的?input方法是通过什么接口实现数
据存入对象的?

答:成绩储存在基类vector<int>中。派生类通过this指针指向的迭代器来访问基类成员数据。input方法通过push_back方法来实现存入数据。


问题2:代码line68分母的功能是?去掉乘以1.0代码,重新编译、运行,结果有影响吗?为什
么要乘以1.0?

答:分子将数据累加,除以分母n得到平均值。如果不乘1.0就不会进行类型转换,会做整除,得不到精确的平均值。


问题3:从真实应用场景角度考虑,GradeCalc类在设计及代码实现细节上,有哪些地方尚未
考虑周全,仍需继续迭代、完善?

用is-a关系比较复杂,可以改为用has-a关系。这样更加清晰一点。input函数里面可以对输入数据再进行一下边界性检查。

Task3

GradeClac.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 }
View Code

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 }
View Code

result3

问题1:组合类GradeCalc定义中,成绩存储在哪里?组合类方法sort, min, max, average,
output都要访问成绩,是通过什么访问到每一个成绩的?观察与实验任务2在代码写法细节上
的差别。

答:成绩储存在私有成员grades中。直接通过类内部访问私有成员来获得成绩。


问题2:对比实验任务2和实验任务3,主体代码逻辑(测试代码)没有变更,类GradeCalc的
接口也没变,变化的是类GradeCalc的设计及接口内部实现细节。你对面向对象编程有什么新
的理解和领悟吗?

答:接口没有变更的话,就不会影响到共同合作的其他写代码的人。这样既有利于代码的优化,同时还利于团队协作。

 Task4

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 }
View Code

result4_1

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

答:清空buffer中的多余字符,直到遇到换行符('\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 }
View Code

result4_2

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 
16     while (cout << "Enter n: ", cin >> n) {
17         cin.ignore(numeric_limits<streamsize>::max(), '\n');
18 
19         vector<string> v2;
20 
21         for (int i = 0; i < n; ++i) {
22             string s;
23             getline(cin, s);
24             v2.push_back(s);
25         }
26         cout << "output v2: " << endl;
27         output(v2);
28         cout << endl;
29     }
30 }
31 
32 int main() {
33     cout << "测试: 使用函数getline()多组输入字符串" << endl;
34     test();
35 }
View Code

result4_3

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

用来清除掉缓冲区中之前输入留下来的换行符,以保证接下来的输入能正常进行。

 Task5

grm.hpp

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

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 }
View Code

result5

Task6

info.hpp

 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <iomanip>
 5 
 6 using std::string;
 7 using std::cout;
 8 using std::endl;
 9 using std::setw;
10 using std::left;
11 
12 class info {
13 public:
14     info(string nickname0, string contact0, string city0, int n0);
15 
16     void display() const;
17 
18 private:
19     string nickname;
20     string contact;
21     string city;
22     int n;
23 
24 public:
25     static int total_now;
26 };
27 
28 int info::total_now = 0;
29 
30 info::info(string nickname0, string contact0, string city0, int n0) : nickname{ nickname0 }, contact{ contact0 }, city{ city0 }, n{ n0 } {
31     total_now += n;
32 }
33 
34 void info::display() const {
35     cout << left << setw(15) << "昵称: " << nickname << endl;
36     cout << left << setw(15) << "联系方式: " << contact << endl;
37     cout << left << setw(15) << "所在城市: " << city << endl;
38     cout << left << setw(15) << "预定人数: " << n << endl;
39     cout << string(30, '*') << endl;
40 }

task6.cpp

 1 #include "info.hpp"
 2 #include <iostream>
 3 #include <vector>
 4 
 5 using namespace std;
 6 
 7 const int capacity = 100;
 8 
 9 void print(vector<info> audience_lst) {
10     cout << string(30, '*') << endl;
11     for (const auto& i : audience_lst)
12         i.display();
13 }
14 
15 void menu() {
16     cout << "开始录入用户预约信息 ";
17     cout << "昵称" << "\t联系方式(邮箱/手机号)" << "\t所在城市" << "\t预定参加人数" << endl;
18 }
19 
20 void input(vector<info>& audience_lst) {
21     string temp_name, temp_contact, temp_city;
22     int temp_n;
23 
24     while (info::total_now < capacity) {
25         cout << "录入第" << audience_lst.size() + 1 << "位用户信息: ";
26         cin >> temp_name >> temp_contact >> temp_city >> temp_n;
27 
28         if ((info::total_now + temp_n) > capacity) {
29 
30             cout << "对不起,现在只剩" << capacity - info::total_now << "个位置" << endl;
31             cout << "1.输入u,更新(update)预定信息" << endl;
32             cout << "2.输入q,退出预定录入系统" << endl;
33 
34             cout << "你的选择: ";
35             string choice; cin >> choice;
36             if (choice == "q")
37                 break;
38             else {
39                 cout << "请重新输入预定信息: " << endl;
40                 cin >> temp_name >> temp_contact >> temp_city >> temp_n;
41                 while ((info::total_now + temp_n) > capacity) {
42                     cout << "重新输入的信息依旧无法录入,请再次录入: " << endl;
43                     cin >> temp_name >> temp_contact >> temp_city >> temp_n;
44                 }
45                 audience_lst.push_back(info(temp_name, temp_contact, temp_city, temp_n));
46             }
47 
48         }
49         else {
50             audience_lst.push_back(info(temp_name, temp_contact, temp_city, temp_n));
51         }
52     }
53 }
54 
55 int main() {
56     vector<info> audience_lst;
57 
58     menu();
59     input(audience_lst);
60     print(audience_lst);
61 
62     return 0;
63 }

result6

 Task7

date.h

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

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 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     
19     int years = year - 1;
20     totalDays = years * 365 + years / 4 - years / 100 + years / 400 + 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 return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
28 }
29 
30 void Date::show()const {
31     cout << getYear() << "-" << getMonth() << "-" << getDay();
32 }
View Code

accumulator.h

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

account.h

 1 #pragma once
 2 
 3 #ifndef __ACCOUNT_H__
 4 #define __ACCOUNT_H__
 5 
 6 #include"date.h"
 7 #include"accumulator.h"
 8 #include<string>
 9 
10 class Account {
11 private:
12     std::string id;
13     double balance;
14     static double total;
15 
16 protected:
17     Account(const Date& date, const std::string& id);
18     void record(const Date& date, double amount, const std::string& desc);
19     void error(const std::string& msg)const;
20 
21 public:
22     const std::string& getId()const { return id; }
23     double getBalance()const { return balance; }
24     static double getTotal() { return total; }
25 
26     void show()const;
27 };
28 
29 
30 class SavingsAccount :public Account {
31 private:
32     Accumulator acc;
33     double rate;
34 
35 public:
36     SavingsAccount(const Date& date, const std::string& id, double rate);
37     double getRate()const { return rate; }
38 
39     void deposit(const Date& date, double amount, const std::string& desc);
40     void withdraw(const Date& date, double amount, const std::string& desc);
41     void settle(const Date& date);
42 };
43 
44 
45 class CreditAccount :public Account {
46 private:
47     Accumulator acc;
48     double credit;
49     double rate;
50     double fee;
51 
52     double getDebt()const {
53         double balance = getBalance();
54         return (balance < 0 ? balance : 0);
55     }
56 
57 public:
58     CreditAccount(const Date& date, const std::string& id, double credit, double rate, double fee);
59 
60     double getCredit()const { return credit; }
61     double getRate()const { return rate; }
62     double getAvailableCredit()const {
63         if (getBalance() < 0)
64             return credit + getBalance();
65         else
66             return credit;
67     }
68 
69     void deposit(const Date& date, double amount, const std::string& desc);
70     void withdraw(const Date& date, double amount, const std::string& desc);
71     void settle(const Date& date);
72     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 double Account::total = 0;
 6 
 7 Account::Account(const Date& date, const string& id) :id{ id }, balance{ 0 } {
 8     date.show(); cout << "\t#" << id << "created" << endl;
 9 }
10 
11 
12 void Account::record(const Date& date, double amount, const string& desc) {
13     amount = floor(amount * 100 + 0.5) / 100;
14     balance += amount;
15     total += amount;
16     date.show();
17     cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
18 }
19 
20 void Account::show()const { cout << id << "\tBalance:" << balance; }
21 void Account::error(const string& msg)const {
22     cout << "Error(#" << id << "):" << msg << endl;
23 }
24 
25 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) :Account(date, id), rate(rate), acc(date, 0) {}
26 
27 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) {
28     record(date, amount, desc);
29     acc.change(date, getBalance());
30 }
31 
32 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) {
33     if (amount > getBalance()) {
34         error("not enough money");
35     }
36     else {
37         record(date, -amount, desc);
38         acc.change(date, getBalance());
39     }
40 }
41 
42 void SavingsAccount::settle(const Date& date) {
43     double interest = acc.getSum(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1));
44     if (interest != 0)record(date, interest, "interest");
45     acc.reset(date, getBalance());
46 }
47 
48 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) {}
49 
50 void CreditAccount::deposit(const Date& date, double amount, const string& desc) {
51     record(date, amount, desc);
52     acc.change(date, getDebt());
53 }
54 
55 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) {
56     if (amount - getBalance() > credit) {
57         error("not enough credit");
58     }
59     else {
60         record(date, -amount, desc);
61         acc.change(date, getDebt());
62     }
63 }
64 
65 void CreditAccount::settle(const Date& date) {
66     double interest = acc.getSum(date) * rate;
67     if (interest != 0)record(date, interest, "interest");
68     if (date.getMonth() == 1)
69         record(date, -fee, "annual fee");
70     acc.reset(date, getDebt());
71 }
72 
73 void CreditAccount::show()const {
74     Account::show();
75     cout << "\tAvailable credit:" << getAvailableCredit();
76 }
View Code

result7

用了is-a关系,从基类Account派生出两个新的类CreditAccount和SavingsAccount。

这样通过接口继承,可以减少代码的重复。

posted on 2024-11-23 18:25  Haruto  阅读(29)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3