实验5 多态
task1:
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 };
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 }
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)publisher类中有纯虚函数,virtual void publish() const = 0;virtual void use() const = 0
(2)不能通过,抽象类需要被其他派生类继承,不可以单独定义一个对象实例
问题2:
(1)void publish() const override;void use() const override;
(2)报错信息:void Film::publish()marked override,but not override
void Film::use()marked override,but not override
问题3:
(1)ptr声明类型是publisher类的指针
(2)ptr实际指向对象分别为Book,Film,Music
(3)因为需要根据子类的不同去分别调用析构函数释放内存;出现的问题:其子类无法调用析构函数,导致内存无法释放
task2:
1 #pragma once 2 #include <string> 3 4 // 图书描述信息类Book: 声明 5 class Book { 6 public: 7 Book(const std::string &name_, 8 const std::string &author_, 9 const std::string &translator_, 10 const std::string &isbn_, 11 double price_); 12 13 friend std::ostream& operator<<(std::ostream &out, const Book &book); 14 15 private: 16 std::string name; // 书名 17 std::string author; // 作者 18 std::string translator; // 译者 19 std::string isbn; // isbn号 20 double price; // 定价 21 };
1 #include <iomanip> 2 #include <iostream>
1 #pragma once 2 3 #include <string> 4 #include "book.hpp" 5 6 // 图书销售记录类BookSales:声明 7 class BookSale { 8 public: 9 BookSale(const Book &rb_, double sales_price_, int sales_amount_); 10 int get_amount() const; // 返回销售数量 11 double get_revenue() const; // 返回营收 12 13 friend std::ostream& operator<<(std::ostream &out, const BookSale &item); 14 15 private: 16 Book rb; 17 double sales_price; // 售价 18 int sales_amount; // 销售数量 19 };
3 #include <string> 4 #include "book.hpp"
1 #include <iomanip> 2 #include <iostream> 3 #include <string> 4 #include "booksale.hpp" 5 6 // 图书销售记录类BookSales:实现 7 BookSale::BookSale(const Book &rb_, 8 double sales_price_, 9 int sales_amount_): rb{rb_}, sales_price{sales_price_}, sales_amount{sales_amount_} { 10 } 11 12 int BookSale::get_amount() const { 13 return sales_amount; 14 } 15 16 double BookSale::get_revenue() const { 17 return sales_amount * sales_price; 18 } 19 20 // 运算符<<重载实现 21 std::ostream& operator<<(std::ostream &out, const BookSale &item) { 22 using std::left; 23 using std::setw; 24 25 out << left; 26 out << item.rb << '\n' 27 << setw(15) << "售价:" << item.sales_price << '\n' 28 << setw(15) << "销售数量:" << item.sales_amount << '\n' 29 << setw(15) << "营收:" << item.get_revenue(); 30 31 return out; 32 }
5 6
1 #include <algorithm> 2 #include <iomanip> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include "booksale.hpp" 7 8 // 按图书销售数量比较 9 bool compare_by_amount(const BookSale &x1, const BookSale &x2) { 10 return x1.get_amount() > x2.get_amount(); 11 } 12 13 void test() { 14 using std::cin; 15 using std::cout; 16 using std::getline; 17 using std::sort; 18 using std::string; 19 using std::vector; 20 using std::ws; 21 22 vector<BookSale> sales_records; // 图书销售记录表 23 24 int books_number; 25 cout << "录入图书数量: "; 26 cin >> books_number; 27 28 cout << "录入图书销售记录\n"; 29 for(int i = 0; i < books_number; ++i) { 30 string name, author, translator, isbn; 31 double price; 32 cout << string(20, '-') << "第" << i+1 << "本图书信息录入" << string(20, '-') << '\n'; 33 cout << "录入书名: "; getline(cin>>ws, name); 34 cout << "录入作者: "; getline(cin>>ws, author); 35 cout << "录入译者: "; getline(cin>>ws, translator); 36 cout << "录入isbn: "; getline(cin>>ws, isbn); 37 cout << "录入定价: "; cin >> price; 38 39 Book book(name, author, translator, isbn, price); 40 41 double sales_price; 42 int sales_amount; 43 44 cout << "录入售价: "; cin >> sales_price; 45 cout << "录入销售数量: "; cin >> sales_amount; 46 47 BookSale record(book, sales_price, sales_amount); 48 sales_records.push_back(record); 49 } 50 51 // 按销售册数排序 52 sort(sales_records.begin(), sales_records.end(), compare_by_amount); 53 54 // 按销售册数降序输出图书销售信息 55 cout << string(20, '=') << "图书销售统计" << string(20, '=') << '\n'; 56 for(auto &record: sales_records) { 57 cout << record << '\n'; 58 cout << string(40, '-') << '\n'; 59 } 60 } 61 62 int main() { 63 test(); 64 }
7 // 图书描述信息类Book: 实现 8 Book::Book(const std::string &name_, 9 const std::string &author_, 10 const std::string &translator_, 11 const std::string &isbn_, 12 double price_):name{name_}, author{author_}, translator{translator_}, isbn{isbn_}, price{price_} { 13 } 14 15 // 运算符<<重载实现 16 std::ostream& operator<<(std::ostream &out, const Book &book) { 17 using std::left; 18 using std::setw; 19 20 out << left; 21 out << setw(15) << "书名:" << book.name << '\n' 22 << setw(15) << "作者:" << book.author << '\n' 23 << setw(15) << "译者:" << book.translator << '\n' 24 << setw(15) << "ISBN:" << book.isbn << '\n' 25 << setw(15) << "定价:" << book.price; 26 27 return out; 28 }

问题1:
(1)重载了两处,第一处重载用于Book类,第二处用于BookSale类
(2)
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;
out << left;
out << item.rb << '\n'
<< setw(15) << "售价:" << item.sales_price << '\n'
<< setw(15) << "销售数量:" << item.sales_amount << '\n'
<< setw(15) << "营收:" << item.get_revenue();
问题2:
(1)使用标准库sort函数和迭代器begin()和end()
(2)
task3:
1 #pragma once 2 3 #include<iostream> 4 #include<string> 5 6 class MachinePet{ 7 public: 8 MachinePet(const std::string &nickname_):nickname{nickname_}{} //构造函数 9 std::string get_nickname()const{ 10 return nickname; 11 } // 供外部获取昵称 12 13 virtual std::string talk()const =0; //返回叫声 多态 14 virtual ~MachinePet()=default;//析构函数 15 protected: 16 std::string nickname; //昵称 17 }; 18 19 class PetCat:public MachinePet{ 20 public: 21 PetCat(const std::string &nickname_):MachinePet{nickname_}{} //构造函数 22 std::string talk()const override{ 23 return "mioa wu~"; 24 } //返回猫叫声 25 26 }; 27 28 class PetDog:public MachinePet{ 29 public: 30 PetDog(const std::string &nickname_):MachinePet{nickname_}{} //构造函数 31 std::string talk()const override{ 32 return "wang wang~"; 33 } //返回狗叫声 34 };
1 #include<iostream> 2 #include<memory> 3 #include<vector> 4 #include"Pet.hpp" 5 6 void test1(){ 7 std::vector<MachinePet*>pets; 8 9 pets.push_back(new PetCat("miku")); 10 pets.push_back(new PetDog("da huang")); 11 12 for(MachinePet* ptr:pets){ 13 std::cout<<ptr->get_nickname()<<" says "<<ptr->talk()<<'\n'; 14 delete ptr; 15 } 16 } 17 18 void test2() { 19 std::vector<std::unique_ptr<MachinePet>> pets; 20 21 pets.push_back(std::make_unique<PetCat>("miku")); 22 pets.push_back(std::make_unique<PetDog>("da huang")); 23 24 for(auto const &ptr:pets){ 25 std::cout<<ptr->get_nickname()<<" says "<<ptr->talk()<<'\n'; 26 } 27 } 28 29 void test3(){ 30 const PetCat cat("miku"); 31 std::cout<<cat.get_nickname()<<" say "<<cat.talk()<<'\n'; 32 33 const PetDog dog("da huang"); 34 std::cout<<dog.get_nickname()<<" say "<<dog.talk()<<'\n'; 35 } 36 37 int main(){ 38 std::cout<<"测试1:使用原始指针\n"; 39 test1(); 40 41 std::cout<<"\n测试2:使用智能指针\n"; 42 test2(); 43 44 std::cout<<"\n测试3:直接使用类\n"; 45 test3(); 46 }

task 5:
1 #pragma once 2 3 #include<iostream> 4 5 template<typename T> 6 class Complex{ 7 public: 8 Complex(T real_=0.0,T imag_=0.0):real{real_},imag{imag_}{} //普通构造 9 Complex(const Complex& X){ 10 real=X.real; 11 imag=X.imag; 12 } //复制构造 13 T get_real()const{ 14 return real; 15 } //返回实部 16 T get_imag()const{ 17 return imag; 18 } //返回虚部 19 20 21 Complex<T>& operator+=(const Complex &c){ 22 real+=c.real; 23 imag+=c.imag; 24 25 return *this; 26 } //重载+= 27 28 friend Complex<T> operator+(const Complex &c1,const Complex &c2){ 29 return Complex<T>(c1.real+c2.real,c1.imag+c2.imag); 30 } //重载+ 31 friend bool operator==(const Complex &c1,const Complex &c2){ 32 return (c1.imag==c2.imag&&c1.real==c2.real); 33 } //重载== 34 friend std::istream& operator>>(std::istream &in,Complex &c){ 35 in>>c.real>>c.imag; 36 return in; 37 }//重载>> 38 friend std::ostream& operator<<(std::ostream &out,const Complex &c){ 39 if(c.imag>=0) 40 out<<c.real<<"+"<<c.imag<<"i"; 41 else 42 out<<c.real<<"-"<<-c.imag<<"i"; 43 44 return out; 45 }//重载<< 46 47 private: 48 T real; 49 T imag; 50 51 };
1 #include<iostream> 2 #include"Complex.hpp" 3 4 void test1(){ 5 using std::cout; 6 using std::boolalpha; 7 8 Complex<int>c1(3,-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 20 void test2(){ 21 using std::cin; 22 using std::cout; 23 24 Complex<double>c1,c2; 25 cout<<"Enter c1 and c2:"; 26 cin>>c1>>c2; 27 cout<<"c1="<<c1<<'\n'; 28 cout<<"c2="<<c2<<'\n'; 29 30 const Complex<double>c3(c1); 31 cout<<"c3.real="<<c3.get_real()<<'\n'; 32 cout<<"c3.imag="<<c3.get_imag()<<'\n'; 33 34 } 35 36 int main(){ 37 std::cout<<"自定义类模板Complex测试1:\n"; 38 test1(); 39 40 std::cout<<"\n自定义模板Complex测试2:\n"; 41 test2(); 42 43 }


浙公网安备 33010602011771号