实验5 继承和多态
task 3:
#include <iostream> #include <vector> using namespace std; class MachinePets { public: MachinePets(const string s):nickname{s}{} virtual string talk()=0; string get_nickname() { return nickname; } private: string nickname; }; class PetCats:public MachinePets { public: PetCats(const string &s):MachinePets{s},nickname{s}{} string talk()override; private: string nickname; }; string PetCats::talk() { return "mio!"; } class PetDogs:public MachinePets { public: PetDogs(const string &s):MachinePets{s},nickname{s}{} string talk()override; private: string nickname; }; string PetDogs::talk() { return "woof!"; }
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 }
task 4:
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <algorithm> 5 #include <iomanip> 6 7 using namespace std; 8 using std::setw; 9 10 class Film 11 { 12 public: 13 Film() 14 { 15 pyear = -9999; 16 } 17 int get() 18 { 19 return pyear; 20 } 21 friend istream& operator>>(istream& in,Film& f) 22 { 23 cout << "片名:"; 24 in >> f.name; 25 cout << "导演:"; 26 in >> f.director; 27 cout << "制片国家/地区:"; 28 in >> f.parea; 29 cout << "上映年份:"; 30 in >> f.pyear; 31 return in; 32 } 33 friend ostream& operator<<(ostream& out, Film f) 34 { 35 out << left; 36 out << setw(10) << f.name; 37 out << setw(10) << f.director; 38 out << setw(10) << f.parea; 39 out << setw(10) << f.pyear; 40 return out; 41 } 42 private: 43 string name, director, parea; 44 int pyear; 45 46 }; 47 48 bool compare_by_year(Film& a, Film& b) 49 { 50 return a.get() < b.get(); 51 }
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 }
task 5:
1 #include <iostream> 2 3 4 using std::cin; 5 using std::cout; 6 using std::endl; 7 using std::boolalpha; 8 using std::ostream; 9 using std::istream; 10 11 12 template <typename elem> 13 class Complex 14 { 15 public: 16 Complex(elem r=0, elem i=0) :real{ r }, imag{ i }{} 17 elem get_real() { 18 return real; 19 } 20 elem get_imag() { 21 return imag; 22 } 23 Complex(const Complex<elem>& t) { 24 this->real = t.real; 25 this->imag = t.imag; 26 } 27 void operator+=(const Complex<elem>& b) 28 { 29 real += b.real; 30 imag += b.imag; 31 } 32 bool operator==(const Complex<elem>& b)const 33 { 34 return real == b.real && imag == b.imag; 35 } 36 template <typename elem0> 37 friend Complex<elem0> operator+(const Complex<elem0>& a, const Complex<elem0>& b); 38 template <typename elem1> 39 friend istream& operator>>(istream& in, Complex<elem1>& f); 40 template <typename elem2> 41 friend ostream& operator<<(ostream& out, const Complex<elem2>& f); 42 43 private: 44 elem imag, real; 45 }; 46 template <typename elem0> 47 Complex<elem0> operator+(const Complex<elem0>& a, const Complex<elem0>& b) 48 { 49 return Complex<elem0>(a.real + b.real, a.imag + b.imag); 50 } 51 template <typename elem1> 52 ostream& operator<<(ostream& out, const Complex<elem1>& f) 53 { 54 if (f.imag >= 0)out << f.real << " + " << f.imag << "i"; 55 else out << f.real << " - " << -1 * f.imag << "i"; 56 return out; 57 } 58 template <typename elem2> 59 istream& operator>>(istream& in, Complex<elem2>& f) 60 { 61 in >> f.real; 62 in >> f.imag; 63 return in; 64 }
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 }
task 6:
1 #pragma once 2 class Date { 3 private: 4 int year; 5 int month; 6 int day; 7 int totalDays; 8 public: 9 Date(int year, int month, int day); 10 int getYear()const { 11 return year; 12 } 13 int getMonth()const { 14 return month; 15 } 16 int getDay()const { 17 return day; 18 } 19 int getMaxDay()const; 20 bool isLeapYear()const { 21 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 22 } 23 void show()const; 24 int operator-(const Date& date)const { 25 return totalDays - date.totalDays; 26 } 27 };
1 #include"date.h" 2 #include<iostream> 3 #include<cstdlib> 4 5 using namespace std; 6 7 namespace { 8 const int DAYS_BEFIRE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304 ,334,365 }; 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_BEFIRE_MONTH[month - 1] + day; 19 if (isLeapYear() && month > 2) totalDays++; 20 } 21 int Date::getMaxDay()const { 22 if (isLeapYear() && month == 2) 23 return 29; 24 else return DAYS_BEFIRE_MONTH[month] - DAYS_BEFIRE_MONTH[month - 1]; 25 } 26 void Date::show()const { 27 cout << getYear() << "-" << getMonth() << "-" << getDay(); 28 }
1 #pragma once 2 #include"date.h" 3 4 class Accumulator { 5 private: 6 Date lastDate; 7 double value; 8 double sum; 9 public: 10 Accumulator(const Date& date, double value) :lastDate(date), value(value), sum { 0 } {} 11 double getSum(const Date& date) const { 12 return sum + value * (date - lastDate); 13 } 14 void change(const Date& date, double value) { 15 sum = getSum(date); 16 lastDate = date; 17 this->value = value; 18 } 19 void reset(const Date& date, double value) { 20 lastDate = date; 21 this->value; 22 sum = 0; 23 } 24 };
1 #pragma once 2 #include"date.h" 3 #include"accumulator.h" 4 #include<string> 5 6 using namespace std; 7 8 class Account { 9 private: 10 string id; 11 double balance; 12 static double total; 13 protected: 14 Account(const Date& date, const string& id); 15 void record(const Date& date, double amoount, const string& desc); 16 void error(const string& msg) const; 17 public: 18 const string& getId() { 19 return id; 20 } 21 double getBalance()const { 22 return balance; 23 } 24 static double getTotal() { 25 return total; 26 } 27 virtual void deposit(const Date& date, double amount, const string& desc) = 0; 28 virtual void withdraw(const Date& date, double amount, const string& desc) = 0; 29 virtual void settle(const Date& date) = 0; 30 virtual void show()const; 31 }; 32 class SavingsAccount :public Account { 33 private: 34 Accumulator acc; 35 double rate; 36 public: 37 SavingsAccount(const Date& date, const string& id, double rate); 38 double getRate() const { 39 return rate; 40 } 41 void deposit(const Date& date, double amount, const string& desc); 42 void withdraw(const Date& date, double amount, const string& desc); 43 void settle(const Date& date); 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 CreditAccount(const Date& date, const string& id, double credit, double rate, double fee); 57 double getCredit()const { 58 return credit; 59 } 60 double getRate()const { 61 return rate; 62 } 63 double getFee() const { 64 return fee; 65 } 66 double getAvailableCredit()const { 67 if (getBalance() < 0)return credit + getBalance(); 68 else return credit; 69 } 70 void deposit(const Date& date, double amount, const string& desc); 71 void withdraw(const Date& date, double amount, const string& desc); 72 void settle(const Date& date); 73 void show()const; 74 };
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 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) : Account(date, id), rate(rate), acc(date, 0) {} 31 32 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 33 record(date, amount, desc); 34 acc.change(date, getBalance()); 35 } 36 37 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 38 if (amount > getBalance()) { 39 error("not enough money"); 40 } 41 else { 42 record(date, -amount, desc); 43 acc.change(date, getBalance()); 44 } 45 } 46 47 void SavingsAccount::settle(const Date& date) { 48 double interest = acc.getSum(date) * rate / (date - Date(date.getYear() - 1, 1, 1)); 49 if (interest != 0) 50 record(date, interest, "interest"); 51 acc.reset(date, getBalance()); 52 } 53 54 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) {} 55 56 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 57 record(date, amount, desc); 58 acc.change(date, getDebt()); 59 } 60 61 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 62 if (amount - getBalance() > credit) { 63 error("not enouogh credit"); 64 } 65 else { 66 record(date, -amount, desc); 67 acc.change(date, getDebt()); 68 } 69 } 70 71 void CreditAccount::settle(const Date& date) { 72 double interest = acc.getSum(date) * rate; 73 if (interest != 0) 74 record(date, interest, "interest"); 75 if (date.getMonth() == 1) 76 record(date, -fee, "annual fee"); 77 acc.reset(date, getDebt()); 78 } 79 80 void CreditAccount::show() const { 81 Account::show(); 82 cout << "\tAvailable credit: " << getAvailableCredit(); 83 }
1 #include "account.h" 2 #include <iostream> 3 4 using namespace std; 5 6 int main() { 7 Date date(2008, 11, 1); 8 SavingsAccount sa1(date, "S3755217", 0.015); 9 SavingsAccount sa2(date, "02342342", 0.015); 10 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 11 Account* accounts[] = { &sa1, &sa2, &ca }; 12 const int n = sizeof(accounts) / sizeof(Account*); 13 cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl; 14 char cmd; 15 do { 16 date.show(); 17 cout << "\tTotal:" << Account::getTotal() << "\tcommand>"; 18 int index, day; 19 double amount; 20 string desc; 21 cin >> cmd; 22 switch (cmd) { 23 case 'd': 24 cin >> index >> amount; 25 getline(cin, desc); 26 accounts[index]->deposit(date, amount, desc); 27 break; 28 case 'w': 29 cin >> index >> amount; 30 getline(cin, desc); 31 accounts[index]->withdraw(date, amount, desc); 32 break; 33 case 's': 34 for (int i = 0; i < n; i++) { 35 cout << "[" << i << "]"; 36 accounts[i]->show(); 37 cout << endl; 38 } 39 break; 40 case 'c': 41 cin >> day; 42 if (day < date.getDay()) { 43 cout << "You cannot specify a previous day"; 44 } 45 else if (day > date.getMaxDay()) { 46 cout << "Invalid day"; 47 } 48 else { 49 date = Date(date.getYear(), date.getMonth(), day); 50 } 51 break; 52 case 'n': 53 if (date.getMonth() == 12) { 54 date = Date(date.getYear() + 1, 1, 1); 55 } 56 else { 57 date = Date(date.getYear(), date.getMonth() + 1, 1); 58 } 59 for (int i = 0; i < n; i++) { 60 accounts[i]->settle(date); 61 } 62 break; 63 } 64 } while (cmd != 'e'); 65 return 0; 66 }