实验五
任务一:
#pragma once #include <string> // 发行/出版物类:Publisher (抽象类) class Publisher { public: Publisher(const std::string &name_ = ""); // 构造函数 virtual ~Publisher() = default; public: virtual void publish() const = 0; // 纯虚函数,作为接口继承 virtual void use() const = 0; // 纯虚函数,作为接口继承 protected: std::string name; // 发行/出版物名称 }; // 图书类: Book class Book: public Publisher { public: Book(const std::string &name_ = "", const std::string &author_ = ""); // 构造函数 public: void publish() const override; // 接口 void use() const override; // 接口 private: std::string author; // 作者 }; // 电影类: Film class Film: public Publisher { public: Film(const std::string &name_ = "", const std::string &director_ = ""); // 构造函数 public: void publish() const override; // 接口 void use() const override; // 接口 private: std::string director; // 导演 }; // 音乐类:Music class Music: public Publisher { public: Music(const std::string &name_ = "", const std::string &artist_ = ""); public: void publish() const override; // 接口 void use() const override; // 接口 private: std::string artist; // 音乐艺术家名称 };
#include <iostream> #include <string> #include "publisher.hpp" // Publisher类:实现 Publisher::Publisher(const std::string &name_): name {name_} { } // Book类: 实现 Book::Book(const std::string &name_ , const std::string &author_ ): Publisher{name_}, author{author_} { } void Book::publish() const { std::cout << "Publishing book《" << name << "》 by " << author << '\n'; } void Book::use() const { std::cout << "Reading book 《" << name << "》 by " << author << '\n'; } // Film类:实现 Film::Film(const std::string &name_, const std::string &director_):Publisher{name_},director{director_} { } void Film::publish() const { std::cout << "Publishing film <" << name << "> directed by " << director << '\n'; } void Film::use() const { std::cout << "Watching film <" << name << "> directed by " << director << '\n'; } // Music类:实现 Music::Music(const std::string &name_, const std::string &artist_): Publisher{name_}, artist{artist_} { } void Music::publish() const { std::cout << "Publishing music <" << name << "> by " << artist << '\n'; } void Music::use() const { std::cout << "Listening to music <" << name << "> by " << artist << '\n'; }
#include <memory> #include <iostream> #include <vector> #include "publisher.hpp" void test1() { std::vector<Publisher *> v; v.push_back(new Book("Harry Potter", "J.K. Rowling")); v.push_back(new Film("The Godfather", R"(Francis Ford Coppola)")); v.push_back(new Music("Blowing in the wind", "Bob Dylan")); for(Publisher *ptr: v) { ptr->publish(); ptr->use(); std::cout << '\n'; delete ptr; } } void test2() { std::vector<std::unique_ptr<Publisher>> v; v.push_back(std::make_unique<Book>("Harry Potter", "J.K. Rowling")); v.push_back(std::make_unique<Film>("The Godfather", "Francis Ford Coppola")); v.push_back(std::make_unique<Music>("Blowing in the wind", "Bob Dylan")); for(const auto &ptr: v) { ptr->publish(); ptr->use(); std::cout << '\n'; } } void test3() { Book book("A Philosophy of Software Design", "John Ousterhout"); book.publish(); book.use(); } int main() { std::cout << "运行时多态:纯虚函数、抽象类\n"; std::cout << "\n测试1: 使用原始指针\n"; test1(); std::cout << "\n测试2: 使用智能指针\n"; test2(); std::cout << "\n测试3: 直接使用类\n"; test3(); }

问题一:
(1)纯虚函数。virtual void publish() const = 0;virtual void use() const = 0;
(2)不能通过,因为Publisher类是抽象类,抽象类不能实例化
问题二:
(1)void publish() const override;
void use() const override;
(2)error: invalid covariant return type for 'virtual void Film::publish()' error: overriding 'virtual void Publisher::publish() const' error: invalid covariant return type for 'virtual void Film::use()' error: overriding 'virtual void Publisher::use() const'
问题三:
(1)指向Publisher类的指针
(2)Book Film Music
(3)为了能正确调用子类的析构函数;无法通过基类指针调用子类析构函数删除子类
任务二:
#pragma once #include <string> // 图书描述信息类Book: 声明 class Book { public: Book(const std::string &name_, const std::string &author_, const std::string &translator_, const std::string &isbn_, double price_); friend std::ostream& operator<<(std::ostream &out, const Book &book); private: std::string name; // 书名 std::string author; // 作者 std::string translator; // 译者 std::string isbn; // isbn号 double price; // 定价 };
#pragma once #include <string> #include "book.hpp" // 图书销售记录类BookSales:声明 class BookSale { public: BookSale(const Book &rb_, double sales_price_, int sales_amount_); int get_amount() const; // 返回销售数量 double get_revenue() const; // 返回营收 friend std::ostream& operator<<(std::ostream &out, const BookSale &item); private: Book rb; double sales_price; // 售价 int sales_amount; // 销售数量 };
#include <iomanip> #include <iostream> #include <string> #include "book.hpp" // 图书描述信息类Book: 实现 Book::Book(const std::string &name_, const std::string &author_, const std::string &translator_, const std::string &isbn_, double price_):name{name_}, author{author_}, translator{translator_}, isbn{isbn_}, price{price_} { } // 运算符<<重载实现 std::ostream& operator<<(std::ostream &out, const Book &book) { using std::left; using std::setw; out << left; out << setw(15) << "书名:" << book.name << '\n' << setw(15) << "作者:" << book.author << '\n' << setw(15) << "译者:" << book.translator << '\n' << setw(15) << "ISBN:" << book.isbn << '\n' << setw(15) << "定价:" << book.price; return out; }
#include <iomanip> #include <iostream> #include <string> #include "booksale.hpp" // 图书销售记录类BookSales:实现 BookSale::BookSale(const Book &rb_, double sales_price_, int sales_amount_): rb{rb_}, sales_price{sales_price_}, sales_amount{sales_amount_} { } int BookSale::get_amount() const { return sales_amount; } double BookSale::get_revenue() const { return sales_amount * sales_price; } // 运算符<<重载实现 std::ostream& operator<<(std::ostream &out, const BookSale &item) { using std::left; using std::setw; out << left; out << item.rb << '\n' << setw(15) << "售价:" << item.sales_price << '\n' << setw(15) << "销售数量:" << item.sales_amount << '\n' << setw(15) << "营收:" << item.get_revenue(); return out; }
#include <algorithm> #include <iomanip> #include <iostream> #include <string> #include <vector> #include "booksale.hpp" // 按图书销售数量比较 bool compare_by_amount(const BookSale &x1, const BookSale &x2) { return x1.get_amount() > x2.get_amount(); } void test() { using std::cin; using std::cout; using std::getline; using std::sort; using std::string; using std::vector; using std::ws; vector<BookSale> sales_records; // 图书销售记录表 int books_number; cout << "录入图书数量: "; cin >> books_number; cout << "录入图书销售记录\n"; for(int i = 0; i < books_number; ++i) { string name, author, translator, isbn; double price; cout << string(20, '-') << "第" << i+1 << "本图书信息录入" << string(20, '-') << '\n'; cout << "录入书名: "; getline(cin>>ws, name); cout << "录入作者: "; getline(cin>>ws, author); cout << "录入译者: "; getline(cin>>ws, translator); cout << "录入isbn: "; getline(cin>>ws, isbn); cout << "录入定价: "; cin >> price; Book book(name, author, translator, isbn, price); double sales_price; int sales_amount; cout << "录入售价: "; cin >> sales_price; cout << "录入销售数量: "; cin >> sales_amount; BookSale record(book, sales_price, sales_amount); sales_records.push_back(record); } // 按销售册数排序 sort(sales_records.begin(), sales_records.end(), compare_by_amount); // 按销售册数降序输出图书销售信息 cout << string(20, '=') << "图书销售统计" << string(20, '=') << '\n'; for(auto &record: sales_records) { cout << record << '\n'; cout << string(40, '-') << '\n'; } } int main() { test(); }


问题一:
(1)两处,Book 、 BookSale
(2)out << item.rb << '\n'; cout << t << endl;
问题二:
(1)通过compare_by_amount函数和标准库函数std::sort
任务四:
#pragma once #include<string> #include<iostream> using namespace std; class MachinePet { public: MachinePet(const string name); ~MachinePet(); string get_nickname()const; virtual string talk() const = 0; private: string nickname; }; class PetCat :public MachinePet{ public: PetCat(const string name); string talk()const override; }; class PetDog :public MachinePet{ public: PetDog(const string name); string talk()const override; };
#include<iostream> #include"pet.hpp" using namespace std; MachinePet::MachinePet(const string name) :nickname{ name } {}; MachinePet::~MachinePet() {}; string MachinePet::get_nickname() const{ return nickname; } PetCat::PetCat(const string name) : MachinePet{ name } {}; string PetCat::talk() const{ return "Miao"; } PetDog::PetDog(const string name) :MachinePet{ name } {}; string PetDog::talk()const { return "Wang"; }
#include <iostream> #include <memory> #include <vector> #include"pet.hpp" void test1() { std::vector<MachinePet*> pets; pets.push_back(new PetCat("miku")); pets.push_back(new PetDog("da huang")); for (MachinePet* ptr : pets) { std::cout << ptr->get_nickname() << " says " << ptr->talk() << '\n'; delete ptr; // 须手动释放资源 } } void test2() { std::vector<std::unique_ptr<MachinePet>> pets; pets.push_back(std::make_unique<PetCat>("miku")); pets.push_back(std::make_unique<PetDog>("da huang")); for (auto const& ptr : pets) std::cout << ptr->get_nickname() << " says " << ptr->talk() << '\n'; } void test3() { // MachinePet pet("little cutie"); // 编译报错:无法定义抽象类对象 const PetCat cat("miku"); std::cout << cat.get_nickname() << " says " << cat.talk() << '\n'; const PetDog dog("da huang"); std::cout << dog.get_nickname() << " says " << dog.talk() << '\n'; } int main() { std::cout << "测试1: 使用原始指针\n"; test1(); std::cout << "\n测试2: 使用智能指针\n"; test2(); std::cout << "\n测试3: 直接使用类\n"; test3(); }

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


浙公网安备 33010602011771号