实验4 类的组合、继承、模板类、标准库
1. 实验任务1
验证性实验
2. 实验任务2
GradeCalc.hpp源代码:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <iomanip>
using std::vector;
using std::string;
using std::cin;
using std::cout;
using std::endl;
class GradeCalc : public vector<int> {//继承自std::vector<int>
public:
GradeCalc(const string& cname, int size);
void input(); // 录入成绩
void output() const; // 输出成绩
void sort(bool ascending = false); // 排序 (默认降序)
int min() const; // 返回最低分
int max() const; // 返回最高分
float average() const; // 返回平均分
void info(); // 输出课程成绩信息
private:
void compute(); // 成绩统计
string course_name; // 课程名
int n; // 课程人数
vector<int> counts = vector<int>(5, 0); // 保存各分数段人数([0, 60), [60, 70), [70, 80), [80, 90), [90, 100]
vector<double> rates = vector<double>(5, 0); // 保存各分数段比例
};
GradeCalc::GradeCalc(const string& cname, int size) : course_name{ cname }, n{ size } {}
void GradeCalc::input() {
int grade;
for (int i = 0; i < n; ++i) {
cin >> grade;
this->push_back(grade);
//this是一个指向当前对象的指针;在成员函数内部,使用this可以访问当前对象的成员。
//push_back是std::vector类的成员函数,用于在向量的末尾添加一个元素
//在这里,将成绩grade添加到当前对象所代表的整数向量中
}
}
void GradeCalc::output() const {
for (auto ptr = this->begin(); ptr != this->end(); ++ptr)
cout << *ptr << " ";
cout << endl;
}
void GradeCalc::sort(bool ascending) {
if (ascending)//如果参数ascending为true,则使用默认的升序排序
std::sort(this->begin(), this->end());
else//否则,使用std::greater<int>作为比较函数进行降序排序
std::sort(this->begin(), this->end(), std::greater<int>());
}
int GradeCalc::min() const {
//使用std::min_element算法找到当前对象中的最小元素
return *std::min_element(this->begin(), this->end());
}
int GradeCalc::max() const {
//使用std::max_element算法找到当前对象中的最大元素
return *std::max_element(this->begin(), this->end());
}
float GradeCalc::average() const {
//使用std::accumulate算法对当前对象中的所有元素求和
return std::accumulate(this->begin(), this->end(), 0) * 1.0 / n;
}
void GradeCalc::compute() {
for (int grade : *this) {//在每次循环迭代中,声明一个整数变量grade,用于存储当前遍历到的元素值
if (grade < 60)
counts.at(0)++;
else if (grade >= 60 && grade < 70)
counts.at(1)++;
else if (grade >= 70 && grade < 80)
counts.at(2)++;
else if (grade >= 80 && grade < 90)
counts.at(3)++;
else if (grade >= 90)
counts.at(4)++;
}
for (int i = 0; i < rates.size(); ++i)
rates.at(i) = counts.at(i) * 1.0 / n;
}
void GradeCalc::info() {
cout << "课程名称:\t" << course_name << endl;
cout << "排序后成绩: \t";
sort(); output();
cout << "最高分:\t" << max() << endl;
cout << "最低分:\t" << min() << endl;
cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl;
compute(); // 统计各分数段人数、比例
vector<string> tmp{ "[0, 60) ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]" };
for (int i = tmp.size() - 1; i >= 0; --i)
cout << tmp[i] << "\t: " << counts[i] << "人\t"
<< std::fixed << std::setprecision(2) << rates[i] * 100 << "%" << endl;
}
task2.cpp源代码:
#include "GradeCalc.hpp"
#include <iomanip>
void test() {
int n;
cout << "输入班级人数: ";
cin >> n;
GradeCalc c1("OOP", n);
cout << "录入成绩: " << endl;;
c1.input();
cout << "输出成绩: " << endl;
c1.output();
cout << string(20, '*') + "课程成绩信息" + string(20, '*') << endl;
c1.info();
}
int main() {
test();
}
运行截图:
回答问题:
问题1:派生类GradeCalc定义中,成绩存储在哪里?派生类方法sort, min, max, average, output都要访问成绩,是通过什么接口访问到每个成绩的?input方法是通过什么接口实现数据存入对象的?
答:成绩存储在基类的成员函数中;并通过继承自基类的接口(如min_element
等)访问成绩;input通过继承自基类的push_back
接口实现数据存入对象。
问题2:代码return std::accumulate(this->begin(), this->end(), 0) * 1.0 / n;
分母的功能是?去掉乘以1.0代码,重新编译、运行,结果有影响吗?为什么要乘以1.0?
答:分母n表示共n个数据(成绩),将求和结果除以n,得到平均值;去掉乘以1.0代码,结果会有影响;accumulate
函数中第3个参数0表示从0开始累加,且这里0是整数类型,所以函数会进行整数累加,乘以1.0后,将结果转换为浮点型,除以n后会得到更为精准的结果。(如果将第3个参数改为0.0,删去1.0后,将没有影响)
问题3:从真实应用场景角度考虑,GradeCalc类在设计及代码实现细节上,有哪些地方尚未考虑周全,仍需继续迭代、完善?
答:在真实场景下,成绩不可能都为整数,所以GradeCalc类应需要支持不同类型的数据,如果把GradeCalc类设计为模板类,允许指定成绩类型,会更加灵活。例如:
template <typename T>
class GradeCalc : public std::vector<T> {
public:
//···
private:
//···
};
template <typename T>
GradeCalc<T>::GradeCalc(const std::string& cname, int size) : course_name(cname), n(size) {}
//···
3. 实验任务3
GradeCalc.hpp源代码:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <iomanip>
using std::vector;
using std::string;
using std::cin;
using std::cout;
using std::endl;
class GradeCalc {
public:
GradeCalc(const string& cname, int size);
void input(); // 录入成绩
void output() const; // 输出成绩
void sort(bool ascending = false); // 排序 (默认降序)
int min() const; // 返回最低分
int max() const; // 返回最高分
float average() const; // 返回平均分
void info(); // 输出课程成绩信息
private:
void compute(); // 成绩统计
private:
string course_name; // 课程名
int n; // 课程人数
vector<int> grades; // 课程成绩
vector<int> counts = vector<int>(5, 0); // 保存各分数段人数([0, 60), [60, 70), [70, 80), [80, 90), [90, 100]
vector<double> rates = vector<double>(5, 0); // 保存各分数段比例
};
GradeCalc::GradeCalc(const string& cname, int size) : course_name{ cname }, n{ size } {}
void GradeCalc::input() {
int grade;
for (int i = 0; i < n; ++i) {
cin >> grade;
grades.push_back(grade);
}
}
void GradeCalc::output() const {
for (int grade : grades)
cout << grade << " ";
cout << endl;
}
void GradeCalc::sort(bool ascending) {
if (ascending)
std::sort(grades.begin(), grades.end());
else
std::sort(grades.begin(), grades.end(), std::greater<int>());
}
int GradeCalc::min() const {
return *std::min_element(grades.begin(), grades.end());
}
int GradeCalc::max() const {
return *std::max_element(grades.begin(), grades.end());
}
float GradeCalc::average() const {
return std::accumulate(grades.begin(), grades.end(), 0) * 1.0 / n;
}
void GradeCalc::compute() {
for (int grade : grades) {
if (grade < 60)
counts.at(0)++;
else if (grade >= 60 && grade < 70)
counts.at(1)++;
else if (grade >= 70 && grade < 80)
counts.at(2)++;
else if (grade >= 80 && grade < 90)
counts.at(3)++;
else if (grade >= 90)
counts.at(4)++;
}
for (int i = 0; i < rates.size(); ++i)
rates.at(i) = counts.at(i) * 1.0 / n;
}
void GradeCalc::info() {
cout << "课程名称:\t" << course_name << endl;
cout << "排序后成绩: \t";
sort(); output();
cout << "最高分:\t" << max() << endl;
cout << "最低分:\t" << min() << endl;
cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl;
compute(); // 统计各分数段人数、比例
vector<string> tmp{ "[0, 60) ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]" };
for (int i = tmp.size() - 1; i >= 0; --i)
cout << tmp[i] << "\t: " << counts[i] << "人\t"
<< std::fixed << std::setprecision(2) << rates[i] * 100 << "%" << endl;
}
task3.cpp源代码:
#include "GradeCalc.hpp"
#include <iomanip>
void test() {
int n;
cout << "输入班级人数: ";
cin >> n;
GradeCalc c1("OOP", n);
cout << "录入成绩: " << endl;;
c1.input();
cout << "输出成绩: " << endl;
c1.output();
cout << string(20, '*') + "课程成绩信息" + string(20, '*') << endl;
c1.info();
}
int main() {
test();
}
运行截图:
回答问题:
问题1:组合类GradeCalc定义中,成绩存储在哪里?组合类方法sort, min, max, average, output都要访问成绩,是通过什么访问到每一个成绩的?观察与实验任务2在代码写法细节上的差别。
答:成绩存储在组合类GradeCalc私有的成员函数vector<int> grades;
中;组合类方法通过组合类GradeCalc来访问自身的私有成员函数。
问题2:对比实验任务2和实验任务3,主体代码逻辑(测试代码)没有变更,类GradeCalc的接口也没变,变化的是类GradeCalc的设计及接口内部实现细节。你对面向对象编程有什么新的理解和领悟吗?
答:实验二使用继承,实验三使用组合。通过查找相关资料,了解到了以下信息:
- 继承虽然方便简单,但强耦合关系也有潜在风险(例如,基类接口发生变化,会影响父类的正确性和稳定性);组合是松散的耦合关系,不会受类外部接口的影响;
- 继承时,用户可能会通过父类对象访问到基类的其他公共接口,会破坏类的设计意图;而组合增强了类的封装性,外部用户只能通过类提供的公共接口来操作数据,类内部的数据结构和操作细节对外部是隐藏的,这有助于提高代码的可维护性和安全性;
- 继承的可维护性也不如组合。
4. 实验任务4
task4_1.cpp源代码:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
void test1() {
string s1, s2;
cin >> s1 >> s2; // cin: 从输入流读取字符串, 碰到空白符(空格/回车/Tab)即结束
cout << "s1: " << s1 << endl;
cout << "s2: " << s2 << endl;
}
void test2() {
string s1, s2;
getline(cin, s1); // getline(): 从输入流中提取字符串,直到遇到换行符
getline(cin, s2);
cout << "s1: " << s1 << endl;
cout << "s2: " << s2 << endl;
}
void test3() {
string s1, s2;
getline(cin, s1, ' '); //从输入流中提取字符串,直到遇到指定分隔符
getline(cin, s2);
cout << "s1: " << s1 << endl;
cout << "s2: " << s2 << endl;
}
int main() {
cout << "测试1: 使用标准输入流对象cin输入字符串" << endl;
test1();
cout << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "测试2: 使用函数getline()输入字符串" << endl;
test2();
cout << endl;
cout << "测试3: 使用函数getline()输入字符串, 指定字符串分隔符" << endl;
test3();
}
运行截图:
回答问题:
问题1:去掉cin.ignore(numeric_limits<streamsize>::max(), '\n');
重新编译、运行,给出此时运行结果截图。查阅资料,回答line35在这里的用途是什么?
答:cin.ignore(numeric_limits<streamsize>::max(), '\n');
是 C++ 中用于清除输入缓冲区的代码。
函数原型:istream& ignore( streamsize n = 1, int delim = EOF );
task4_2.cpp源代码:
#include <iostream>
#include <string>
#include <vector>
#include <limits>
using namespace std;
void output(const vector<string>& v) {
for (auto& s : v)
cout << s << endl;
}
void test() {
int n;
while (cout << "Enter n: ", cin >> n) {
vector<string> v1;
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
v1.push_back(s);
}
cout << "output v1: " << endl;
output(v1);
cout << endl;
}
}
int main() {
cout << "测试: 使用cin多组输入字符串" << endl;
test();
}
运行截图:
task4_3.cpp源代码:
#include <iostream>
#include <string>
#include <vector>
#include <limits>
using namespace std;
void output(const vector<string>& v) {
for (auto& s : v)
cout << s << endl;
}
void test() {
int n;
while (cout << "Enter n: ", cin >> n) {
cin.ignore(numeric_limits<streamsize>::max(), '\n');
vector<string> v2;
for (int i = 0; i < n; ++i) {
string s;
getline(cin, s);
v2.push_back(s);
}
cout << "output v2: " << endl;
output(v2);
cout << endl;
}
}
int main() {
cout << "测试: 使用函数getline()多组输入字符串" << endl;
test();
}
运行截图:
回答问题:
问题2:去掉task4_3.cpp的cin.ignore(numeric_limits<streamsize>::max(), '\n');
,重新编译、运行,给出此时运行结果。查阅资料,回答line16在这里的用途是什么?
答:和问题1一样,这里作用是清除回撤。
5. 实验任务5
grm.hpp源代码:
#pragma once
#include <iostream>
template<typename T>
class GameResourceManager {
public:
GameResourceManager(T res):resource(res){}
T get() const{ return resource; }
void update(T num) {
resource += num;
if (resource < 0) resource = 0;
}
private:
T resource;
};
task5.cpp源代码:
#include "grm.hpp"
#include <iostream>
using std::cout;
using std::endl;
void test1() {
GameResourceManager<float> HP_manager(99.99);
cout << "当前生命值: " << HP_manager.get() << endl;
HP_manager.update(9.99);
cout << "增加9.99生命值后, 当前生命值: " << HP_manager.get() << endl;
HP_manager.update(-999.99);
cout << "减少999.99生命值后, 当前生命值: " << HP_manager.get() << endl;
}
void test2() {
GameResourceManager<int> Gold_manager(100);
cout << "当前金币数量: " << Gold_manager.get() << endl;
Gold_manager.update(50);
cout << "增加50个金币后, 当前金币数量: " << Gold_manager.get() << endl;
Gold_manager.update(-99);
cout << "减少99个金币后, 当前金币数量: " << Gold_manager.get() << endl;
}
int main() {
cout << "测试1: 用float类型对类模板GameResourceManager实例化" << endl;
test1();
cout << endl;
cout << "测试2: 用int类型对类模板GameResourceManager实例化" << endl;
test2();
}
运行截图:
6. 实验任务6
info.hpp源代码:
#pragma once
#include <iostream>
#include <iomanip>
class Info {
public:
Info(std::string name, std::string con, std::string cit, int num) :nickname(name), contact(con), city(cit), n(num) {}
void display() const {
std::cout << std::setw(15) << std::left << "昵称:" << nickname << std::endl;
std::cout << std::setw(15) << std::left << "联系方式:" << contact << std::endl;
std::cout << std::setw(15) << std::left << "所在城市:" << city << std::endl;
std::cout << std::setw(15) << std::left << "预定人数:" << n << std::endl;
}
private:
std::string nickname;
std::string contact;
std::string city;
int n;
};
task6.cpp源代码:
#include "Info.hpp"
#include <iostream>
#include <vector>
int main() {
const int capacity = 100;
int capacity_cur = 0;
std::vector<Info> audience_lst;
std::cout << "录入用户预约信息:" << std::endl;
std::cout << std::setw(10) << std::left << "昵称"
<< std::setw(30) << std::left << "联系方式(邮箱/手机号)"
<< std::setw(15) << std::left << "所在城市"
<< std::setw(15) << std::left << "预定参加人数" << std::endl;
int num;
std::string nickname, contact, city;
while (capacity_cur < capacity && std::cin >> nickname >> contact >> city >> num) {
capacity_cur += num;
if (capacity_cur <= capacity) {
audience_lst.push_back(Info(nickname, contact, city, num));
}
else {
capacity_cur -= num;
std::cout << "对不起,只剩" << capacity - capacity_cur << "个位置." << std::endl;
std::cout << "1.输入u,更新(update)预定信息" << std::endl;
std::cout << "2.输入q,退出预定" << std::endl;
std::cout << "你的选择:";
char choice;
std::cin >> choice;
if (choice == 'u') {
std::cout << "请重新输入预定信息:" << std::endl;
std::cout << std::setw(10) << std::left << "昵称"
<< std::setw(30) << std::left << "联系方式(邮箱/手机号)"
<< std::setw(15) << std::left << "所在城市"
<< std::setw(15) << std::left << "预定参加人数" << std::endl;
continue;
}
if (choice == 'q' || capacity_cur == capacity) {
break;
}
}
}
std::cout << "截至目前,一共有" << capacity_cur << "位听众预约。预约听众信息如下:" << std::endl;
std::cout << std::string(40, '-') << std::endl;
for (auto i = 0; i < audience_lst.size(); i++) {
audience_lst[i].display();
std::cout << std::string(40, '-') << std::endl;
}
}
运行截图:
7. 实验任务7
date.h源代码:
#pragma once
#ifndef __DATE_H__
#define __DATE_H__
class Date { //日期类
private:
int year; //年
int month; //月
int day; //日
int totalDays; //该日期是从公元元年1月1日开始的第几天
public:
Date(int year, int month, int day); //用年、月、日构造日期
int getYear() const { return year; }
int getMonth() const { return month; }
int getDay() const { return day; }
int getMaxDay() const; //获得当月有多少天
bool isLeapYear() const { //判断当年是否为闰年
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
void show() const; //输出当前日期
//计算两个日期之间差多少天
int distance(const Date& date) const {
return totalDays - date.totalDays;
}
};
#endif //__DATE_H__
date.cpp源代码:
#include "date.h"
#include <iostream>
#include <cstdlib>
using namespace std;
namespace { //namespace使下面的定义只在当前文件中有效
//存储平年中的某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项
const int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
}
Date::Date(int year, int month, int day) : year(year), month(month), day(day) {
if (day <= 0 || day > getMaxDay()) {
cout << "Invalid date: ";
show();
cout << endl;
exit(1);
}
int years = year - 1;
totalDays = years * 365 + years / 4 - years / 100 + years / 400
+ DAYS_BEFORE_MONTH[month - 1] + day;
if (isLeapYear() && month > 2) totalDays++;
}
int Date::getMaxDay() const {
if (isLeapYear() && month == 2)
return 29;
else
return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
}
void Date::show() const {
cout << getYear() << "-" << getMonth() << "-" << getDay();
}
accumulator.h源代码:
#pragma once
#ifndef __ACCUMULATOR_H__
#define __ACCUMULATOR_H__
#include "date.h"
class Accumulator { //将某个数值按日累加
private:
Date lastDate; //上次变更数值的时期
double value; //数值的当前值
double sum; //数值按日累加之和
public:
//构造函数,date为开始累加的日期,value为初始值
Accumulator(const Date& date, double value)
: lastDate(date), value(value), sum(0) { }
//获得到日期date的累加结果
double getSum(const Date& date) const {
return sum + value * date.distance(lastDate);
}
//在date将数值变更为value
void change(const Date& date, double value) {
sum = getSum(date);
lastDate = date; this->value = value;
}
//初始化,将日期变为date,数值变为value,累加器清零
void reset(const Date& date, double value) {
lastDate = date; this->value = value; sum = 0;
}
};
#endif //__ACCUMULATOR_H__
account.h源代码:
#pragma once
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
#include "date.h"
#include "accumulator.h"
#include <string>
class Account { //账户类
private:
std::string id; //帐号
double balance; //余额
static double total; //所有账户的总金额
protected:
//供派生类调用的构造函数,id为账户
Account(const Date& date, const std::string& id);
//记录一笔帐,date为日期,amount为金额,desc为说明
void record(const Date& date, double amount, const std::string& desc);
//报告错误信息
void error(const std::string& msg) const;
public:
const std::string& getId() const { return id; }
double getBalance() const { return balance; }
static double getTotal() { return total; }
//显示账户信息
void show() const;
};
class SavingsAccount : public Account { //储蓄账户类
private:
Accumulator acc; //辅助计算利息的累加器
double rate; //存款的年利率
public:
//构造函数
SavingsAccount(const Date& date, const std::string& id, double rate);
double getRate() const { return rate; }
//存入现金
void deposit(const Date& date, double amount, const std::string& desc);
//取出现金
void withdraw(const Date& date, double amount, const std::string& desc);
void settle(const Date& date); //结算利息,每年1月1日调用一次该函数
};
class CreditAccount : public Account { //信用账户类
private:
Accumulator acc; //辅助计算利息的累加器
double credit; //信用额度
double rate; //欠款的日利率
double fee; //信用卡年费
double getDebt() const { //获得欠款额
double balance = getBalance();
return (balance < 0 ? balance : 0);
}
public:
//构造函数
CreditAccount(const Date& date, const std::string& id, double credit, double rate, double fee);
double getCredit() const { return credit; }
double getRate() const { return rate; }
double getFee() const { return fee; }
double getAvailableCredit() const { //获得可用信用
if (getBalance() < 0)
return credit + getBalance();
else
return credit;
}
//存入现金
void deposit(const Date& date, double amount, const std::string& desc);
//取出现金
void withdraw(const Date& date, double amount, const std::string& desc);
void settle(const Date& date); //结算利息和年费,每月1日调用一次该函数
void show() const;
};
#endif //__ACCOUNT_H__
account.cpp源代码:
#include "account.h"
#include <cmath>
#include <iostream>
using namespace std;
double Account::total = 0;
//Account类的实现
Account::Account(const Date& date, const string& id)
: id(id), balance(0) {
date.show(); cout << "\t#" << id << " created" << endl;
}
void Account::record(const Date& date, double amount, const string& desc) {
amount = floor(amount * 100 + 0.5) / 100; //保留小数点后两位
balance += amount; total += amount;
date.show();
cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
}
void Account::show() const { cout << id << "\tBalance: " << balance; }
void Account::error(const string& msg) const {
cout << "Error(#" << id << "): " << msg << endl;
}
//SavingsAccount类相关成员函数的实现
SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate)
: Account(date, id), rate(rate), acc(date, 0) { }
void SavingsAccount::deposit(const Date& date, double amount, const string& desc) {
record(date, amount, desc);
acc.change(date, getBalance());
}
void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) {
if (amount > getBalance()) {
error("not enough money");
}
else {
record(date, -amount, desc);
acc.change(date, getBalance());
}
}
void SavingsAccount::settle(const Date& date) {
double interest = acc.getSum(date) * rate //计算年息
/ date.distance(Date(date.getYear() - 1, 1, 1));
if (interest != 0) record(date, interest, "interest");
acc.reset(date, getBalance());
}
//CreditAccount类相关成员函数的实现
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) { }
void CreditAccount::deposit(const Date& date, double amount, const string& desc) {
record(date, amount, desc);
acc.change(date, getDebt());
}
void CreditAccount::withdraw(const Date& date, double amount, const string& desc) {
if (amount - getBalance() > credit) {
error("not enough credit");
}
else {
record(date, -amount, desc);
acc.change(date, getDebt());
}
}
void CreditAccount::settle(const Date& date) {
double interest = acc.getSum(date) * rate;
if (interest != 0) record(date, interest, "interest");
if (date.getMonth() == 1)
record(date, -fee, "annual fee");
acc.reset(date, getDebt());
}
void CreditAccount::show() const {
Account::show();
cout << "\tAvailable credit:" << getAvailableCredit();
}
7_10.cpp源代码:
#include "account.h"
#include <iostream>
using namespace std;
int main() {
Date date(2008, 11, 1);
//建立几个账户
SavingsAccount sa1(date, "S3755217", 0.015);
SavingsAccount sa2(date, "02342342", 0.015);
CreditAccount ca(date, "C5392394", 10000, 0.0005, 50);
//11月份的几笔账目
sa1.deposit(Date(2008, 11, 5), 5000, "salary");
ca.withdraw(Date(2008, 11, 15), 2000, "buy a cell");
sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323");
//结算信用卡
ca.settle(Date(2008, 12, 1));
//12月份的几笔账目
ca.deposit(Date(2008, 12, 1), 2016, "repay the credit");
sa1.deposit(Date(2008, 12, 5), 5500, "salary");
//结算所有账户
sa1.settle(Date(2009, 1, 1));
sa2.settle(Date(2009, 1, 1));
ca.settle(Date(2009, 1, 1));
//输出各个账户信息
cout << endl;
sa1.show(); cout << endl;
sa2.show(); cout << endl;
ca.show(); cout << endl;
cout << "Total: " << Account::getTotal() << endl;
return 0;
}
运行截图:
总结:使用了类的继承的相关用法,定义了派生类,可以使代码更加简洁方便。但是,使用继承后,用户可能会通过父类对象访问到基类的其他公共接口,会破坏类的设计意图;同时,如果后续要修改基类接口,可能会影响父类的正确性和稳定性。
[实验总结]
1、 istream& ignore( streamsize n = 1, int delim = EOF );
函数,用于清除输入缓冲区的代码。
2、了解了继承与组合的区别,以及它们的优点和缺点。