实验5 继承和多态
实验任务1:
验证性实验,自行练习,无需写入实验博客文档。
练习、理解此实验任务后,参照这个任务,完成实验任务3。
实验任务2:
验证性实验,自行练习,无需写入实验博客文档。
练习、理解此实验任务后,参照这个任务,完成实验任务4。
实验任务3:
1 #pragma once 2 #include <iostream> 3 #include <string> 4 using std::string; 5 6 // 机器宠物类 MachinePets 7 class MachinePets { 8 public: 9 // 带参数的构造函数 10 MachinePets(const string& s) : nickname(s) {} 11 12 // 纯虚函数,为机器宠物派生类提供宠物叫声的统一接口 13 virtual string talk() const = 0; 14 15 // 获取机器宠物的昵称 16 string get_nickname() const { return nickname; } 17 18 protected: 19 string nickname; // 昵称 20 }; 21 22 // 宠物猫类 PetCats 23 class PetCats : public MachinePets { 24 public: 25 // 带参数的构造函数 26 PetCats(const string& s) : MachinePets(s) {} 27 28 // 返回电子宠物猫叫声 29 string talk() const override { 30 return "miao wu~"; // 猫叫声 31 } 32 }; 33 34 // 宠物狗类 PetDogs 35 class PetDogs : public MachinePets { 36 public: 37 // 带参数的构造函数 38 PetDogs(const string& s) : MachinePets(s) {} 39 40 // 返回电子宠物狗叫声 41 string talk() const override { 42 return "wang wang~"; // 狗叫声 43 } 44 };
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 }
运行结果截图:
实验任务4:
1 #pragma once 2 #include <iostream> 3 #include <string> 4 #include<iomanip> 5 6 7 using std::setw; 8 using std::ostream; 9 using std::istream; 10 using std::string; 11 12 class Film { 13 public: 14 // 构造函数 15 Film(const std::string& title = "", const std::string& director = "", 16 const std::string& country = "", int year = 0) 17 : title_(title), director_(director), country_(country), year_(year) {} 18 19 // 重载输入运算符>> 20 friend std::istream& operator>>(std::istream& is, Film& film) { 21 // 正确的输入提示和变量读取方式 22 std::cout << "录入片名:"; 23 is >> film.title_; 24 std::cout << "录入导演:"; 25 is >> film.director_; 26 std::cout << "录入制片国家/地区:"; 27 is >> film.country_; 28 std::cout << "录入上映年份:"; 29 is >> film.year_; 30 return is; 31 } 32 33 // 重载输出运算符<< 34 friend std::ostream& operator<<(std::ostream& os, const Film& film) { 35 os << film.title_ 36 << setw(8) << film.director_ 37 << setw(8) << film.country_ 38 << setw(8) << film.year_; 39 return os; 40 } 41 42 // 按发行年份升序排序的辅助函数 43 static bool compare_by_year(const Film& a, const Film& b) { 44 return a.year_ < b.year_; 45 } 46 47 private: 48 std::string title_; // 片名 49 std::string director_; // 导演 50 std::string country_; // 制片国家/地区 51 int year_; // 上映年份 52 };
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 using std::setw; 10 11 int n; 12 cout << "输入电影数目: "; 13 cin >> n; 14 15 cout << "录入" << n << "部影片信息" << endl; 16 vector<Film> film_lst; 17 for(int i = 0; i < n; ++i) { 18 Film f; 19 cout << string(20, '-') << "第" << i+1 << "部影片录入" << string(20, '-') << endl; 20 cin >> f; 21 film_lst.push_back(f); 22 } 23 24 // 按发行年份升序排序 25 sort(film_lst.begin(), film_lst.end(), Film::compare_by_year); 26 27 cout << string(20, '=') + "电影信息(按发行年份)" + string(20, '=')<< endl; 28 for(auto &f: film_lst) 29 cout << f << endl; 30 } 31 32 int main() { 33 test(); 34 }
运行结果截图:
实验任务5:
1 #ifndef COMPLEX_HPP 2 #define COMPLEX_HPP 3 4 #include <istream> 5 #include <ostream> 6 7 template<typename T> 8 class Complex { 9 private: 10 T real; // 实部 11 T imag; // 虚部 12 13 public: 14 // 构造函数 15 Complex(T r = 0, T i = 0) : real(r), imag(i) {} 16 17 // 访问器 18 T get_real() const { return real; } 19 T get_imag() const { return imag; } 20 21 // 设置器 22 void set_real(T r) { real = r; } 23 void set_imag(T i) { imag = i; } 24 25 // 重载+=运算符 26 Complex& operator+=(const Complex& rhs) { 27 real += rhs.real; 28 imag += rhs.imag; 29 return *this; 30 } 31 32 // 重载+运算符 33 Complex operator+(const Complex& rhs) const { 34 return Complex(real + rhs.real, imag + rhs.imag); 35 } 36 37 // 重载==运算符 38 bool operator==(const Complex& rhs) const { 39 return (real == rhs.real) && (imag == rhs.imag); 40 } 41 42 // 友元函数,重载输入运算符 43 friend std::istream& operator>>(std::istream& in, Complex& c) { 44 in >> c.real >> c.imag; 45 if (!in) { 46 // 处理错误,例如设置异常或错误标志 47 } 48 return in; 49 } 50 51 // 友元函数,重载输出运算符 52 friend std::ostream& operator<<(std::ostream& out, const Complex& c) { 53 if(c.imag>0){ 54 out << c.real << "+" << c.imag << "i";} 55 else if(c.imag<0){ 56 out<<c.real << c.imag << "i";} 57 else{ 58 out<<c.real; 59 } 60 return out; 61 } 62 }; 63 64 #endif // 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 }
运行结果截图:
试验任务6:
1 #pragma once 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__
1 //date.cpp 2 #include "date.h" 3 #include <iostream> 4 #include <cstdlib> 5 using namespace std; 6 7 namespace { //namespace使下面的定义只在当前文件中有效 8 //存储平年中某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项 9 const int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; 10 } 11 12 Date::Date(int year, int month, int day) : year(year), month(month), day(day) { 13 if (day <= 0 || day > getMaxDay()) { 14 cout << "Invalid date: "; 15 show(); 16 cout << endl; 17 exit(1); 18 } 19 int years = year - 1; 20 totalDays = years * 365 + years / 4 - years / 100 + years / 400 21 + DAYS_BEFORE_MONTH[month - 1] + day; 22 if (isLeapYear() && month > 2) totalDays++; 23 } 24 25 int Date::getMaxDay() const { 26 if (isLeapYear() && month == 2) 27 return 29; 28 else 29 return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1]; 30 } 31 32 void Date::show() const { 33 cout << getYear() << "-" << getMonth() << "-" << getDay(); 34 }
1 #pragma once 2 #ifndef __ACCUMULATOR_H__ 3 #define __ACCUMULATOR_H__ 4 #include "date.h" 5 6 class Accumulator { //将某个数值按日累加 7 private: 8 Date lastDate; //上次变更数值的时期 9 double value; //数值的当前值 10 double sum; //数值按日累加之和 11 public: 12 //构造函数,date为开始累加的日期,value为初始值 13 Accumulator(const Date& date, double value) 14 : lastDate(date), value(value), sum(0) { } 15 16 //获得到日期date的累加结果 17 double getSum(const Date& date) const { 18 return sum + value * (date - lastDate); 19 } 20 21 //在date将数值变更为value 22 void change(const Date& date, double value) { 23 sum = getSum(date); 24 lastDate = date; 25 this->value = value; 26 } 27 28 //初始化,将日期变为date,数值变为value,累加器清零 29 void reset(const Date& date, double value) { 30 lastDate = date; 31 this->value = value; 32 sum = 0; 33 } 34 }; 35 36 #endif //__ACCUMULATOR_H__
1 #pragma once 2 #ifndef __ACCOUNT_H__ 3 #define __ACCOUNT_H__ 4 #include "date.h" 5 #include "accumulator.h" 6 #include <string> 7 8 class Account { //账户类 9 private: 10 std::string id; //帐号 11 double balance; //余额 12 static double total; //所有账户的总金额 13 protected: 14 //供派生类调用的构造函数,id为账户 15 Account(const Date& date, const std::string& id); 16 //记录一笔帐,date为日期,amount为金额,desc为说明 17 void record(const Date& date, double amount, const std::string& desc); 18 //报告错误信息 19 void error(const std::string& msg) const; 20 public: 21 const std::string& getId() const { return id; } 22 double getBalance() const { return balance; } 23 static double getTotal() { return total; } 24 //存入现金,date为日期,amount为金额,desc为款项说明 25 virtual void deposit(const Date& date, double amount, const std::string& desc) = 0; 26 //取出现金,date为日期,amount为金额,desc为款项说明 27 virtual void withdraw(const Date& date, double amount, const std::string& desc) = 0; 28 //结算(计算利息、年费等),每月结算一次,date为结算日期 29 virtual void settle(const Date& date) = 0; 30 //显示账户信息 31 virtual void show() const; 32 }; 33 34 class SavingsAccount : public Account { //储蓄账户类 35 private: 36 Accumulator acc; //辅助计算利息的累加器 37 double rate; //存款的年利率 38 public: 39 //构造函数 40 SavingsAccount(const Date& date, const std::string& id, double rate); 41 double getRate() const { return rate; } 42 virtual void deposit(const Date& date, double amount, const std::string& desc); 43 virtual void withdraw(const Date& date, double amount, const std::string& desc); 44 virtual void settle(const Date& date); 45 }; 46 47 class CreditAccount : public Account { //信用账户类 48 private: 49 Accumulator acc; //辅助计算利息的累加器 50 double credit; //信用额度 51 double rate; //欠款的日利率 52 double fee; //信用卡年费 53 54 double getDebt() const { //获得欠款额 55 double balance = getBalance(); 56 return (balance < 0 ? balance : 0); 57 } 58 public: 59 //构造函数 60 CreditAccount(const Date& date, const std::string& id, double credit, double rate, double fee); 61 double getCredit() const { return credit; } 62 double getRate() const { return rate; } 63 double getFee() const { return fee; } 64 double getAvailableCredit() const { //获得可用信用 65 if (getBalance() < 0) 66 return credit + getBalance(); 67 else 68 return credit; 69 } 70 virtual void deposit(const Date& date, double amount, const std::string& desc); 71 virtual void withdraw(const Date& date, double amount, const std::string& desc); 72 virtual void settle(const Date& date); 73 virtual void show() const; 74 }; 75 76 #endif //__ACCOUNT_H__
1 #include "account.h" 2 #include <cmath> 3 #include <iostream> 4 using namespace std; 5 6 double Account::total = 0; 7 8 //Account类的实现 9 Account::Account(const Date& date, const string& id) 10 : id(id), balance(0) { 11 date.show(); 12 cout << "\t#" << id << " created" << endl; 13 } 14 15 void Account::record(const Date& date, double amount, const string& desc) { 16 amount = floor(amount * 100 + 0.5) / 100; //保留小数点后两位 17 balance += amount; 18 total += amount; 19 date.show(); 20 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 21 } 22 23 void Account::show() const { 24 cout << id << "\tBalance: " << balance; 25 } 26 27 void Account::error(const string& msg) const { 28 cout << "Error(#" << id << "): " << msg << endl; 29 } 30 31 //SavingsAccount类相关成员函数的实现 32 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) 33 : Account(date, id), rate(rate), acc(date, 0) { } 34 35 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 36 record(date, amount, desc); 37 acc.change(date, getBalance()); 38 } 39 40 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 41 if (amount > getBalance()) { 42 error("not enough money"); 43 } 44 else { 45 record(date, -amount, desc); 46 acc.change(date, getBalance()); 47 } 48 } 49 50 void SavingsAccount::settle(const Date& date) { 51 if (date.getMonth() == 1) { //每年的一月计算一次利息 52 double interest = acc.getSum(date) * rate 53 / (date - Date(date.getYear() - 1, 1, 1)); 54 if (interest != 0) 55 record(date, interest, "interest"); 56 acc.reset(date, getBalance()); 57 } 58 } 59 60 //CreditAccount类相关成员函数的实现 61 CreditAccount::CreditAccount(const Date& date, const string& id, double credit, double rate, double fee) 62 : Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) { } 63 64 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 65 record(date, amount, desc); 66 acc.change(date, getDebt()); 67 } 68 69 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 70 if (amount - getBalance() > credit) { 71 error("not enough credit"); 72 } 73 else { 74 record(date, -amount, desc); 75 acc.change(date, getDebt()); 76 } 77 } 78 79 void CreditAccount::settle(const Date& date) { 80 double interest = acc.getSum(date) * rate; 81 if (interest != 0) 82 record(date, interest, "interest"); 83 if (date.getMonth() == 1) 84 record(date, -fee, "annual fee"); 85 acc.reset(date, getDebt()); 86 } 87 88 void CreditAccount::show() const { 89 Account::show(); 90 cout << "\tAvailable credit:" << getAvailableCredit(); 91 }
1 //8_8.cpp 2 #include "account.h" 3 #include <iostream> 4 using namespace std; 5 6 int main() { 7 Date date(2008, 11, 1); //起始日期 8 //建立几个账户 9 SavingsAccount sa1(date, "S3755217", 0.015); 10 SavingsAccount sa2(date, "02342342", 0.015); 11 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 12 Account* accounts[] = { &sa1, &sa2, &ca }; 13 const int n = sizeof(accounts) / sizeof(Account*); //账户总数 14 15 cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl; 16 char cmd; 17 do { 18 //显示日期和总金额 19 date.show(); 20 cout << "\tTotal: " << Account::getTotal() << "\tcommand> "; 21 22 int index, day; 23 double amount; 24 string desc; 25 26 cin >> cmd; 27 switch (cmd) { 28 case 'd': //存入现金 29 cin >> index >> amount; 30 getline(cin, desc); 31 accounts[index]->deposit(date, amount, desc); 32 break; 33 case 'w': //取出现金 34 cin >> index >> amount; 35 getline(cin, desc); 36 accounts[index]->withdraw(date, amount, desc); 37 break; 38 case 's': //查询各账户信息 39 for (int i = 0; i < n; i++) { 40 cout << "[" << i << "] "; 41 accounts[i]->show(); 42 cout << endl; 43 } 44 break; 45 case 'c': //改变日期 46 cin >> day; 47 if (day < date.getDay()) 48 cout << "You cannot specify a previous day"; 49 else if (day > date.getMaxDay()) 50 cout << "Invalid day"; 51 else 52 date = Date(date.getYear(), date.getMonth(), day); 53 break; 54 case 'n': //进入下个月 55 if (date.getMonth() == 12) 56 date = Date(date.getYear() + 1, 1, 1); 57 else 58 date = Date(date.getYear(), date.getMonth() + 1, 1); 59 for (int i = 0; i < n; i++) 60 accounts[i]->settle(date); 61 break; 62 } 63 } while (cmd != 'e'); 64 return 0; 65 }
运行结果截图:
答:改进:可以通过基类的指针来执行各种操作,因而各种类型的账户对象都可以通过一个基类指针的数组来访问,提供大便利,使我们可以通过统一的方式来操作各个账户。