实验5 继承和多态
一、实验目的
正确使用C++语法重载运算符,理解编译器是如何将表达式转换为对运算符重载的调用的
基于问题场景,合理使用派生机制,虚函数,纯虚函数,抽象类,实现接口继承与运行时多态
能灵活应用类的组合继承,多态编程解决实际问题
二、实验准备
第7章 类的继承
第8章 多态
9.1节 类模板
三、实验内容
3. 实验任务3
代码:
hpp文件:
1 #ifndef PETS_HPP 2 #define PETS_HPP 3 #include <string> 4 using namespace std; 5 6 7 class MachinePets{ 8 private: 9 string nickname; 10 public: 11 MachinePets(const string &s); 12 string get_nickname() const { return nickname; } 13 virtual string talk()=0 ; 14 15 16 virtual ~MachinePets() = default; // 添加虚析构函数 17 18 }; 19 // MachinePets构造函数实现 20 MachinePets::MachinePets(const string &s) : nickname(s) {} 21 22 23 class PetCats:public MachinePets{ 24 public: 25 PetCats(const string &s); 26 string talk() override; 27 }; 28 // PetCats构造函数实现 29 PetCats::PetCats(const string &s) : MachinePets(s) {} 30 string PetCats::talk() { 31 return "miao wu~"; 32 } 33 34 class PetDogs:public MachinePets { 35 public: 36 PetDogs(const string &s); 37 string talk() override; 38 }; 39 40 41 // PetDogs构造函数实现 42 PetDogs::PetDogs(const string &s) : MachinePets(s) {} 43 string PetDogs::talk() { 44 return "wang wang~"; 45 } 46 47 #endif
task文件:
1 #include <iostream> 2 #include <vector> 3 #include "petts.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. 实验任务4
代码:
头文件:
1 #ifndef FILM_HPP 2 #define FILM_HPP 3 4 #include <iostream> 5 #include <string> 6 7 class Film { 8 private: 9 std::string filmname; 10 std::string director; 11 std::string area; 12 int year; 13 14 public: 15 // 默认构造函数 16 Film() = default; 17 18 // 带参数的构造函数 19 Film(const std::string& name, const std::string& dir, const std::string& area, int year) 20 : filmname(name), director(dir), area(area), year(year) {} 21 22 // 输入流重载 23 friend std::istream& operator>>(std::istream& is, Film& f); 24 25 // 输出流重载 26 friend std::ostream& operator<<(std::ostream& os, const Film& f); 27 28 // 获取年份用于比较 29 int getYear() const { return year; } 30 }; 31 32 // 输入流重载实现 33 std::istream& operator>>(std::istream& is, Film& f) { 34 std::cout << "录入片名:"; 35 is >> f.filmname; 36 std::cout << "录入导演:"; 37 is >> f.director; 38 std::cout << "录入制片国家/地区:"; 39 is >> f.area; 40 std::cout << "录入上映年份:"; 41 is >> f.year; // 读取年份 42 return is; 43 } 44 45 // 输出流重载实现 46 std::ostream& operator<<(std::ostream& os, const Film& f) { 47 os << f.filmname << "\t" << f.director << "\t" << f.area << "\t" << f.year; 48 return os; 49 } 50 51 #endif 52 53
task文件:
1 #include "petts.hpp" 2 #include <iostream> 3 #include <string> 4 #include <vector> 5 #include <algorithm> 6 //增加这一部分 7 bool compare_by_year(const Film& f1, const Film& f2) { 8 return f1.getYear() < f2.getYear(); // 按年份升序比较 9 } 10 11 void test() { 12 using namespace std; 13 14 int n; 15 cout << "输入电影数目: "; 16 cin >> n; 17 18 cout << "录入" << n << "部影片信息" << endl; 19 vector<Film> film_lst; 20 for(int i = 0; i < n; ++i) { 21 Film f; 22 cout << string(20, '-') << "第" << i+1 << "部影片录入" << string(20, '-') << endl; 23 cin >> f; 24 film_lst.push_back(f); 25 } 26 27 // 按发行年份升序排序 28 sort(film_lst.begin(), film_lst.end(), compare_by_year); 29 30 cout << string(20, '=') + "电影信息(按发行年份)" + string(20, '=')<< endl; 31 for(auto &f: film_lst) 32 cout << f << endl; 33 } 34 35 int main() { 36 test(); 37 }
运行截图:(换一组)

5. 实验任务5
代码:
头文件:
1 #ifndef COMPLEX_HPP 2 #define COMPLEX_HPP 3 4 #include <iostream> 5 #include <string> 6 7 using namespace std; 8 template <typename T> 9 10 class Complex { 11 private: 12 T real; 13 T imag; 14 public: 15 Complex():real(T()),imag(T()){ 16 }//默认构造函数 17 18 //有参构造函数 19 Complex(T r,T i):real(r),imag(i){ 20 } 21 T get_real() const {return real ;} 22 23 T get_imag() const{return imag;} 24 25 26 Complex<T>& operator += (const Complex <T> & other){ 27 real+=other.real; 28 imag+=other.imag; 29 return *this; 30 } 31 bool operator ==(const Complex <T>& other )const{ 32 return (real==other.real)&&(imag==other.imag);} 33 34 Complex<T> operator +(const Complex<T>& other)const{ 35 return Complex<T>(real+other.real,imag+other.imag);} 36 37 friend istream& operator>>(istream& is,Complex<T> &c){ 38 T r, i; 39 is >> r >> i; // 读取实部和虚部 40 c.real = r; 41 c.imag = i; 42 return is; 43 } 44 friend ostream& operator <<(ostream& os,const Complex<T>&c){ 45 os<<c.real; 46 if(c.imag>=0){ 47 os<<"+"; 48 49 } 50 os<<c.imag<<"i"; 51 return os; 52 } 53 }; 54 55 56 #endif 57 58
task文件:
1 #include "petts.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. 实验任务6
代码:
date.h,
1 #pragma once 2 3 class Date { 4 private: 5 int year; 6 int month; 7 int day; 8 int totalDays; 9 10 public: 11 Date(int year, int month, int day); 12 13 int getYear() const { return year; } 14 int getMonth() const { return month; } 15 int getDay() const { return day; } 16 int getMaxDay() const; 17 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 };
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 int years = year - 1; 19 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day; 20 if (isLeapYear() && month > 2) totalDays++; 21 } 22 23 int Date::getMaxDay() const { 24 if (isLeapYear() && month == 2) 25 return 29; 26 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 }
accumulator.h,
1 #pragma once 2 #include "date.h" 3 4 class Accumulator { 5 private: 6 Date lastDate; 7 double value; 8 double sum; 9 10 public: 11 Accumulator(const Date& date, double value) : lastDate(date), value(value), sum{ 0 } {} 12 13 double getSum(const Date& date) const { 14 return sum + value * (date - lastDate); 15 } 16 17 void change(const Date& date, double value) { 18 sum = getSum(date); 19 lastDate = date; 20 this->value = value; 21 } 22 23 void reset(const Date& date, double value) { 24 lastDate = date; 25 this->value = value; 26 sum = 0; 27 } 28 };
account.h,
1 #pragma once 2 3 #include "date.h" 4 #include "accumulator.h" 5 #include <string> 6 7 using namespace std; 8 9 class Account { 10 private: 11 string id; 12 double balance; 13 static double total; 14 15 protected: 16 Account(const Date& date, const string& id); 17 18 void record(const Date& date, double amount, const string& desc); 19 void error(const string& msg) const; 20 21 public: 22 const string& getId() { return id; } 23 double getBalance() const { return balance; } 24 static double getTotal() { return total; } 25 26 virtual void deposit(const Date& date, double amount, const string& desc) = 0; 27 virtual void withdraw(const Date& date, double amount, const string& desc) = 0; 28 virtual void settle(const Date& date) = 0; 29 30 virtual void show() const; 31 }; 32 33 34 class SavingsAccount : public Account { 35 private: 36 Accumulator acc; 37 double rate; 38 39 public: 40 SavingsAccount(const Date& date, const string& id, double rate); 41 42 double getRate() const { return rate; } 43 44 void deposit(const Date& date, double amount, const string& desc); 45 void withdraw(const Date& date, double amount, const string& desc); 46 void settle(const Date& date); 47 }; 48 49 50 class CreditAccount : public Account { 51 private: 52 Accumulator acc; 53 double credit; 54 double rate; 55 double fee; 56 57 double getDebt() const { 58 double balance = getBalance(); 59 return (balance < 0 ? balance : 0); 60 } 61 62 public: 63 CreditAccount(const Date& date, const string& id, double credit, double rate, double fee); 64 65 double getCredit() const { return credit; } 66 double getRate() const { return rate; } 67 double getFee() const { return fee; } 68 double getAvailableCredit() const { 69 if (getBalance() < 0) return credit + getBalance(); 70 else return credit; 71 } 72 73 void deposit(const Date& date, double amount, const string& desc); 74 void withdraw(const Date& date, double amount, const string& desc); 75 void settle(const Date& date); 76 void show() const; 77 };
account.cpp,
1 #include "account.h" 2 #include <cmath> 3 #include<iostream> 4 5 using namespace std; 6 7 double Account::total = 0; 8 9 Account::Account(const Date& date, const string& id) : id(id), balance(0) { 10 date.show(); 11 cout << "\t#" << id << " created" << endl; 12 } 13 14 void Account::record(const Date& date, double amount, const string& desc) { 15 amount = floor(amount * 100 + 0.5) / 100; 16 balance += amount; 17 total += amount; 18 date.show(); 19 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 20 } 21 22 void Account::show() const { 23 cout << id << "\tBalance: " << balance; 24 } 25 26 void Account::error(const string& msg) const { 27 cout << "Error (#" << id << "): " << msg << endl; 28 } 29 30 31 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) 32 : Account(date, id), rate(rate), acc(date, 0) {} 33 34 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 35 record(date, amount, desc); 36 acc.change(date, getBalance()); 37 } 38 39 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 40 if (amount > getBalance()) { 41 error("not enough money"); 42 } 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 - Date(date.getYear() - 1, 1, 1)); 51 if (interest != 0) record(date, interest, "interest"); 52 acc.reset(date, getBalance()); 53 } 54 55 56 CreditAccount::CreditAccount(const Date& date, const string& id, double credit, double rate, double fee) 57 : Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) {} 58 59 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 60 record(date, amount, desc); 61 acc.change(date, getDebt()); 62 } 63 64 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 65 if (amount - getBalance() > credit) { 66 error("not enough credit"); 67 } 68 69 else { 70 record(date, -amount, desc); 71 acc.change(date, getDebt()); 72 } 73 } 74 75 void CreditAccount::settle(const Date& date) { 76 double interest = acc.getSum(date) * rate; 77 if (interest != 0) record(date, interest, "interest"); 78 if (date.getMonth() == 1) record(date, -fee, "annual fee"); 79 acc.reset(date, getDebt()); 80 } 81 82 void CreditAccount::show() const { 83 Account::show(); 84 cout << "\tAvailable credit: " << getAvailableCredit(); 85 }
8_8.cpp源码
1 #include "account.h" 2 #include<iostream> 3 using namespace std; 4 5 int main() { 6 Date date(2008, 11, 1); 7 8 SavingsAccount sa1(date, "S3755217", 0.015); 9 SavingsAccount sa2(date, "02342342", 0.015); 10 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 11 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 date.show(); 19 cout << "\tTotal:" << Account::getTotal() << "\tcommand>"; 20 21 int index, day; 22 double amount; 23 string desc; 24 cin >> cmd; 25 26 switch (cmd) { 27 case 'd': 28 cin >> index >> amount; 29 getline(cin, desc); 30 accounts[index]->deposit(date, amount, desc); 31 break; 32 33 case 'w': 34 cin >> index >> amount; 35 getline(cin, desc); 36 accounts[index]->withdraw(date, amount, desc); 37 break; 38 39 case 's': 40 for (int i = 0; i < n; i++) { 41 cout << "[" << i << "]"; 42 accounts[i]->show(); 43 cout << endl; 44 } 45 break; 46 47 case 'c': 48 cin >> day; 49 if (day < date.getDay()) { 50 cout << "You cannot specify a previous day"; 51 } 52 else if (day > date.getMaxDay()) 53 cout << "Invalid day"; 54 else date = Date(date.getYear(), date.getMonth(), day); 55 break; 56 57 case 'n': 58 if (date.getMonth() == 12) 59 date = Date(date.getYear() + 1, 1, 1); 60 else date = Date(date.getYear(), date.getMonth() + 1, 1); 61 for (int i = 0; i < n; i++) { 62 accounts[i]->settle(date); 63 } 64 break; 65 } 66 67 } while (cmd != 'e'); 68 69 return 0; 70 }
运行截图:

用文字总结: 本章第7章例7-10,这一版本做了哪些改进? 这一版本迭代后,还有哪些问题有待解决/完善
加入了抽象类,利用了类的多态实现了代码复用。建立一个数组,各种对象都可以用数组的一个基类指针来访问,提高运行效率,使代码更为简洁。

浙公网安备 33010602011771号