实验5 继承和多态

1. 实验任务1
publisher.hpp
 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<string>
 5 
 6 using std::cout;
 7 using std::endl;
 8 using std::string;
 9 
10 //Publisher
11 class Publisher {
12 public:
13     Publisher(const string& s = "");
14 
15 public:
16     virtual void publish()const = 0;
17     virtual void use()const = 0;
18 
19 protected:
20     string name;
21 };
22 
23 Publisher::Publisher(const string& s) :name{ s } {
24 }
25 
26 
27 //Book
28 class Book :public Publisher {
29 public:
30     Book(const string& s = "", const string& a = "");
31 
32 public:
33     void publish()const override;
34     void use()const override;
35 
36 private:
37     string author;
38 };
39 
40 Book::Book(const string& s, const string& a) :Publisher{ s }, author{ a } {
41 }
42 
43 void Book::publish()const{
44     cout << "Publishing book:《" << name << "》by " << author << endl;
45 }
46 
47 void Book::use()const {
48     cout << "Reading book: " << name << " by " << author << endl;
49 }
50 
51 
52 //Film
53 class Film : public Publisher {
54 public:
55     Film(const string& s = "", const string& d = ""); 
56 public:
57     void publish() const override; 
58     void use() const override; 
59 private:
60     string director; 
61 };
62 
63 Film::Film(const string& s, const string& d) : Publisher{ s }, director{ d } {
64 }
65 
66 void Film::publish() const {
67     cout << "Publishing film: <" << name << "> directed by " << director <<
68         endl;
69 }
70 
71 void Film::use() const {
72     cout << "Watching film: " << name << " directed by " << director <<
73         endl;
74 }
75 
76 
77 //Music
78 class Music : public Publisher {
79 public:
80     Music(const string& s = "", const string& a = "");
81 
82 public:
83     void publish() const override; 
84     void use() const override; 
85 
86 private:
87     string artist; 
88 };
89 
90 Music::Music(const string& s, const string& a) : Publisher{ s }, artist{ a } {
91 }
92 
93 void Music::publish() const {
94     cout << "Publishing music <" << name << "> by " << artist << endl;
95 }
96 
97 void Music::use() const {
98     cout << "Listening to music: " << name << " by " << artist << endl;
99 }
View Code
task1.cpp
 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 }
View Code

 

2. 实验任务2
book.hpp 
 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& suthor, 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;
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) << "定价:" << book.price;
43 
44     return out;
45 }
View Code
booksale.hpp 
 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 preice, 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 }
View Code
task2.cpp
 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 }
View Code

1.重载插入运算符<<用于图书销售记录的代码是如何编写的?
首先在类中声明友元函数friend ostream& operator<<(ostream& out, const BookSale& item);接着在类外定义该函数
2. 图书销售记录按照销售数量降序排序,代码是如何实现的?
通过编写一个比较函数,该函数内比较,返回真或假;
3. 编写组合类BookSale构造函数时,其内嵌成员book初始化的代码写法?
Book book(name, author, translator, isbn, price);
 
3. 实验任务3
 pets.hpp
 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 
 5 using namespace std;
 6 
 7 class MachinePets {
 8 public:
 9     MachinePets(const string &s="");
10     virtual string talk()const = 0;
11     string get_nickname()const;
12 
13 private:
14     string nickname;
15 };
16 
17 string MachinePets::get_nickname()const {
18     return nickname;
19 }
20 MachinePets::MachinePets(const string& s) :nickname{ s }{}
21 
22 class PetCats :public MachinePets {
23 public:
24     PetCats(const string& s);
25     string talk()const override;
26 
27 };
28 PetCats::PetCats(const string& s):MachinePets{s}{}
29 string PetCats::talk()const {
30     return "miao wu~";
31 }
32 
33 class PetDogs :public MachinePets {
34 public:
35     PetDogs(const string& s);
36     string talk()const override;
37 
38 };
39 PetDogs::PetDogs(const string& s) :MachinePets{ s } {}
40 string PetDogs::talk()const {
41     return "wang wang~";
42 }
View Code

task3.cpp

 1 #include <iostream>
 2 #include <vector>
 3 #include "pets.hpp"
 4 void test() {
 5     using namespace std;
 6     vector<MachinePets*> pets;
 7     pets.push_back(new PetCats("miku"));
 8     pets.push_back(new PetDogs("da huang"));
 9     for (auto& ptr : pets)
10         cout << ptr->get_nickname() << " says " << ptr->talk() << endl;
11 }
12 int main() {
13     test();
14 }
View Code

 

4. 实验任务4
film.hpp
 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 #include<iomanip>
 5 
 6 using std::string;
 7 using std::ostream;
 8 using std::istream;
 9 using std::endl;
10 using std::setw;
11 using std::left;
12 using std::cout;
13 using std::cin;
14 
15 class Film {
16 public:
17     Film(const string& name=" ", const string& director=" ", const string& country=" ", const string& year=" ");
18     string get_year()const;
19     friend ostream& operator<<(ostream &out, const Film &film);
20     friend istream& operator>>(istream& in, Film& film);
21 private:
22     string name;
23     string director;
24     string country;
25     string year;
26 };
27 
28 Film::Film(const string& name, const string& director, const string& country, const string& year) {
29     this->name = name;
30     this->director = director;
31     this->country = country;
32     this->year = year;
33 }
34 
35 string Film::get_year()const {
36     return year;
37 }
38 
39 ostream& operator<<(ostream &out, const Film &film) {
40     out << left;
41     out << setw(15) << film.name
42         << setw(15) << film.director
43         << setw(15) << film.country
44         << setw(15) << film.year << endl;
45 
46     return out;
47 }
48 
49 istream& operator>>(istream& is, Film& film) {
50     cout << "录入片名: ";
51     is >> film.name;
52     cout<<endl;
53     cout << "录入导演: ";
54     is >> film.director;
55     cout << endl;
56     cout << "录入制片国家/地区: ";
57     is >> film.country;
58     cout << endl;
59     cout << "录入上映年份: ";
60     is >> film.year;
61     cout << endl;
62     return is;
63 }
64 
65 bool compare_by_year(const Film &f1,const Film &f2) {
66     return f1.get_year() < f2.get_year();
67 }
View Code
task4.cpp
 1 #include "film.hpp"
 2 #include <iostream>
 3 #include <string>
 4 #include <vector>
 5 #include <algorithm>
 6 void test() {
 7     using namespace std;
 8     int n;
 9     cout << "输入电影数目: ";
10     cin >> n;
11     cout << "录入" << n << "部影片信息" << endl;
12     vector<Film> film_lst;
13     for (int i = 0; i < n; ++i) {
14         Film f;
15         cout << string(20, '-') << "" << i + 1 << "部影片录入" << string(20,
16             '-') << endl;
17         cin >> f;
18         film_lst.push_back(f);
19     }
20     // 按发行年份升序排序
21     sort(film_lst.begin(), film_lst.end(), compare_by_year);
22     cout << string(20, '=') + "电影信息(按发行年份)" + string(20, '=') << endl;
23     for (auto& f : film_lst)
24         cout << f << endl;
25 }
26 int main() {
27     test();
28 }
View Code

 

 
5. 实验任务5
Complex.hpp
 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 
 5 using namespace std;
 6 
 7 template<typename T>
 8 class Complex {
 9 public:
10     Complex(T r = 0, T i = 0);
11     Complex(const Complex& c);
12     Complex<T>& operator+=(const Complex<T>& c);
13 
14     T get_real()const;
15     T get_imag()const;
16 
17     bool operator==(const Complex<T>& c)const;
18 
19     template<typename U>
20     friend istream& operator>>(istream& in, Complex<U>& c);
21     template<typename U>
22     friend ostream& operator<<(ostream& out, const Complex<U>& c);
23     template<typename U>
24     friend Complex<U> operator+(const Complex<U>& c1, const Complex<U>& c2);
25 private:
26     T real, imag;
27 };
28 
29 //构造函数
30 template<typename T>
31 Complex<T>::Complex(T r, T i) : real{ r }, imag{ i } {
32 }
33 //复制构造
34 template<typename T>
35 Complex<T>::Complex(const Complex& c) {
36     real = c.real;
37     imag = c.imag;
38 }
39 
40 //+=
41 template<typename T>
42 Complex<T>& Complex<T>::operator+=(const Complex& c) {
43     real += c.real;
44     imag += c.imag;
45     return *this;
46 }
47 
48 //get_real
49 template<typename T>
50 T Complex<T>::get_real()const {
51     return real;
52 }
53 
54 //get_imag
55 template<typename T>
56 T Complex<T>::get_imag()const {
57     return imag;
58 }
59 
60 //==
61 template<typename T>
62 bool Complex<T>::operator==(const Complex<T>& c)const {
63     return real == c.real && imag == c.imag;
64 }
65 
66 //+
67 template<typename T>
68 Complex<T> operator+(const Complex<T>& c1, const Complex<T>& c2) {
69     return Complex<T>(c1.real + c2.real, c1.imag + c2.imag);
70 }
71 
72 //<<
73 template<typename T>
74 ostream& operator<<(ostream& out, const Complex<T>& c) {
75     if (c.imag >= 0)
76         out << c.real << " + " << c.imag << "i";
77     else
78         out << c.real << " - " << -c.imag << "i";
79     return out;
80 }
81 
82 //>>
83 template<typename T>
84 istream& operator>>(istream& in, Complex<T>& c) {
85     in >> c.real >> c.imag;
86     return in;
87 }
View Code
task5.cpp
 1 #include "Complex.hpp"
 2 #include <iostream>
 3 using std::cin;
 4 using std::cout;
 5 using std::endl;
 6 using std::boolalpha;
 7 void test1() {
 8     Complex<int> c1(2, -5), c2(c1);
 9     cout << "c1 = " << c1 << endl;
10     cout << "c2 = " << c2 << endl;
11     cout << "c1 + c2 = " << c1 + c2 << endl;
12     c1 += c2;
13     cout << "c1 = " << c1 << endl;
14     cout << boolalpha << (c1 == c2) << endl;
15 }
16 void test2() {
17     Complex<double> c1, c2;
18     cout << "Enter c1 and c2: ";
19     cin >> c1 >> c2;
20     cout << "c1 = " << c1 << endl;
21     cout << "c2 = " << c2 << endl;
22     cout << "c1.real = " << c1.get_real() << endl;
23     cout << "c1.imag = " << c1.get_imag() << endl;
24 }
25 int main() {
26     cout << "自定义类模板Complex测试1: " << endl;
27     test1();
28     cout << endl;
29     cout << "自定义类模板Complex测试2: " << endl;
30     test2();
31 }
View Code

 

 
6. 实验任务6
 account.h
 1 #pragma once
 2 #include"date.h"
 3 #include"accumulator.h"
 4 #include<string>
 5 using namespace std;
 6 class Account {
 7 private:
 8     string id;
 9     double balance;
10     static double total;
11 protected:
12     Account(const Date& date, const string& id);
13     void record(const Date& date, double amoount, const string& desc);
14     void error(const string& msg) const;
15 public:const string& getId() { return id; }
16       double getBalance() const { return balance; }
17       static double getTotal() { return total; }
18       virtual void deposit(const Date& date, double amount, const string& desc) = 0;
19       virtual void withdraw(const Date& date, double amount, const string& desc) = 0;
20       virtual void settle(const Date& date) = 0;
21       virtual void show() const;
22 };
23 class SavingsAccount :public Account {
24 private:
25     Accumulator acc;
26     double rate;
27 public:
28     SavingsAccount(const Date& date, const string& id, double rate);
29     double getRate() const { return  rate; }
30     void deposit(const Date& date, double amount, const string& desc);
31     void withdraw(const Date& date, double amount, const string& desc);
32     void settle(const Date& date);
33 };
34 class CreditAccount :public Account {
35 private:
36     Accumulator acc;
37     double credit;
38     double rate;
39     double fee;
40     double getDebt()const {
41         double balance = getBalance();
42         return(balance < 0 ? balance : 0);
43     }
44 public:CreditAccount(const Date& date, const string& id, double credit, double rate, double fee);
45       double getCredit()const { return credit; }
46       double getRate() const { return rate; }
47       double getFee() const { return fee; }
48       double getAvailableCredit() const {
49           if (getBalance() < 0)return credit + getBalance();
50           else return credit;
51       }
52       void deposit(const Date& date, double amount, const string& desc);
53       void withdraw(const Date& date, double amount, const string& desc);
54       void settle(const Date& date);
55       void show() const;
56 };
View Code
accumulator.h
 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 };
View Code
date.h
 1 #pragma once
 2 
 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 {
12         return year;
13     }
14     int getMonth() const {
15         return month;
16     }
17     int getDay() const {
18         return day;
19     }
20     int getMaxDay() const;
21     bool isLeapYear() const {
22         return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
23     }
24     void show() const;
25     int operator-(const Date& date) const {
26         return totalDays - date.totalDays;
27     }
28 };
View Code
account.cpp
 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 }
View Code
date.cpp
 1 #include"date.h"
 2 #include<iostream>
 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 }
View Code
task6.cpp
 1 #include"account.h"
 2 #include<iostream>
 3 using namespace std;
 4 int main() {
 5     Date date(2008, 11, 1);
 6     SavingsAccount sa1(date, "S3755217", 0.015);
 7     SavingsAccount sa2(date, "02342342", 0.015);
 8     CreditAccount ca(date, "C5392394", 10000, 0.0005, 50);
 9     Account* accounts[] = { &sa1,&sa2,&ca };
10     const int n = sizeof(accounts) / sizeof(Account*);
11     cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl;
12     char cmd;
13     do {
14         date.show();
15         cout << "\tTotal:" << Account::getTotal() << "\tcommand>";
16         int index, day;
17         double amount;
18         string desc;
19         cin >> cmd;
20         switch (cmd) {
21         case 'd':
22             cin >> index >> amount;
23             getline(cin, desc);
24             accounts[index]->deposit(date, amount, desc);
25             break;
26         case 'w':
27             cin >> index >> amount;
28             getline(cin, desc);
29             accounts[index]->withdraw(date, amount, desc);
30             break;
31         case 's':
32             for (int i = 0; i < n; i++) {
33                 cout << "[" << i << "]";
34                 accounts[i]->show();
35                 cout << endl;
36             }
37             break;
38         case 'c':
39             cin >> day;
40             if (day < date.getDay()) {
41                 cout << "You cannot specify a previous day";
42             }
43             else if (day > date.getMaxDay())
44                 cout << "Invalid day";
45             else date = Date(date.getYear(), date.getMonth(), day);
46             break;
47         case 'n':
48             if (date.getMonth() == 12)
49                 date = Date(date.getYear() + 1, 1, 1);
50             else date = Date(date.getYear(), date.getMonth() + 1, 1);
51             for (int i = 0; i < n; i++) {
52                 accounts[i]->settle(date);
53             }
54             break;
55         }
56     } while (cmd != 'e');
57     return 0;
58 }
View Code

 

 
posted @ 2024-12-04 21:47  Mikuuuu  阅读(5)  评论(0编辑  收藏  举报