实验四 类的组合、继承、模板类、标准库

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

 

 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 }
task2.cpp

 

运行结果截图:

 

问题1: 1、派生类GradeCalc定义中,成绩存储在哪里?

              2、派生类方法sort, min, max, average,  output都要访问成绩,是通过什么接口访问到每个成绩的?               

    3、input方法是通过什么接口实现数据存入对象的?

  答:1、派生类的基类为 vector<int> 其数据存储在由基类 vector<int> 所管理的连续内存空间中。

    2、通过基类 vector<int> 中的迭代器访问数据。

       3、通过 vector<int> 中的 push_back() 接口。

 

问题2:1、代码line68分母的功能是?

     2、去掉乘以1.0代码,重新编译、运行,结果有影响吗?

     3、为什么要乘以1.0?

  答:1、通过 accumulate() 函数进行对所有成绩的简单累计求和。

     2、有影响。

     3、accumulate() 函数返回值为整型,除以 n 得到的也是整型,不符合实际意义,乘以1.0 ,将整型转换为浮点型,最后结果也是浮点型。

 

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

  答:1、对于错误是成绩输入应该有“报错提示”,如果输入的成绩小于0 或者大于 100 ,会对之后的运算结果造成影响。以下就是一种错误输入。

 

 

    2、输入的成绩类型可以直接设置为 double 类型( vector<double>),方便之后运算。

 

 

 

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

 

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 }
task3.cpp

 

运行结果截图:

 

问题1:  1、组合类GradeCalc定义中,成绩存储在哪里?

    2、组合类方法sort, min, max, average,output都要访问成绩,是通过什么访问到每一个成绩的?       

    3、观察与实验任务2在代码写法细节上的差别。

  答:1、储存在组合类GradeCalc 的私有成员 vector<int> grades 中。

    2、通过类中成员 vector 中的迭代器访问数据。

    3、实验任务2是通过构造以 vector 为基类的派生类进行数据操作,实验任务3是直接将 vector 作为类的成员,再通过该成员进行数据操作。

 

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

  答:1、派生类基于已有的类搭建新的类来满足特定的需求,减少了重复开发。

    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 }
task4_1.cpp

 

 

 

 

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

    答:作用:清空输入流缓存区数据。

   如果没有line 35 在第一次输入 Hello World! 后会有一个回车键进入输入流缓存区,之后这个回车键会被赋值给 测试二的 s1,所以 s1 没有输出。

 

 

 

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_2.cpp

出现错误:当输入的字符串多余 n 时会异常终止。如下:

 

 

 

 

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 }
task4_3.cpp

 

 

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

  答:作用与 2 相似,防止输入的回车键对结果产生影响。

 

 

实验任务5:

grm.hpp

 1 #pragma once
 2 
 3 #include<iostream>
 4 
 5 template <typename T>
 6 class GameResourceManager
 7 {
 8 public:
 9     GameResourceManager(T initR) :resource{ initR } {}
10 
11     T get()const { return resource; }
12 
13     void update(T num)
14     {
15         resource += num;
16         if (resource < 0) { resource = 0; }
17     }
18 private:
19     T resource;
20 };
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 }
task5.cpp

 

 

 

实验任务6:

info.hpp

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 #include<iomanip>
 5 
 6 using namespace std;
 7 
 8 class info
 9 {
10 public:
11     info(string nickname,string contact,string city,int n):
12         nickname{nickname},contact{contact},city{city},n{n}{}
13     void display()const
14     {
15         cout << string(30, '-') << endl;
16         cout << left << setw(15) << "昵称:" << nickname << "\n"
17             << setw(15) << "联系方式:" << contact << "\n"
18             << setw(15) << "所在城市:" << city << "\n"
19             << setw(15) << "预定人数:" << n << endl;
20     }
21 
22 
23 private:
24     string nickname;
25     string contact;
26     string city;
27     int n;
28 };
info.cpp

 

task6.cpp

 1 #include"info.hpp"
 2 #include<vector>
 3 #include<iomanip>
 4 
 5 const int capacity = 100;
 6 int main()
 7 {
 8     vector<info> a;
 9     cout << "录入用户预约信息:\n" << endl;
10     int num = 0, last_num = 0;
11     string nickname, contact, city;
12     int n;
13     label:
14     while(cout << "昵称:",cin >> nickname)
15     {
16         cout << "联系方式(手机号/邮箱):";
17         cin >> contact;
18         cout << "所在城市:";
19         cin >> city;
20         cout << "预定参加人数:";
21         cin >> n;
22         last_num = num;
23         num += n;
24         if (num > capacity) { break; }
25         a.push_back(info(nickname, contact, city, n));
26         if (num == capacity) { break; }
27         cout << endl;
28     }
29     if (num > capacity)
30     {
31         cout << "对不起,当前只剩 " << capacity - last_num << " 个位置。" << endl;
32         cout << "1、输入u,更新(update)预定信息。\n"
33             << "2、输入q,退出预定。" << endl;
34         num = last_num;
35         char choice; cin >> choice;
36         if (choice == 'u') { goto label; }
37     }
38     cout << endl;
39     cout << "截至目前共有 " << num << " 位听众预约。预约听众信息如下:" << endl;
40     for (auto& i : a)
41     {
42         i.display();
43     }
44 
45 }
task6.cpp

 

1、人数满时结束

 

2、人数超出时提供选择项

  1、选 u

   2、选 q 

 

3、人数不满时结束

 

 

实验任务7:

 date.h

 1 //date.h
 2 #ifndef __DATE_H__
 3 #define __DATE_H__
 4 class Date {
 5     //日期类
 6 private:
 7     int year;
 8     //
 9     int month;
10     //
11     int day;
12     //
13     int totalDays;
14     //该日期是从公元元年1月1日开始的第几天
15 public:
16     Date(int year, int month, int day);
17     //用年、月、日构造日期
18     int getYear() const { return year; }
19     int getMonth() const { return month; }
20     int getDay() const { return day; }
21     int getMaxDay() const;
22     //获得当月有多少天
23     bool isLeapYear() const {
24         //判断当年是否为闰年
25         return year % 4 == 0 && year % 100!= 0 || year % 400 == 0;
26     }
27     void show() const;
28     //输出当前日期
29     int distance(const Date& date) const {
30         return totalDays - date.totalDays;
31     }
32 };
33 #endif //__DATE_H__
date.h

 

date.cpp

 1 //date.cpp
 2 #include "date.h"
 3 #include <iostream>
 4 #include <cstdlib>
 5 using namespace std;
 6 namespace {
 7     //namespace使下面的定义只在当前文件中有效
 8     const int DAYS_BEFORE_MONTH[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 356};
 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 int Date::getMaxDay() const {
22     if (isLeapYear() && month == 2) return 29;
23     else return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
24 }
25 void Date::show() const {
26     cout << getYear() << "-" << getMonth() << "-" << getDay();
27 }
date.cpp

 

accumulator.h

 1 //accumulator.h
 2 #ifndef _ACCUMULATOR_H_
 3 #define _ACCUMULATOR_H_
 4 #include "date.h"
 5 
 6 class Accumulator {
 7     //将某个数值按日累加
 8 private:
 9     Date lastDate;     //上次变更数值的时期
10     double value;      //数值的当前值
11     double sum;        //数值按日累加之和
12 public:
13     //构造函数,date为开始累加的日期,value为初始值
14     Accumulator(const Date &date, double value)
15         : lastDate(date), value(value), sum(0) {}
16     //获得日期date的累加结果
17     double getSum(const Date &date) const {
18         return sum + value * date.distance(lastDate);
19     }
20     //在date将数值变更为value
21     void change(const Date &date, double value) {
22         sum = getSum(date);
23         lastDate = date;
24         this->value = value;
25     }
26     //初始化,将日期变为date,数值变为value,累加器清零
27     void reset(const Date &date, double value) {
28         lastDate = date;
29         this->value = value;
30         sum = 0;
31     }
32 };
33 #endif //_ACCUMULATOR_H_
accumulator.h

 

account.h

 1 //account.h
 2 #ifndef _ACCOUNT_H_
 3 #define _ACCOUNT_H_
 4 #include "date.h"
 5 #include "accumulator.h"
 6 #include <string>
 7 
 8 class Account {
 9     //账户类
10 private:
11     std::string id;    //账号
12     double balance;               //余额
13     static double total;          //所有账户的总金额
14 protected:
15     //供派生类调用的构造函数,id为账户
16     Account(const Date &date, const std::string &id);
17     //记录一笔账,date为日期,amount为金额,desc为说明
18     void record(const Date &date, double amount, const std::string &desc);
19     //报告错误信息
20     void error(const std::string &msg) const;
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 class SavingsAccount : public Account {   //储蓄账户类
30 private:
31     Accumulator acc;        //辅助计算利息的累加器
32     double rate;            //存款的年利率
33 public:
34     //构造函数
35     SavingsAccount(const Date &date, const std::string &id, double rate);
36     double getRate() const { return rate; }
37     //存入现金
38     void deposit(const Date &date, double amount, const std::string &desc);
39     //取出现金
40     void withdraw(const Date &date, double amount, const std::string &desc);
41     //结算利息,每年1月1日调用一次该函数
42     void settle(const Date &date);
43 };
44 
45 class CreditAccount : public Account {    //信用账户类
46 private:
47     Accumulator acc;        //辅助计算利息的累加器
48     double credit;          //信用额度
49     double rate;            //欠款的日利率
50     double fee;             //信用卡年费
51     double getDebt() const {
52         double balance = getBalance();
53         return (balance < 0? balance : 0);
54     }
55 public:
56     //构造函数
57     CreditAccount(const Date &date, const std::string &id, double credit, double rate, double fee);
58     double getCredit() const { return credit; }
59     double getRate() const { return rate; }
60     double getFee() const { return fee; }
61     double getAvailableCredit() const {
62     if (getBalance() < 0)
63         return credit + getBalance();
64     else
65         return credit;
66 }
67 //存入现金
68 void deposit(const Date &date, double amount, const std::string &desc);
69 //取出现金
70 void withdraw(const Date &date, double amount, const std::string &desc);
71 void settle(const Date &date);
72 //显示账户信息
73 void show() const;
74 };
75 #endif //_ACCOUNT_H_
account.h

 

account.cpp

 1 //account.cpp
 2 #include "account.h"
 3 #include <cmath>
 4 #include <iostream>
 5 using namespace std;
 6 
 7 double Account::total = 0;
 8 
 9 //Account类的实现
10 Account::Account(const Date &date, const 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 //SavingsAccount类相关成员函数的实现
33 SavingsAccount::SavingsAccount(const Date &date, const string &id, double rate)
34     : Account(date, id), rate(rate), acc(date, 0) {}
35 
36 void SavingsAccount::deposit(const Date &date, double amount, const string &desc) {
37     record(date, amount, desc);
38     acc.change(date, getBalance());
39 }
40 void SavingsAccount::withdraw(const Date &date, double amount, const string &desc) {
41     if (amount > getBalance()) {
42         error("not enough money");
43     } else {
44         record(date, -amount, desc);
45         acc.change(date, getBalance());
46     }
47 }
48 
49 void SavingsAccount::settle(const Date &date) {
50     double interest = acc.getSum(date) * rate / date.distance(Date(date.getYear()-1, 1, 1));
51     if (interest!= 0)
52         record(date, interest, "interest");
53     acc.reset(date, getBalance());
54 }
55 
56 //CreditAccount类相关成员函数的实现
57 CreditAccount::CreditAccount(const Date &date, const string &id, double credit, double rate, double fee)
58     : Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) {}
59 
60 void CreditAccount::deposit(const Date &date, double amount, const string &desc) {
61     record(date, amount, desc);
62     acc.change(date, getDebt());
63 }
64 
65 void CreditAccount::withdraw(const Date &date, double amount, const string &desc) {
66     if (amount - getBalance() > credit) {
67         error("not enough credit");
68     } else {
69         record(date, -amount, desc);
70         acc.change(date, getDebt());
71     }
72 }
73 
74 void CreditAccount::settle(const Date &date) {
75     double interest = acc.getSum(date) * rate;
76     if (interest!= 0)
77         record(date, interest, "interest");
78     if (date.getMonth() == 1)
79         record(date, -fee, "annual fee");
80     acc.reset(date, getDebt());
81 }
82 
83 void CreditAccount::show() const {
84     Account::show();
85     cout << "\tAvailable credit: " << getAvailableCredit();
86 }
account.cpp

 

7_10.cpp

 1 //7_10.cpp
 2 #include <iostream>
 3 #include "account.h"
 4 using namespace std;
 5 
 6 int main() {
 7     Date date(2008, 11, 1);    //起始日期
 8 
 9     //建立几个账户
10     SavingsAccount sa1(date, "S3755217", 0.015);
11     SavingsAccount sa2(date, "02342342", 0.015);
12     CreditAccount ca(date, "C5392394", 10000, 0.0005, 50);
13 
14     //11月份的几笔账目
15     sa1.deposit(Date(2008, 11, 5), 5000, "salary");
16     ca.withdraw(Date(2008, 11, 15), 2000, "buy a cell");
17     sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323");
18 
19     //结算信用卡
20     ca.settle(Date(2008, 12, 1));
21 
22     //12月份的几笔账目
23     ca.deposit(Date(2008, 12, 1), 2016, "repay the credit");
24     sa1.deposit(Date(2008, 12, 5), 5500, "salary");
25 
26     //结算所有账户
27     sa1.settle(Date(2009, 1, 1));
28     sa2.settle(Date(2009, 1, 1));
29     ca.settle(Date(2009, 1, 1));
30 
31     //输出各个账户信息
32     cout << endl;
33     sa1.show(); cout << endl;
34     sa2.show(); cout << endl;
35     ca.show(); cout << endl;
36     cout << "Total: " << Account::getTotal() << endl;
37 
38     return 0;
39 }
7_10.cpp

 

 

改进:1、第二版相较于第一版封装性更好,以 Account 类作为基类,SavingsAccount和CreditAccount类继承自Account类,这种结构使得代码更具          可维护性和可扩展性。

   2、第二版增加了更多的功能,如信用账户的利息计算、信用额度管理、年费计算等。例如,CreditAccount类中新增了double credit(信用额         度)、double rate(欠款的日利率)、double fee(信用卡年费)等成员变量,以及相应的计算函数。

   3、 第二版在SavingsAccount和CreditAccount类中都增加了更完善的错误处理机制。例如,SavingsAccount::withdraw函数中会检查取款金           额是否超过账户余额,CreditAccount::withdraw函数中会检查取款金额是否超过信用额度。

问题:1、 第二版虽然采用了面向对象的设计,但类之间的耦合度可能较高。例如,SavingsAccount和CreditAccount类都依赖于Account类和             Accumulator类,对这些基类的修改可能会影响到派生类的功能。

   2、第二版由于功能增多和逻辑复杂,测试的难度和工作量也相应增加。需要编写更多的测试用例来确保各个功能的正确性,特别是信用账户的各            种操作和计算。

 

posted @ 2024-11-24 16:49  小鱼戏小新  阅读(8)  评论(0编辑  收藏  举报