实验5 继承和多态
实验任务3
实验代码:
1 #pragma once 2 #include <iostream> 3 #include <string> 4 5 class MachinePets { 6 public: 7 MachinePets(const std::string& s); 8 std::string get_nickname() const; 9 virtual std::string talk() = 0; 10 11 protected: 12 std::string nickname; 13 }; 14 15 class PetCats : public MachinePets { 16 public: 17 PetCats(const std::string& s); 18 std::string talk() ; 19 }; 20 21 class PetDogs : public MachinePets { 22 public: 23 PetDogs(const std::string& s); 24 std::string talk() ; 25 }; 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 35 PetCats::PetCats(const std::string& s) : MachinePets(s) {} 36 37 std::string PetCats::talk() { 38 return "miao wu~"; 39 } 40 41 42 PetDogs::PetDogs(const std::string& s) : MachinePets(s) {} 43 44 std::string PetDogs::talk() { 45 return "wang wang~"; 46 }
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 <vector> 5 #include <algorithm> 6 7 class Film { 8 public: 9 friend std::istream& operator>>(std::istream& in, Film& f); 10 friend std::ostream& operator<<(std::ostream& out, const Film& f); 11 bool operator<(const Film& other) const; 12 13 private: 14 std::string title; 15 std::string director; 16 std::string country; 17 int year; 18 }; 19 20 21 22 23 std::istream& operator>>(std::istream& in, Film& f) { 24 std::cout << "录入片名: "; 25 in >> f.title; 26 std::cout << "录入导演: "; 27 in >> f.director; 28 std::cout << "录入制片国家/地区: "; 29 in >> f.country; 30 std::cout << "录入上映年份: "; 31 in >> f.year; 32 return in; 33 } 34 35 std::ostream& operator<<(std::ostream& out, const Film& f) { 36 out << f.title << " " << f.director << " " << f.country << " " << f.year; 37 return out; 38 } 39 40 bool Film::operator<(const Film& other) const { 41 return year < other.year; 42 } 43 44 45 bool compare_by_year(const Film& f1, const Film& f2) { 46 return f1< f2; 47 }
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:
实验代码:
1 #ifndef COMPLEX_HPP 2 #define COMPLEX_HPP 3 4 #include <iostream> 5 #include <string> 6 7 8 template <typename T> 9 class Complex { 10 public: 11 12 Complex(T real = 0, T imag = 0) : real(real), imag(imag) {} 13 14 15 Complex& operator+=(const Complex& other) { 16 real += other.real; 17 imag += other.imag; 18 return *this; 19 } 20 21 Complex operator+(const Complex& other) const { 22 return Complex(real + other.real, imag + other.imag); 23 } 24 25 bool operator==(const Complex& other) const { 26 return real == other.real && imag == other.imag; 27 } 28 29 30 friend std::istream& operator>>(std::istream& in, Complex& c) { 31 in >> c.real >> c.imag; 32 return in; 33 } 34 35 friend std::ostream& operator<<(std::ostream& out, const Complex& c) { 36 out << c.real_<< (c.imag_ >= 0 ? "+" : "") << c.imag << "i"; 37 return out; 38 } 39 40 41 T get_real() const { return real; } 42 T get_imag() const { return imag; } 43 44 private: 45 T real; 46 T imag; 47 }; 48 49 #endif
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 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 { return year; } 11 int getMonth()const { return month; } 12 int getDay()const { return day; } 13 int getMaxDay()const; 14 bool isLeapYear()const { 15 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 16 } 17 void show()const; 18 int operator-(const Date& date)const { 19 return totalDays - date.totalDays; 20 } 21 };
1 #include<iostream> 2 #include"date.h" 3 #include<cstdlib> 4 using namespace std; 5 namespace { 6 const int DAYS_BEFIRE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304 ,334,365 }; 7 } 8 Date::Date(int year, int month, int day) :year(year), month(month), day(day) { 9 if (day <= 0 || day > getMaxDay()) { 10 cout << "Invalid date: "; 11 show(); 12 cout << endl; 13 exit(1); 14 } 15 int years = year - 1; 16 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFIRE_MONTH[month - 1] + day; 17 if (isLeapYear() && month > 2) totalDays++; 18 } 19 int Date::getMaxDay()const { 20 if (isLeapYear() && month == 2) 21 return 29; 22 else return DAYS_BEFIRE_MONTH[month] - DAYS_BEFIRE_MONTH[month - 1]; 23 } 24 void Date::show()const { 25 cout << getYear() << "-" << getMonth() << "-" << getDay(); 26 }
#pragma once #include"date.h" #include"accumulator.h" #include<string> using namespace std; class Account { private: string id; double balance; static double total; protected: Account(const Date& date, const string& id); void record(const Date& date, double amoount, const string& desc); void error(const string& msg) const; public:const string& getId() { return id; } double getBalance()const { return balance; } static double getTotal() { return total; } virtual void deposit(const Date& date, double amount, const string& desc) = 0; virtual void withdraw(const Date& date, double amount, const string& desc) = 0; virtual void settle(const Date& date) = 0; virtual void show()const; }; class SavingsAccount :public Account { private: Accumulator acc; double rate; public: SavingsAccount(const Date& date, const string& id, double rate); double getRate() const { return rate; } void deposit(const Date& date, double amount, const string& desc); void withdraw(const Date& date, double amount, const string& desc); void settle(const Date& date); }; class CreditAccount :public Account { private: Accumulator acc; double credit; double rate; double fee; double getDebt()const { double balance = getBalance(); return(balance < 0 ? balance : 0); } public:CreditAccount(const Date& date, const string& id, double credit, double rate, double fee); double getCredit()const { return credit; } double getRate()const { return rate; } double getFee() const { return fee; } double getAvailableCredit()const { if (getBalance() < 0)return credit + getBalance(); else return credit; } void deposit(const Date& date, double amount, const string& desc); void withdraw(const Date& date, double amount, const string& desc); void settle(const Date& date); void show()const; };
1 #include "account.h" 2 #include <cmath> 3 #include<iostream> 4 using namespace std; 5 double Account::total = 0; 6 Account::Account(const Date& date, const string& id) :id(id), balance(0) { 7 date.show(); 8 cout << "\t#" << id << "created" << endl; 9 } 10 void Account::record(const Date& date, double amount, const string& desc) { 11 amount = floor(amount * 100 + 0.5) / 100; 12 balance += amount; 13 total += amount; 14 date.show(); 15 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 16 } 17 void Account::show()const { cout << id << "\tBalance:" << balance; } 18 void Account::error(const string& msg)const { 19 cout << "Error(#" << id << "):" << msg << endl; 20 } 21 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) :Account(date, id), rate(rate), acc(date, 0) {} 22 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 23 record(date, amount, desc); 24 acc.change(date, getBalance()); 25 } 26 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 27 if (amount > getBalance()) { 28 error("not enough money"); 29 } 30 else { 31 record(date, -amount, desc); 32 acc.change(date, getBalance()); 33 } 34 } 35 void SavingsAccount::settle(const Date& date) { 36 double interest = acc.getSum(date) * rate / (date - Date(date.getYear() - 1, 1, 1)); 37 if (interest != 0)record(date, interest, "interest"); 38 acc.reset(date, getBalance()); 39 } 40 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) {} 41 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 42 record(date, amount, desc); 43 acc.change(date, getDebt()); 44 } 45 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 46 if (amount - getBalance() > credit) { 47 error("not enouogh credit"); 48 } 49 else { 50 record(date, -amount, desc); 51 acc.change(date, getDebt()); 52 } 53 } 54 void CreditAccount::settle(const Date& date) { 55 double interest = acc.getSum(date) * rate; 56 if (interest != 0) record(date, interest, "interest"); 57 if (date.getMonth() == 1)record(date, -fee, "annual fee"); 58 acc.reset(date, getDebt()); 59 } 60 void CreditAccount::show()const { 61 Account::show(); 62 cout << "\tAvailable credit:" << getAvailableCredit(); 63 }
1 #pragma once 2 #include"date.h" 3 class Accumulator { 4 private: 5 Date lastDate; 6 double value; 7 double sum; 8 public: 9 Accumulator(const Date& date, double value) :lastDate(date), value(value), sum{ 0 } {} 10 double getSum(const Date& date) const { 11 return sum + value * (date - lastDate); 12 } 13 void change(const Date& date, double value) { 14 sum = getSum(date); 15 lastDate = date; 16 this->value = value; 17 } 18 void reset(const Date& date, double value) { 19 lastDate = date; 20 this->value; 21 sum = 0; 22 } 23 };
1 #include"account.h" 2 #include<iostream> 3 using namespace std; 4 5 int main() { 6 Date date(2008, 11, 1); 7 SavingsAccount sa1(date, "S3755217", 0.015); 8 SavingsAccount sa2(date, "02342342", 0.015); 9 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 10 Account* accounts[] = { &sa1,&sa2,&ca }; 11 const int n = sizeof(accounts) / sizeof(Account*); 12 cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl; 13 char cmd; 14 do { 15 date.show(); 16 cout << "\tTotal:" << Account::getTotal() << "\tcommand>"; 17 int index, day; 18 double amount; 19 string desc; 20 cin >> cmd; 21 switch (cmd) { 22 case 'd': 23 cin >> index >> amount; 24 getline(cin, desc); 25 accounts[index]->deposit(date, amount, desc); 26 break; 27 28 case 'w': 29 cin >> index >> amount; 30 getline(cin, desc); 31 accounts[index]->withdraw(date, amount, desc); 32 break; 33 34 case 's': 35 for (int i = 0; i < n; i++) { 36 cout << "[" << i << "]"; 37 accounts[i]->show(); 38 cout << endl; 39 } 40 break; 41 42 case 'c': 43 cin >> day; 44 if (day < date.getDay()) { 45 cout << "You cannot specify a previous day"; 46 } 47 else if (day > date.getMaxDay()) 48 cout << "Invalid day"; 49 else date = Date(date.getYear(), date.getMonth(), day); 50 break; 51 52 case 'n': 53 if (date.getMonth() == 12) 54 date = Date(date.getYear() + 1, 1, 1); 55 else date = Date(date.getYear(), date.getMonth() + 1, 1); 56 for (int i = 0; i < n; i++) { 57 accounts[i]->settle(date); 58 } 59 break; 60 } 61 } while (cmd != 'e'); 62 return 0; 63 }
实验结果截图:
好处:Account 中将账户的公共操作都声明为虚函数,因此可以通过基类的指针来执行各种操作,因而各种类型的账户对象都可以通过一个基类指针数组来访问,使得我们可以通过一个统一的方式来操作各个账户。
不足:账户是在程序一开始创建好的,不能通过用户自己输入来创建,程序比较局限。