实验五
实验任务一:
publisher.hpp
1 #pragma once 2 3 #include <string> 4 5 // 发行/出版物类:Publisher (抽象类) 6 class Publisher { 7 public: 8 Publisher(const std::string &name_ = ""); // 构造函数 9 virtual ~Publisher() = default; 10 11 public: 12 virtual void publish() const = 0; // 纯虚函数,作为接口继承 13 virtual void use() const = 0; // 纯虚函数,作为接口继承 14 15 protected: 16 std::string name; // 发行/出版物名称 17 }; 18 19 // 图书类: Book 20 class Book: public Publisher { 21 public: 22 Book(const std::string &name_ = "", const std::string &author_ = ""); // 构造函数 23 24 public: 25 void publish() const override; // 接口 26 void use() const override; // 接口 27 28 private: 29 std::string author; // 作者 30 }; 31 32 // 电影类: Film 33 class Film: public Publisher { 34 public: 35 Film(const std::string &name_ = "", const std::string &director_ = ""); // 构造函数 36 37 public: 38 void publish() const override; // 接口 39 void use() const override; // 接口 40 41 private: 42 std::string director; // 导演 43 }; 44 45 46 // 音乐类:Music 47 class Music: public Publisher { 48 public: 49 Music(const std::string &name_ = "", const std::string &artist_ = ""); 50 51 public: 52 void publish() const override; // 接口 53 void use() const override; // 接口 54 55 private: 56 std::string artist; // 音乐艺术家名称 57 };
publisher.cpp
1 #include <iostream> 2 #include <string> 3 #include "publisher.hpp" 4 5 // Publisher类:实现 6 Publisher::Publisher(const std::string &name_): name {name_} { 7 } 8 9 10 // Book类: 实现 11 Book::Book(const std::string &name_ , const std::string &author_ ): Publisher{name_}, author{author_} { 12 } 13 14 void Book::publish() const { 15 std::cout << "Publishing book《" << name << "》 by " << author << '\n'; 16 } 17 18 void Book::use() const { 19 std::cout << "Reading book 《" << name << "》 by " << author << '\n'; 20 } 21 22 23 // Film类:实现 24 Film::Film(const std::string &name_, const std::string &director_):Publisher{name_},director{director_} { 25 } 26 27 void Film::publish() const { 28 std::cout << "Publishing film <" << name << "> directed by " << director << '\n'; 29 } 30 31 void Film::use() const { 32 std::cout << "Watching film <" << name << "> directed by " << director << '\n'; 33 } 34 35 36 // Music类:实现 37 Music::Music(const std::string &name_, const std::string &artist_): Publisher{name_}, artist{artist_} { 38 } 39 40 void Music::publish() const { 41 std::cout << "Publishing music <" << name << "> by " << artist << '\n'; 42 } 43 44 void Music::use() const { 45 std::cout << "Listening to music <" << name << "> by " << artist << '\n'; 46 }
task5.1.cpp
1 #include <memory> 2 #include <iostream> 3 #include <vector> 4 #include "publisher.hpp" 5 6 void test1() { 7 std::vector<Publisher *> v; 8 9 v.push_back(new Book("Harry Potter", "J.K. Rowling")); 10 v.push_back(new Film("The Godfather", "Francis Ford Coppola")); 11 v.push_back(new Music("Blowing in the wind", "Bob Dylan")); 12 13 for(Publisher *ptr: v) { 14 ptr->publish(); 15 ptr->use(); 16 std::cout << '\n'; 17 delete ptr; 18 } 19 } 20 21 void test2() { 22 std::vector<std::unique_ptr<Publisher>> v; 23 24 v.push_back(std::make_unique<Book>("Harry Potter", "J.K. Rowling")); 25 v.push_back(std::make_unique<Film>("The Godfather", "Francis Ford Coppola")); 26 v.push_back(std::make_unique<Music>("Blowing in the wind", "Bob Dylan")); 27 28 for(const auto &ptr: v) { 29 ptr->publish(); 30 ptr->use(); 31 std::cout << '\n'; 32 } 33 } 34 35 void test3() { 36 Book book("A Philosophy of Software Design", "John Ousterhout"); 37 book.publish(); 38 book.use(); 39 } 40 41 int main() { 42 std::cout << "运行时多态:纯虚函数、抽象类\n"; 43 44 std::cout << "\n测试1: 使用原始指针\n"; 45 test1(); 46 47 std::cout << "\n测试2: 使用智能指针\n"; 48 test2(); 49 50 std::cout << "\n测试3: 直接使用类\n"; 51 test3(); 52 }
运行结果:

问题1:
(1)Pubilsher包含纯虚函数,virtual void publish() const = 0;和virtual void use() const = 0;
(2)不能,因为抽象类只能作为基类被继承
问题2:
(1)void publish() const override;
void use() const override;
(2)无法覆盖基类的const成员函数
问题3:
(1)Publisher*
(2)Book、Film、Music
(3)因为虚析构函数是为了多态场景下正确调用派生类的析构函数,出现派生类的资源无法释放
实验任务二:
book.hpp
book.cpp
booksale.hpp
booksale.cpp
task5.2.cpp
浙公网安备 33010602011771号