实验5
实验任务1
#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", "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
(1)
是因为至少含有一个纯虚函数
publisher.hpp 第12、13行
virtual void publish() const = 0;
virtual void use() const = 0;
(2)
不能,抽象类不能实例化
问题2:
(1)
void publish() const override;
void use() const override;
(2)
error: looser throw specifier for ‘virtual void Film::publish()’
error: overriding ‘virtual void Publisher::publish() const’
问题3:
(1)
是Publisher
(2)
Book → Film → Music
(3)
为了让通过基类指针删除派生类对象时,先调用派生类析构函数,再调用基类析构函数,防止内存泄漏。
若去掉virtual,将只调用Publisher的析构函数,造成Book/Film/Music部分资源泄漏
实验任务2
#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; // 定价 };
#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; }
#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 "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 "booksale.hpp" #include <iostream> #include <string> #include <vector> #include <algorithm> // 按图书销售数额比较 bool compare_by_amount(const BookSale &x1, const BookSale &x2) { return x1.get_amount() > x2.get_amount(); } void test() { using namespace std; vector<BookSale> sales_lst; // 存放图书销售记录 int books_number; cout << "录入图书数量: "; cin >> books_number; cout << "录入图书销售记录" << endl; for(int i = 0; i < books_number; ++i) { string name, author, translator, isbn; float price; cout << string(20, '-') << "第" << i+1 << "本图书信息录入" << string(20, '-') << endl; cout << "录入书名: "; cin >> name; cout << "录入作者: "; cin >> author; cout << "录入译者: "; cin >> translator; cout << "录入isbn: "; cin >> isbn; cout << "录入定价: "; cin >> price; Book book(name, author, translator, isbn, price); float sales_price; int sales_amount; cout << "录入售价: "; cin >> sales_price; cout << "录入销售数量: "; cin >> sales_amount; BookSale record(book, sales_price, sales_amount); sales_lst.push_back(record); } // 按销售册数排序 sort(sales_lst.begin(), sales_lst.end(), compare_by_amount); // 按销售册数降序输出图书销售信息 cout << string(20, '=') << "图书销售统计" << string(20, '=') << endl; for(auto &t: sales_lst) { cout << t << endl; cout << string(40, '-') << endl; } } int main() { test(); }

问题1:
(1)
2处
book.cpp中,重载了std::ostream& operator<<(std::ostream &out, const Book &book);输出book类型对象
booksale.cpp中,重载了std::ostream& operator<<(std::ostream &out, const Booksale &item);输出booksale类型对象
(2)
// 位于 task2.cpp 第 49–51 行
for (auto &t : sales_lst)
cout << t << endl; // t 为 Booksale 对象,调用②号重载
// 位于 booksale.cpp 第 26 行
out << item.rb << '\n'; // 输出 Booksale 内部的 Book 对象,调用①号重载
问题2:
(1)
通过定义比较函数和调用标准库算法
bool compare_by_amount(const Booksale &x1, const Booksale &x2) { return x1.get_amount() > x2.get_amount(); // 降序 }
sort(sales_lst.begin(), sales_lst.end(), compare_by_amount);
(2)
直接把 compare_by_amount 换成 lambda 即可
sort(sales_lst.begin(), sales_lst.end(), [](const Booksale &a, const Booksale &b) { return a.get_amount() > b.get_amount(); // 降序 });
实验任务3
#include <iostream> // 类A的定义 class A { public: A(int x0, int y0); void display() const; private: int x, y; }; A::A(int x0, int y0): x{x0}, y{y0} { } void A::display() const { std::cout << x << ", " << y << '\n'; } // 类B的定义 class B { public: B(double x0, double y0); void display() const; private: double x, y; }; B::B(double x0, double y0): x{x0}, y{y0} { } void B::display() const { std::cout << x << ", " << y << '\n'; } void test() { std::cout << "测试类A: " << '\n'; A a(3, 4); a.display(); std::cout << "\n测试类B: " << '\n'; B b(3.2, 5.6); b.display(); } int main() { test(); }
#include <iostream> #include <string> // 定义类模板 template<typename T> class X{ public: X(T x0, T y0); void display(); private: T x, y; }; template<typename T> X<T>::X(T x0, T y0): x{x0}, y{y0} { } template<typename T> void X<T>::display() { std::cout << x << ", " << y << '\n'; } void test() { std::cout << "测试1: 用int实例化类模板X" << '\n'; X<int> x1(3, 4); x1.display(); std::cout << "\n测试2: 用double实例化类模板X" << '\n'; X<double> x2(3.2, 5.6); x2.display(); std::cout << "\n测试3: 用string实例化类模板X" << '\n'; X<std::string> x3("hello", "oop"); x3.display(); } int main() { test(); }
实验任务4
pets
#include <iostream> #include <string> #pragma once using namespace std; class MachinePets { private: string nickname; public: MachinePets(const string& s) : nickname{ s } {} string get_nickname() const { return nickname; } virtual string talk() = 0; }; class PetCats : public MachinePets { public: PetCats(const string& s) : MachinePets{ s } {} string talk() override { return "miao wu~"; } }; class PetDogs : public MachinePets { public: PetDogs(const string& s) : MachinePets(s) {} string talk() override { return "wang wang~"; } };
test
#include <iostream> #include "pets.hpp" void play(MachinePets& obj) { std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl; } void test() { PetCats cat("miku"); PetDogs dog("da huang"); play(cat); play(dog); } int main() { test(); }

实验任务5
Complex.hpp
#ifndef COMPLEX_HPP #define COMPLEX_HPP #include <iostream> template <typename T> class Complex { private: T real_; T imag_; public: /*---- 构造 ----*/ Complex() : real_(0), imag_(0) {} Complex(T r, T i) : real_(r), imag_(i) {} Complex(const Complex& rhs) : real_(rhs.real_), imag_(rhs.imag_) {} /*---- 只读接口 ----*/ T get_real() const { return real_; } T get_imag() const { return imag_; } /*---- 复合赋值 ----*/ Complex& operator+=(const Complex& rhs) { real_ += rhs.real_; imag_ += rhs.imag_; return *this; } /*---- 友元:二元运算符 & IO ----*/ friend Complex operator+(const Complex& lhs, const Complex& rhs) { return Complex(lhs.real_ + rhs.real_, lhs.imag_ + rhs.imag_); } friend bool operator==(const Complex& lhs, const Complex& rhs) { return lhs.real_ == rhs.real_ && lhs.imag_ == rhs.imag_; } friend std::istream& operator>>(std::istream& in, Complex& x) { in >> x.real_ >> x.imag_; return in; } friend std::ostream& operator<<(std::ostream& out, const Complex& x) { out << x.real_; if (x.imag_ >= 0) out << " + " << x.imag_ << 'i'; else out << " - " << -x.imag_ << 'i'; return out; } }; #endif // COMPLEX_HPP
task5.cpp
#ifndef COMPLEX_HPP #define COMPLEX_HPP #include <iostream> template <typename T> class Complex { private: T real_; T imag_; public: /*---- 构造 ----*/ Complex() : real_(0), imag_(0) {} Complex(T r, T i) : real_(r), imag_(i) {} Complex(const Complex& rhs) : real_(rhs.real_), imag_(rhs.imag_) {} /*---- 只读接口 ----*/ T get_real() const { return real_; } T get_imag() const { return imag_; } /*---- 复合赋值 ----*/ Complex& operator+=(const Complex& rhs) { real_ += rhs.real_; imag_ += rhs.imag_; return *this; } /*---- 友元:二元运算符 & IO ----*/ friend Complex operator+(const Complex& lhs, const Complex& rhs) { return Complex(lhs.real_ + rhs.real_, lhs.imag_ + rhs.imag_); } friend bool operator==(const Complex& lhs, const Complex& rhs) { return lhs.real_ == rhs.real_ && lhs.imag_ == rhs.imag_; } friend std::istream& operator>>(std::istream& in, Complex& x) { in >> x.real_ >> x.imag_; return in; } friend std::ostream& operator<<(std::ostream& out, const Complex& x) { out << x.real_; if (x.imag_ >= 0) out << " + " << x.imag_ << 'i'; else out << " - " << -x.imag_ << 'i'; return out; } }; #endif // COMPLEX_HPP

浙公网安备 33010602011771号