实验5 继承和多态
实验任务3
pets.hpp
1 #ifndef PETS_HPP 2 #define PETS_HPP 3 #include <string> 4 5 6 class MachinePets { 7 public: 8 MachinePets(const std::string s); 9 virtual std::string talk() = 0; 10 std::string get_nickname() const; 11 12 private: 13 std::string nickname; 14 }; 15 16 class PetCats : public MachinePets { 17 public: 18 PetCats(const std::string &s); 19 std::string talk() override; 20 }; 21 22 class PetDogs : public MachinePets { 23 public: 24 PetDogs(const std::string &s); 25 std::string talk() override; 26 }; 27 28 MachinePets::MachinePets(const std::string s) : nickname(s) {} 29 30 std::string MachinePets::get_nickname() const { 31 return nickname; 32 } 33 34 PetCats::PetCats(const std::string &s) : MachinePets(s) {} 35 36 std::string PetCats::talk() { 37 return "miao wu~"; 38 } 39 40 PetDogs::PetDogs(const std::string &s) : MachinePets(s) {} 41 42 std::string PetDogs::talk() { 43 return "wang wang~"; 44 } 45 46 #endif
task.cpp
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
film.hpp
1 #pragma once 2 #include<iostream> 3 #include<iomanip> 4 5 using namespace std; 6 7 class Film { 8 private: 9 string name, director, area, year; 10 public: 11 Film(const string& name="", const string& director="", const string& area="", const string& year=""); 12 friend ostream& operator<<(ostream& out, const Film& f); 13 friend istream& operator>>(istream& in, Film& f); 14 string get_year()const; 15 }; 16 17 Film::Film(const string& name, const string& director, const string& area, const string& year) { 18 this->name = name; 19 this->director = director; 20 this->area = area; 21 this->year = year; 22 } 23 24 string Film::get_year()const { 25 return year; 26 } 27 28 ostream& operator<<(ostream& out, const Film& f) { 29 out << f.name<< setw(15) << f.director << setw(15) << f.area << setw(15) << f.year << endl; 30 return out; 31 } 32 33 istream& operator>>(istream& in,Film& f) { 34 in >> f.name >> f.director >> f.area >> f.year; 35 return in; 36 } 37 38 bool compare_by_year(const Film& f1, const Film& f2) { 39 return f1.get_year() < f2.get_year(); 40 }
task4.cpp
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 }
运行结果截图:
实验内容5
Complex.hpp
1 #pragma once 2 #include <iostream> 3 4 using namespace std; 5 6 template <typename T> 7 class Complex{ 8 public: 9 Complex(const T& r = 0, const T& i = 0): real(r), imag(i) { } 10 11 T get_real() const{ 12 return real; 13 } 14 15 T get_imag() const{ 16 return imag; 17 } 18 19 Complex operator+=(const Complex &c){ 20 real += c.real; 21 imag += c.imag; 22 return *this; 23 } 24 25 friend bool operator==(const Complex &c1, const Complex &c2){ 26 return c1.real==c2.real&&c1.imag==c2.imag; 27 } 28 29 friend Complex operator +(const Complex &c1,const Complex &c2){ 30 return Complex<T>(c1.real+c2.real,c1.imag+c2.imag); 31 } 32 33 friend istream & operator >>(istream &in, const Complex &c){ 34 in >> c.real >> c.imag; 35 return in; 36 } 37 38 friend ostream & operator <<(ostream &out, const Complex &c){ 39 if(c.imag >= 0) 40 out << c.real << " + " << c.imag << "i"; 41 else 42 out << c.real << " - " << -c.imag << "i"; 43 return out; 44 } 45 46 private: 47 T real; 48 T imag; 49 };
task5.cpp
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 date.h 2 #pragma once 3 class Date { 4 private: 5 int year; 6 int month; 7 int day; 8 int totalDays; 9 public: 10 Date(int year, int month, int day); 11 int getYear()const { return year; } 12 int getMonth()const { return month; } 13 int getDay()const { return day; } 14 int getMaxDay()const; 15 bool isLeapYear()const { 16 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 17 } 18 void show()const; 19 int operator-(const Date& date)const { 20 return totalDays - date.totalDays; 21 } 22 }; 23 24 25 accmulator.h 26 #pragma once 27 #include"date.h" 28 class Accumulator { 29 private: 30 Date lastDate; 31 double value; 32 double sum; 33 public: 34 Accumulator(const Date& date, double value) :lastDate(date), value(value), sum{ 0 } {} 35 double getSum(const Date& date) const{ 36 return sum + value * (date - lastDate); 37 } 38 void change(const Date& date, double value) { 39 sum = getSum(date); 40 lastDate = date; 41 this->value = value; 42 } 43 void reset(const Date& date, double value) { 44 lastDate = date; 45 this->value; 46 sum = 0; 47 } 48 }; 49 50 account.h 51 52 #pragma once 53 #include"date.h" 54 #include"accumulator.h" 55 #include<string> 56 using namespace std; 57 class Account { 58 private: 59 string id; 60 double balance; 61 static double total; 62 protected: 63 Account(const Date& date, const string &id); 64 void record(const Date& date, double amoount, const string& desc); 65 void error(const string& msg) const; 66 public:const string& getId() { return id; } 67 double getBalance()const { return balance; } 68 static double getTotal() { return total; } 69 virtual void deposit(const Date& date, double amount, const string& desc) = 0; 70 virtual void withdraw(const Date& date, double amount, const string& desc) = 0; 71 virtual void settle(const Date& date) = 0; 72 virtual void show()const; 73 }; 74 class SavingsAccount :public Account { 75 private: 76 Accumulator acc; 77 double rate; 78 public: 79 SavingsAccount(const Date& date, const string& id, double rate); 80 double getRate() const { return rate; } 81 void deposit(const Date& date, double amount, const string& desc); 82 void withdraw(const Date& date, double amount, const string& desc); 83 void settle(const Date& date); 84 }; 85 class CreditAccount :public Account { 86 private: 87 Accumulator acc; 88 double credit; 89 double rate; 90 double fee; 91 double getDebt()const { 92 double balance = getBalance(); 93 return(balance < 0 ? balance : 0); 94 } 95 public:CreditAccount(const Date& date, const string& id, double credit, double rate, double fee); 96 double getCredit()const { return credit; } 97 double getRate()const { return rate; } 98 double getFee() const { return fee; } 99 double getAvailableCredit()const { 100 if (getBalance() < 0)return credit + getBalance(); 101 else return credit; 102 } 103 void deposit(const Date& date, double amount, const string& desc); 104 void withdraw(const Date& date, double amount, const string& desc); 105 void settle(const Date& date); 106 void show()const; 107 }; 108 109 110 date.cpp 111 #include"date.h" 112 #include<iostream> 113 #include<cstdlib> 114 using namespace std; 115 namespace { 116 const int DAYS_BEFIRE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304 ,334,365 }; 117 } 118 Date::Date(int year, int month, int day) :year(year), month(month), day(day) { 119 if (day <= 0 || day > getMaxDay()) { 120 cout << "Invalid date: "; 121 show(); 122 cout << endl; 123 exit(1); 124 } 125 int years = year - 1; 126 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFIRE_MONTH[month - 1] + day; 127 if (isLeapYear() && month > 2) totalDays++; 128 } 129 int Date::getMaxDay()const { 130 if (isLeapYear() && month == 2) 131 return 29; 132 else return DAYS_BEFIRE_MONTH[month] - DAYS_BEFIRE_MONTH[month - 1]; 133 } 134 void Date::show()const { 135 cout << getYear() << "-" << getMonth() << "-" << getDay(); 136 } 137 138 139 account.cpp 140 #include "account.h" 141 #include <cmath> 142 #include<iostream> 143 using namespace std; 144 double Account::total = 0; 145 Account::Account(const Date& date, const string& id) :id(id), balance(0) { 146 date.show(); 147 cout << "\t#" << id << "created" << endl; 148 } 149 void Account::record(const Date& date, double amount, const string& desc) { 150 amount = floor(amount * 100 + 0.5) / 100; 151 balance += amount; 152 total += amount; 153 date.show(); 154 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 155 } 156 void Account::show()const { cout << id << "\tBalance:" << balance; } 157 void Account::error(const string& msg)const { 158 cout << "Error(#" << id << "):" << msg << endl; 159 } 160 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) :Account(date, id), rate(rate), acc(date, 0) {} 161 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 162 record(date, amount, desc); 163 acc.change(date, getBalance()); 164 } 165 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 166 if (amount > getBalance()) { 167 error("not enough money"); 168 } 169 else { 170 record(date, -amount, desc); 171 acc.change(date, getBalance()); 172 } 173 } 174 void SavingsAccount::settle(const Date& date) { 175 double interest = acc.getSum(date) * rate / (date-Date(date.getYear() - 1, 1, 1)); 176 if (interest != 0)record(date, interest, "interest"); 177 acc.reset(date, getBalance()); 178 } 179 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) {} 180 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 181 record(date, amount, desc); 182 acc.change(date, getDebt()); 183 } 184 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 185 if (amount - getBalance() > credit) { 186 error("not enouogh credit"); 187 } 188 else { 189 record(date, -amount, desc); 190 acc.change(date, getDebt()); 191 } 192 } 193 void CreditAccount::settle(const Date& date) { 194 double interest = acc.getSum(date) * rate; 195 if (interest != 0) record(date, interest, "interest"); 196 if (date.getMonth() == 1)record(date, -fee, "annual fee"); 197 acc.reset(date, getDebt()); 198 } 199 void CreditAccount::show()const { 200 Account::show(); 201 cout << "\tAvailable credit:" << getAvailableCredit(); 202 } 203 204 task6.cpp 205 #include"account.h" 206 #include<iostream> 207 using namespace std; 208 int main() { 209 Date date(2008, 11, 1); 210 SavingsAccount sa1(date, "S3755217", 0.015); 211 SavingsAccount sa2(date, "02342342", 0.015); 212 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 213 Account* accounts[] = { &sa1,&sa2,&ca }; 214 const int n = sizeof(accounts) / sizeof(Account*); 215 cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl; 216 char cmd; 217 do { 218 date.show(); 219 cout << "\tTotal:" << Account::getTotal() << "\tcommand>"; 220 int index, day; 221 double amount; 222 string desc; 223 cin >> cmd; 224 switch (cmd) { 225 case 'd': 226 cin >> index >> amount; 227 getline(cin, desc); 228 accounts[index]->deposit(date, amount, desc); 229 break; 230 231 case 'w': 232 cin >> index >> amount; 233 getline(cin, desc); 234 accounts[index]->withdraw(date, amount, desc); 235 break; 236 case 's': 237 for (int i = 0; i < n; i++) { 238 cout << "[" << i << "]"; 239 accounts[i]->show(); 240 cout << endl; 241 } 242 break; 243 244 case 'c': 245 cin >> day; 246 if (day < date.getDay()) { 247 cout << "You cannot specify a previous day"; 248 } 249 else if (day > date.getMaxDay()) 250 cout << "Invalid day"; 251 else date = Date(date.getYear(), date.getMonth(), day); 252 break; 253 case 'n': 254 if (date.getMonth() == 12) 255 date = Date(date.getYear() + 1, 1, 1); 256 else date = Date(date.getYear(), date.getMonth() + 1, 1); 257 for (int i = 0; i < n; i++) { 258 accounts[i]->settle(date); 259 } 260 break; 261 } 262 } while (cmd != 'e'); 263 return 0; 264 }
运行结果截图: