实验5 继承和多态

任务3

源码:

 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <string>
 5 
 6 using std::string;
 7 
 8 
 9 class MachinePets {
10 public:
11     MachinePets(const string &s);           
12 
13 public:
14     virtual string get_nickname() const = 0;                 
15     virtual string talk() const = 0;                
16 
17 protected:
18     string nickname;    
19 };
20 
21 MachinePets::MachinePets(const string &s): nickname{s} {
22 }
23 
24 
25 
26 class PetCats: public MachinePets {
27 public:
28     PetCats(const string &s);  
29 
30 public:
31     string get_nickname() const override;      
32     string talk() const override;         
33 
34 };
35 
36 PetCats::PetCats(const string &s): MachinePets{s} {
37 }
38 
39 string PetCats::get_nickname() const{
40     return nickname;
41 }
42 
43 string PetCats::talk() const {
44     return "miao wu~";
45 }
46 
47 
48 class PetDogs: public MachinePets {
49 public:
50     PetDogs(const string &s);  
51 
52 public:
53     string get_nickname() const override;      
54     string talk() const override;         
55 
56 };
57 
58 PetDogs::PetDogs(const string &s): MachinePets{s} {
59 }
60 
61 string PetDogs::get_nickname() const{
62     return nickname;
63 }
64 
65 string PetDogs::talk() const {
66     return "wang wang~";
67 }
pets.hpp
 1 #include <iostream>
 2 #include <vector>
 3 #include "pets.hpp"
 4 
 5 void test() {
 6     using namespace std;
 7 
 8     vector<MachinePets *> pets;
 9 
10     pets.push_back(new PetCats("miku"));
11     pets.push_back(new PetDogs("da huang"));
12 
13     for(auto &ptr: pets)
14         cout <<ptr->get_nickname() << " says " << ptr->talk() << endl;
15 }
16 
17 int main() {
18     test();
19 }
task3.cpp

运行测试截图:

 

任务4

 源码:

 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <string>
 5 #include <iomanip>
 6 
 7 using std::ostream;
 8 using std::istream;
 9 using std::string;
10 using std::left;
11 using std::setw;
12 using std::cout;
13 
14 class Film {
15 public:
16     Film(){}
17     friend ostream& operator<<(ostream &out, const Film &f);
18     friend istream& operator>>(istream &in, Film &f);
19     int get_year() const;
20     
21 private:
22     string name;
23     string director;
24     string country;
25     int year;
26 };
27 
28 ostream& operator<<(ostream &out, const Film &f) {
29     out << left;
30     out << setw(20) << f.name 
31         << setw(20) << f.director 
32         << setw(20) << f.country 
33         << setw(20) << f.year;
34     
35     return out;
36 }
37 
38 istream& operator>>(istream &in, Film &f) {
39     cout << "录入片名:";
40     in >> f.name;
41     cout << "录入导演:";
42     in >> f.director;
43     cout << "录入制片国家/地区: ";
44     in >> f.country;
45     cout << "录入上映年份:";
46     in >> f.year;
47     
48     return in;
49 }
50 
51 
52 int Film::get_year() const {
53     return year;
54 }
55 
56 bool compare_by_year(const Film& f1, const Film& f2) {
57     return f1.get_year() < f2.get_year();
58 }
film.hpp
 1 #include "film.hpp"
 2 #include <iostream>
 3 #include <string>
 4 #include <vector>
 5 #include <algorithm>
 6 
 7 void test() {
 8     using namespace std;
 9     
10     int n;
11     cout << "输入电影数目: ";
12     cin >> n;
13 
14     cout << "录入" << n << "部影片信息" << endl;
15     vector<Film> film_lst;
16     for(int i = 0; i < n; ++i) {
17         Film f;
18         cout << string(20, '-') << "" << i+1 << "部影片录入" << string(20, '-') << endl;
19         cin >> f;
20         film_lst.push_back(f);
21     }
22 
23     // 按发行年份升序排序
24     sort(film_lst.begin(), film_lst.end(), compare_by_year);
25 
26     cout << string(20, '=') + "电影信息(按发行年份)" +  string(20, '=')<< endl;
27     for(auto &f: film_lst)
28         cout << f << endl;
29 }
30 
31 int main() {
32     test();
33 }
task4.cpp

运行测试截图:

注:

重载运算符operator>>的参数是Film &f,不能加const

 

任务5

源码:

 1 #pragma once
 2 
 3 #include <iostream>
 4 
 5 using std::istream;
 6 using std::ostream;
 7 
 8 template<typename T>
 9 class Complex {
10 public:
11     Complex(T real = 0, T imag = 0) : real(real), imag(imag) {}
12 
13     Complex<T> operator+=(const Complex<T> &c) {
14         real += c.real;
15         imag += c.imag;
16         return *this;
17     }
18 
19     friend Complex<T> operator+(const Complex<T> &c1, const Complex<T> &c2) {
20         return Complex<T>(c1.real + c2.real, c1.imag + c2.imag);
21     }
22     
23     friend bool operator==(const Complex<T> &c1, const Complex<T> &c2) {
24         return (c1.real == c2.real) && (c1.imag == c2.imag);
25     }
26     
27     friend istream& operator>>(istream &in, Complex<T> &c) {
28         in >> c.real >> c.imag;
29         return in;
30     }
31     
32     friend ostream& operator<<(ostream &out, const Complex<T> &c) {
33     if(c.imag >= 0)
34         out << c.real << " + " << c.imag << "i";
35     else
36         out << c.real << " - " << -c.imag << "i";
37     return out;
38 }    
39     
40     T get_real() const {
41         return real;
42     }
43     
44     T get_imag() const{
45         return imag;
46     }
47     
48 private:
49     T real;
50     T imag;
51 };
Complex.hpp
 1 #include "Complex.hpp"
 2 #include <iostream>
 3 
 4 using std::cin;
 5 using std::cout;
 6 using std::endl;
 7 using std::boolalpha;
 8 
 9 void test1() {
10     Complex<int> c1(2, -5), c2(c1);
11 
12     cout << "c1 = " << c1 << endl;
13     cout << "c2 = " << c2 << endl;
14     cout << "c1 + c2 = " << c1 + c2 << endl;
15     
16     c1 += c2;
17     cout << "c1 = " << c1 << endl;
18     cout << boolalpha << (c1 == c2) << endl;
19 }
20 
21 void test2() {
22     Complex<double> c1, c2;
23     cout << "Enter c1 and c2: ";
24     cin >> c1 >> c2;
25     cout << "c1 = " << c1 << endl;
26     cout << "c2 = " << c2 << endl;
27 
28     cout << "c1.real = " << c1.get_real() << endl;
29     cout << "c1.imag = " << c1.get_imag() << endl;
30 }
31 
32 int main() {
33     cout << "自定义类模板Complex测试1: " << endl;
34     test1();
35 
36     cout << endl;
37 
38     cout << "自定义类模板Complex测试2: " << endl;
39     test2();
40 }
task5.cpp

运行测试截图:

 

任务6

源码:

  1 //date.h
  2 #ifndef __DATE_H__
  3 #define __DATE_H__
  4 
  5 class Date {    //日期类
  6 private:
  7     int year;        //
  8     int month;        //
  9     int day;        //
 10     int totalDays;    //该日期是从公元元年1月1日开始的第几天
 11 
 12 public:
 13     Date(int year, int month, int day);    //用年、月、日构造日期
 14     int getYear() const { return year; }
 15     int getMonth() const { return month; }
 16     int getDay() const { return day; }
 17     int getMaxDay() const;        //获得当月有多少天
 18     bool isLeapYear() const {    //判断当年是否为闰年
 19         return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
 20     }
 21     void show() const;            //输出当前日期
 22     //计算两个日期之间差多少天    
 23     int operator - (const Date& date) const {
 24         return totalDays - date.totalDays;
 25     }
 26 };
 27 
 28 #endif //__DATE_H__
 29 
 30 
 31 //date.cpp
 32 #include "date.h"
 33 #include <iostream>
 34 #include <cstdlib>
 35 using namespace std;
 36 
 37 namespace {    //namespace使下面的定义只在当前文件中有效
 38     //存储平年中某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项
 39     const int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
 40 }
 41 
 42 Date::Date(int year, int month, int day) : year(year), month(month), day(day) {
 43     if (day <= 0 || day > getMaxDay()) {
 44         cout << "Invalid date: ";
 45         show();
 46         cout << endl;
 47         exit(1);
 48     }
 49     int years = year - 1;
 50     totalDays = years * 365 + years / 4 - years / 100 + years / 400
 51         + DAYS_BEFORE_MONTH[month - 1] + day;
 52     if (isLeapYear() && month > 2) totalDays++;
 53 }
 54 
 55 int Date::getMaxDay() const {
 56     if (isLeapYear() && month == 2)
 57         return 29;
 58     else
 59         return DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month - 1];
 60 }
 61 
 62 void Date::show() const {
 63     cout << getYear() << "-" << getMonth() << "-" << getDay();
 64 }
 65 
 66 
 67 //accumulator.h
 68 #ifndef __ACCUMULATOR_H__
 69 #define __ACCUMULATOR_H__
 70 #include "date.h"
 71 
 72 class Accumulator {    //将某个数值按日累加
 73 private:
 74     Date lastDate;    //上次变更数值的时期
 75     double value;    //数值的当前值
 76     double sum;        //数值按日累加之和
 77 public:
 78     //构造函数,date为开始累加的日期,value为初始值
 79     Accumulator(const Date &date, double value)
 80         : lastDate(date), value(value), sum(0) { }
 81 
 82     //获得到日期date的累加结果
 83     double getSum(const Date &date) const {
 84         return sum + value * (date - lastDate);
 85     }
 86 
 87     //在date将数值变更为value
 88     void change(const Date &date, double value) {
 89         sum = getSum(date);
 90         lastDate = date;
 91         this->value = value;
 92     }
 93 
 94     //初始化,将日期变为date,数值变为value,累加器清零
 95     void reset(const Date &date, double value) {
 96         lastDate = date;
 97         this->value = value;
 98         sum = 0;
 99     }
100 };
101 
102 #endif //__ACCUMULATOR_H__
103 
104 
105 
106 //account.h
107 #ifndef __ACCOUNT_H__
108 #define __ACCOUNT_H__
109 #include "date.h"
110 #include "accumulator.h"
111 #include <string>
112 
113 class Account { //账户类
114 private:
115     std::string id;    //帐号
116     double balance;    //余额
117     static double total; //所有账户的总金额
118 protected:
119     //供派生类调用的构造函数,id为账户
120     Account(const Date &date, const std::string &id);
121     //记录一笔帐,date为日期,amount为金额,desc为说明
122     void record(const Date &date, double amount, const std::string &desc);
123     //报告错误信息
124     void error(const std::string &msg) const;
125 public:
126     const std::string &getId() const { return id; }
127     double getBalance() const { return balance; }
128     static double getTotal() { return total; }
129     //存入现金,date为日期,amount为金额,desc为款项说明
130     virtual void deposit(const Date &date, double amount, const std::string &desc) = 0;
131     //取出现金,date为日期,amount为金额,desc为款项说明
132     virtual void withdraw(const Date &date, double amount, const std::string &desc) = 0;
133     //结算(计算利息、年费等),每月结算一次,date为结算日期
134     virtual void settle(const Date &date) = 0;
135     //显示账户信息
136     virtual void show() const;
137 };
138 
139 class SavingsAccount : public Account { //储蓄账户类
140 private:
141     Accumulator acc;    //辅助计算利息的累加器
142     double rate;        //存款的年利率
143 public:
144     //构造函数
145     SavingsAccount(const Date &date, const std::string &id, double rate);
146     double getRate() const { return rate; }
147     virtual void deposit(const Date &date, double amount, const std::string &desc);
148     virtual void withdraw(const Date &date, double amount, const std::string &desc);
149     virtual void settle(const Date &date);
150 };
151 
152 class CreditAccount : public Account { //信用账户类
153 private:
154     Accumulator acc;    //辅助计算利息的累加器
155     double credit;        //信用额度
156     double rate;        //欠款的日利率
157     double fee;            //信用卡年费
158 
159     double getDebt() const {    //获得欠款额
160         double balance = getBalance();
161         return (balance < 0 ? balance : 0);
162     }
163 public:
164     //构造函数
165     CreditAccount(const Date &date, const std::string &id, double credit, double rate, double fee);
166     double getCredit() const { return credit; }
167     double getRate() const { return rate; }
168     double getFee() const { return fee; }
169     double getAvailableCredit() const {    //获得可用信用
170         if (getBalance() < 0) 
171             return credit + getBalance();
172         else
173             return credit;
174     }
175     virtual void deposit(const Date &date, double amount, const std::string &desc);
176     virtual void withdraw(const Date &date, double amount, const std::string &desc);
177     virtual void settle(const Date &date);
178     virtual void show() const;
179 };
180 
181 #endif //__ACCOUNT_H__
182 
183 
184 
185 //account.cpp
186 #include "account.h"
187 #include <cmath>
188 #include <iostream>
189 using namespace std;
190 
191 double Account::total = 0;
192 
193 //Account类的实现
194 Account::Account(const Date &date, const string &id)
195     : id(id), balance(0) {
196     date.show();
197     cout << "\t#" << id << " created" << endl;
198 }
199 
200 void Account::record(const Date &date, double amount, const string &desc) {
201     amount = floor(amount * 100 + 0.5) / 100;    //保留小数点后两位
202     balance += amount;
203     total += amount;
204     date.show();
205     cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
206 }
207 
208 void Account::show() const {
209     cout << id << "\tBalance: " << balance;
210 }
211 
212 void Account::error(const string &msg) const {
213     cout << "Error(#" << id << "): " << msg << endl;
214 }
215 
216 //SavingsAccount类相关成员函数的实现
217 SavingsAccount::SavingsAccount(const Date &date, const string &id, double rate)
218     : Account(date, id), rate(rate), acc(date, 0) { }
219 
220 void SavingsAccount::deposit(const Date &date, double amount, const string &desc) {
221     record(date, amount, desc);
222     acc.change(date, getBalance());
223 }
224 
225 void SavingsAccount::withdraw(const Date &date, double amount, const string &desc) {
226     if (amount > getBalance()) {
227         error("not enough money");
228     } else {
229         record(date, -amount, desc);
230         acc.change(date, getBalance());
231     }
232 }
233 
234 void SavingsAccount::settle(const Date &date) {
235     if (date.getMonth() == 1) {    //每年的一月计算一次利息
236         double interest = acc.getSum(date) * rate
237             / (date - Date(date.getYear() - 1, 1, 1));
238         if (interest != 0)
239             record(date, interest, "interest");
240         acc.reset(date, getBalance());
241     }
242 }
243 
244 //CreditAccount类相关成员函数的实现
245 CreditAccount::CreditAccount(const Date& date, const string& id, double credit, double rate, double fee)
246     : Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) { }
247 
248 void CreditAccount::deposit(const Date &date, double amount, const string &desc) {
249     record(date, amount, desc);
250     acc.change(date, getDebt());
251 }
252 
253 void CreditAccount::withdraw(const Date &date, double amount, const string &desc) {
254     if (amount - getBalance() > credit) {
255         error("not enough credit");
256     } else {
257         record(date, -amount, desc);
258         acc.change(date, getDebt());
259     }
260 }
261 
262 void CreditAccount::settle(const Date &date) {
263     double interest = acc.getSum(date) * rate;
264     if (interest != 0)
265         record(date, interest, "interest");
266     if (date.getMonth() == 1)
267         record(date, -fee, "annual fee");
268     acc.reset(date, getDebt());
269 }
270 
271 void CreditAccount::show() const {
272     Account::show();
273     cout << "\tAvailable credit:" << getAvailableCredit();
274 }
275 
276 
277 
278 //8_8.cpp
279 #include "account.h"
280 #include <iostream>
281 using namespace std;
282 
283 int main() {
284     Date date(2008, 11, 1);    //起始日期
285     //建立几个账户
286     SavingsAccount sa1(date, "S3755217", 0.015);
287     SavingsAccount sa2(date, "02342342", 0.015);
288     CreditAccount ca(date, "C5392394", 10000, 0.0005, 50);
289     Account *accounts[] = { &sa1, &sa2, &ca };
290     const int n = sizeof(accounts) / sizeof(Account*);    //账户总数
291 
292     cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl;
293     char cmd;
294     do {
295         //显示日期和总金额
296         date.show();
297         cout << "\tTotal: " << Account::getTotal() << "\tcommand> ";
298 
299         int index, day;
300         double amount;
301         string desc;
302 
303         cin >> cmd;
304         switch (cmd) {
305         case 'd':    //存入现金
306             cin >> index >> amount;
307             getline(cin, desc);
308             accounts[index]->deposit(date, amount, desc);
309             break;
310         case 'w':    //取出现金
311             cin >> index >> amount;
312             getline(cin, desc);
313             accounts[index]->withdraw(date, amount, desc);
314             break;
315         case 's':    //查询各账户信息
316             for (int i = 0; i < n; i++) {
317                 cout << "[" << i << "] ";
318                 accounts[i]->show();
319                 cout << endl;
320             }
321             break;
322         case 'c':    //改变日期
323             cin >> day;
324             if (day < date.getDay())
325                 cout << "You cannot specify a previous day";
326             else if (day > date.getMaxDay())
327                 cout << "Invalid day";
328             else
329                 date = Date(date.getYear(), date.getMonth(), day);
330             break;
331         case 'n':    //进入下个月
332             if (date.getMonth() == 12)
333                 date = Date(date.getYear() + 1, 1, 1);
334             else
335                 date = Date(date.getYear(), date.getMonth() + 1, 1);
336             for (int i = 0; i < n; i++)
337                 accounts[i]->settle(date);
338             break;
339         }
340     } while (cmd != 'e');
341     return 0;
342 }
task6

运行测试截图:

 改进:

1.将show函数声明为虚函数,可以通过创建一个Account指针类型的数组,使每个元素分别指向各个账户对象,通过循环控制指针,调用show函数,展示账户信息

2.deposit,withdraw,settle函数均为纯虚函数,Account类为抽象类,通过运行时多态通过基类指针可以调用派生类的对应函数

不足:

1.用户无法动态添加新的账户

 

posted @ 2024-12-02 19:59  吴婧希  阅读(9)  评论(0编辑  收藏  举报