实验5 继承和多态
task1
代码
1 #pragma once 2 #include <iostream> 3 #include <string> 4 using std::cout; 5 using std::endl; 6 using std::string; 7 // 发行/出版物类:Publisher (抽象类) 8 class Publisher { 9 public: 10 Publisher(const string &s = ""); // 构造函数 11 public: 12 virtual void publish() const = 0; // 纯虚函数,作为接口继承 13 virtual void use() const = 0; // 纯虚函数,作为接口继承 14 protected: 15 string name; // 发行/出版物名称 16 }; 17 Publisher::Publisher(const string &s): name {s} { 18 } 19 // 图书类: Book 20 class Book: public Publisher { 21 public: 22 Book(const string &s = "", const string &a = ""); // 构造函数 23 24 public: 25 void publish() const override; // 接口 26 void use() const override; // 接口 27 28 private: 29 string author; // 作者 30 }; 31 32 Book::Book(const string &s, const string &a): Publisher{s}, author{a} { 33 } 34 35 void Book::publish() const { 36 cout << "Publishing book: 《" << name << "》 by " << author << endl; 37 } 38 39 void Book::use() const { 40 cout << "Reading book: " << name << " by " << author << endl; 41 } 42 43 44 // 电影类: Film 45 class Film: public Publisher { 46 public: 47 Film(const string &s = "", const string &d = ""); // 构造函数 48 49 public: 50 void publish() const override; // 接口 51 void use() const override; // 接口 52 53 private: 54 string director; // 导演 55 }; 56 57 Film::Film(const string &s, const string &d): Publisher{s}, director{d} { 58 } 59 60 void Film::publish() const { 61 cout << "Publishing film: <" << name << "> directed by " << director << endl; 62 } 63 64 void Film::use() const { 65 cout << "Watching film: " << name << " directed by " << director << endl; 66 } 67 68 69 // 音乐类:Music 70 class Music: public Publisher { 71 public: 72 Music(const string &s = "", const string &a = ""); 73 74 public: 75 void publish() const override; // 接口 76 void use() const override; // 接口 77 78 private: 79 string artist; // 音乐艺术家名称 80 }; 81 82 Music::Music(const string &s, const string &a): Publisher{s}, artist{a} { 83 } 84 85 void Music::publish() const { 86 cout << "Publishing music <" << name << "> by " << artist << endl; 87 } 88 89 void Music::use() const { 90 cout << "Listening to music: " << name << " by " << artist << endl; 91 }
1 #include "publisher.hpp" 2 #include <vector> 3 #include <typeinfo> 4 5 using std::vector; 6 7 void test() { 8 vector<Publisher *> v; 9 10 v.push_back(new Book("Harry Potter", "J.K. Rowling")); 11 v.push_back(new Film("The Godfather", "Francis Ford Coppola")); 12 v.push_back(new Music("Blowing in the wind", "Bob Dylan")); 13 14 for(auto &ptr: v) { 15 cout << "pointer type: " << typeid(ptr).name() << endl; // 输出指针类型 16 cout << "RTTI type: " << typeid(*ptr).name() << endl; // 输出指针指向的对象类型 17 ptr->publish(); 18 ptr->use(); 19 cout << endl; 20 } 21 } 22 23 int main() { 24 test(); 25 }
截图:
task2:
代码:
1 #pragma once 2 3 #include <string> 4 #include <iostream> 5 #include <iomanip> 6 7 using std::string; 8 using std::ostream; 9 using std::endl; 10 using std::setw; 11 using std::left; 12 13 class Book { 14 public: 15 Book(const string &name, const string &author, const string &translator, const string &isbn, float price); 16 17 friend ostream& operator<<(ostream &out, const Book &book); 18 19 private: 20 string name; // 书名 21 string author; // 作者 22 string translator; // 译者 23 string isbn; // isbn号 24 float price; // 定价 25 }; 26 27 // 成员函数实现 28 Book::Book(const string &name, const string &author, const string &translator, const string &isbn, float price) { 29 this->name = name; 30 this->author = author; 31 this->translator = translator; 32 this->isbn = isbn; 33 this->price = price; 34 } 35 36 // 友元实现 37 ostream& operator<<(ostream &out, const Book &book) { 38 out << left; 39 out << setw(15) << "书名:" << book.name << endl 40 << setw(15) << "作者:" << book.author << endl 41 << setw(15) << "译者:" << book.translator << endl 42 << setw(15) << "ISBN:" << book.isbn << endl 43 << setw(15) << "定价:" << book.price; 44 45 return out; 46 }
1 #pragma once 2 3 #include "book.hpp" 4 #include <iostream> 5 #include <string> 6 #include <iomanip> 7 8 using std::string; 9 using std::cout; 10 using std::endl; 11 using std::setw; 12 13 class BookSale { 14 public: 15 BookSale(const Book &b, float price, int amount); 16 int get_amount() const; 17 18 friend ostream& operator<<(ostream &out, const BookSale &item); 19 20 private: 21 Book rb; 22 float sales_price; // 售价 23 int sales_amount; // 销售数量 24 float revenue; // 营收 25 }; 26 27 // 成员函数实现 28 BookSale::BookSale(const Book &b, float price, int amount): rb{b}, sales_price(price), sales_amount{amount} { 29 revenue = sales_amount * sales_price; 30 } 31 32 int BookSale::get_amount() const { 33 return sales_amount; 34 } 35 36 // 友元函数实现 37 ostream& operator<<(ostream &out, const BookSale &item) { 38 out << left; 39 out << item.rb << endl 40 << setw(15) << "售价:" << item.sales_price << endl 41 << setw(15) << "销售数量:" << item.sales_amount << endl 42 << setw(15) << "营收:" << item.revenue; 43 44 return out; 45 }
1 #include "booksale.hpp" 2 #include <iostream> 3 #include <string> 4 #include <vector> 5 #include <algorithm> 6 7 // 按图书销售数额比较 8 bool compare_by_amount(const BookSale &x1, const BookSale &x2) { 9 return x1.get_amount() > x2.get_amount(); 10 } 11 12 void test() { 13 using namespace std; 14 15 vector<BookSale> sales_lst; // 存放图书销售记录 16 17 int books_number; 18 cout << "录入图书数量: "; 19 cin >> books_number; 20 21 cout << "录入图书销售记录" << endl; 22 for(int i = 0; i < books_number; ++i) { 23 string name, author, translator, isbn; 24 float price; 25 cout << string(20, '-') << "第" << i+1 << "本图书信息录入" << string(20, '-') << endl; 26 cout << "录入书名: "; cin >> name; 27 cout << "录入作者: "; cin >> author; 28 cout << "录入译者: "; cin >> translator; 29 cout << "录入isbn: "; cin >> isbn; 30 cout << "录入定价: "; cin >> price; 31 32 Book book(name, author, translator, isbn, price); 33 34 float sales_price; 35 int sales_amount; 36 37 cout << "录入售价: "; cin >> sales_price; 38 cout << "录入销售数量: "; cin >> sales_amount; 39 40 BookSale record(book, sales_price, sales_amount); 41 sales_lst.push_back(record); 42 } 43 44 // 按销售册数排序 45 sort(sales_lst.begin(), sales_lst.end(), compare_by_amount); 46 47 // 按销售册数降序输出图书销售信息 48 cout << string(20, '=') << "图书销售统计" << string(20, '=') << endl; 49 for(auto &t: sales_lst) { 50 cout << t << endl; 51 cout << string(40, '-') << endl; 52 } 53 } 54 55 int main() { 56 test(); 57 }
问题1:
通过友元函数&operator<<实现的
问题2:
通过函数compare-by-amount实现
问题3:
不理解
截图:
task3:
代码:
1 #pragma once 2 #include<iostream> 3 #include<string> 4 using std::string; 5 using std::cin; 6 using std::cout; 7 using std::endl; 8 class MachinePets{ 9 public: 10 MachinePets(const string& s); 11 string get_nickname() const; 12 virtual string talk()=0; 13 private: 14 string nickname; 15 }; 16 MachinePets::MachinePets(const string &s):nickname{s}{} 17 string MachinePets::get_nickname() const{ 18 return nickname; 19 } 20 class PetCats:public MachinePets{ 21 public: 22 PetCats(const string& s); 23 string talk() override; 24 }; 25 PetCats::PetCats(const string &s):MachinePets{s}{} 26 string PetCats::talk() { 27 return string("miao,miao"); 28 } 29 class PetDogs:public MachinePets{ 30 public: 31 PetDogs(const string& s); 32 string talk() override; 33 }; 34 PetDogs::PetDogs(const string &s):MachinePets{s}{} 35 string PetDogs::talk() { 36 return string("wang,wang"); 37 }
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 }
截图:
task4:
代码:
1 #pragma once 2 #include<iostream> 3 #include<string> 4 #include<iomanip> 5 using std::cin; 6 using std::cout; 7 using std::string; 8 using std::istream; 9 using std::ostream; 10 using std::setw; 11 using std::left; 12 class Film{ 13 public: 14 Film(){} 15 friend istream& operator>>(istream& in,Film& f); 16 friend ostream& operator<<(ostream& out,const Film& f); 17 friend bool compare_by_year(const Film& f1,const Film& f2); 18 string film_name; 19 string director_name; 20 string film_area; 21 int year; 22 }; 23 istream& operator>>(istream& in,Film& f){ 24 cout<<"录入片名:"; 25 in>>f.film_name; 26 cout<<"录入导演:"; 27 in>>f.director_name; 28 cout<<"录入制片国家/地区:"; 29 in>>f.film_area; 30 cout<<"录入上映年份:"; 31 in>>f.year; 32 return in; 33 } 34 ostream& operator<<(ostream&out,Film& f){ 35 out<<left; 36 out<<setw(20)<<f.film_name<<setw(20)<<f.director_name<<setw(20)<<f.film_area<<setw(20)<<f.year; 37 return out; 38 } 39 bool compare_by_year(const Film& f1,const Film& f2){ 40 return f1.year<f2.year; 41 }
1 #pragma once 2 #include"film.hpp" 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 8 void test() { 9 using namespace std; 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(), 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 }
截图:
task5:
代码:
1 #ifndef COMPLEX_HPP 2 #define COMPLEX_HPP 3 #include <iostream> 4 using std::istream; 5 using std::ostream; 6 template <typename T> 7 class Complex { 8 private: 9 T real; 10 T imag; 11 public: 12 // 默认 13 Complex(T r = 0, T i = 0) : real(r), imag(i) {} 14 // 复制 15 Complex(const Complex<T>& other) : real(other.real), imag(other.imag) {} 16 // +重载 17 Complex<T> operator+(const Complex<T>& other) const { 18 return Complex<T>(real + other.real, imag + other.imag); 19 } 20 // +=重载 21 Complex<T>& operator+=(const Complex<T>& other) { 22 real += other.real; 23 imag += other.imag; 24 return *this; 25 } 26 // ==重载 27 bool operator==(const Complex<T>& other) const { 28 return real == other.real && imag == other.imag; 29 } 30 // 得到实部 31 T get_real() const { 32 return real; 33 } 34 // 得到虚部 35 T get_imag() const { 36 return imag; 37 } 38 // 输出重载 39 friend ostream& operator<<(ostream& out, const Complex<T>& c) { 40 out << c.real; 41 if (c.imag >= 0) { 42 out<< " + " << c.imag << "i"; 43 } else { 44 out << " - " << -c.imag << "i"; 45 } 46 return out; 47 } 48 // 输入重载 49 friend istream& operator>>(istream& in, Complex<T>& c) { 50 in >> c.real >>c.imag; 51 return in; 52 } 53 }; 54 #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 }
截图:
task6:
代码:
1 date.h 2 #ifndef__DATE_H__ 3 #define__DATE_H__ 4 class Date{ 5 private: 6 int year; 7 int month; 8 int day; 9 int totalDays; 10 public: 11 Date(int year,int month,int day); 12 int getYear()const {return year;} 13 int getMonth()const{return month;} 14 int getDay()const{return day;} 15 int getMaxDay()const; 16 bool isLeapYear() const{ 17 return year%4==0&&year%100!=0||year%400==0; 18 } 19 void show() const; 20 int operator-(const Date& date) const{return totalDays-date.totalDays ;} 21 }; 22 #endif//__DATE_H__
1 date.cpp 2 #include"date.h" 3 #include<iostream> 4 #include<cstdlib> 5 using namespace std; 6 namespace{ 7 const int DAYS_BEFORE_MONTH[]={0,31,59,90,120,151,181,212,243,273,304,334,365}; 8 } 9 Date::Date(int year,int month,int day):year(year),month(month),day(day){ 10 if(day<=0||day>getMaxDay()){ 11 cout<<"Invalid date:"; 12 show(); 13 cout<<endl; 14 exit(1); 15 } 16 int years=year-1; 17 totalDays=years*365+years/4-years/100+years/400+DAYS_BEFORE_MONTH[month-1]+day; 18 if(isLeapYear()&&month>2) totalDays++; 19 } 20 int Date::getMaxDay() const{ 21 if(isLeapYear()&&month==2) return 29; 22 else return DAYS_BEFORE_MONTH[month]-DAYS_BEFORE_MONTH[month-1]; 23 } 24 void Date::show() const{ 25 cout<<getYear()<<"-"<<getMonth()<<"-"<<getDay(); 26 }
1 account.h 2 #ifndef__ACCOUNT_H__ 3 #define__ACCOUNT_H__ 4 #include"date.h" 5 #include"accumulator.h" 6 #include<string> 7 class Account{ 8 private: 9 std::string id; 10 double balance; 11 static double total; 12 protected: 13 Account(const Date &date,const std::string &id); 14 void record(const Date &date,double amount,const std::string &desc); 15 void error(const std::string &msg) const; 16 public: 17 const std::string &getId() const{return id;} 18 double getBalance() const {return balance;} 19 static double getTotal(){ 20 return total; 21 } 22 virtual void deposit(const Date& date,double amount,const std::string &desc)=0; 23 virtual void withdraw(const Date& date,double amount,const std::string &desc)=0; 24 virtual void settle(const Date &date)=0; 25 virtual void show() const; 26 }; 27 #endif//__ACCOUNT_H__
1 account.cpp 2 #include"account.h" 3 #include<cmath> 4 #include<iostream> 5 using namespace std; 6 double Account::total = 0; 7 Account::Account(const Date& date, const string& id) :id{ id }, balance{ 0 } { 8 date.show(); 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 enough 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) 58 record(date, -fee, "annual fee"); 59 acc.reset(date, getDebt()); 60 } 61 void CreditAccount::show()const { 62 Account::show(); 63 cout << "\tAvailable credit:" << getAvailableCredit(); 64 }
1 accumulator.h 2 #ifndef__ACCUMULATOR_H__ 3 #define__ACCUMULATOR_H__ 4 #include"date.h" 5 class Accumulator{ 6 private: 7 Date lastDate; 8 double value; 9 double sum; 10 public: 11 double getSum(const Date &date) const{return sum+value*(date-lastDate);} 12 }; 13 #endif//__ACCUMULATOR_H__
1 task5.6.cpp 2 #include"account.h"" 3 #include<iostream> 4 using namespace std; 5 int main() 6 { 7 Date date(2008,11,1); 8 SavingsAccount sa1(date,"s3755271",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 { 36 cout<<"["<<i<<"]"; 37 accounts[i]->show(); 38 cout<<endl; 39 } 40 break; 41 case 'c': 42 cin>>day; 43 if(day<date.getDay()) 44 cout<<"You cannot specify a previous day"; 45 else if(day>date.getDay()) 46 cout<<"Invalid day"; 47 else 48 date=Date(date.getYear(),date.getMonth(),day); 49 break; 50 case 'n': 51 if(date.getMonth()==12) 52 date=Date(date.getYear()+1,1,1); 53 else 54 date=Date(date.getYear(),date.getMonth()+1,1); 55 for(int i=0;i<n;i++) 56 { 57 accounts[i]->settle(date); 58 } 59 break; 60 } 61 }while(cmd!='e'); 62 return 0; 63 }
截图: