OOP实验五
任务一
出版物分类管理
源码
Publisher.hpp
#pragma once
#include <string>
class Publisher {
public:
Publisher(const std::string &name_ = "");
virtual ~Publisher() = default;
virtual void publish() const = 0;
virtual void use() const = 0;
protected:
std::string name;
};
class Book : public Publisher {
public:
Book(const std::string &name_, const std::string &author_ = "");
void publish() const override;
void use() const override;
private:
std::string author;
};
class Film : public Publisher {
public:
Film(const std::string &name_, const std::string &director_ = "");
void publish() const override;
void use() const override;
private:
std::string director;
};
class Music : public Publisher {
public:
Music(const std::string &name_, const std::string &artist_ = "");
void publish() const override;
void use() const override;
private:
std::string artist;
};
Publisher.cpp
#include <iostream>
#include "Publisher.hpp"
Publisher::Publisher(const std::string &name_) : name(name_) {}
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(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(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';
}
task1.cpp
#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:抽象类机制
-
是什么决定了 Publisher 是抽象类?
存在至少一个纯虚函数
代码依据:virtual void publish() const = 0 -
若在 main.cpp 直接写
Publisher p;能否编译通过?不能 抽象类不允许实例化
问题 2:纯虚函数与接口继承
-
Book、Film、Music 必须实现的两个函数:
void publish() const override; void use() const override; -
去掉
const后编译报错:error: no matching function for call to ...
派生类函数签名与基类不完全一致,导致未实现基类纯虚函数
问题 3:运行时多态与虚析构
ptr声明类型:Publisher*- 循环指向的实际对象类型顺序:
Book -> Film -> Music - 析构函数为何
virtual?保证通过基类指针
delete时,能正确调用派生类析构函数
若删除virtual,会导致未定义行为(派生部分资源泄漏
任务二
图书销售统计
源码
book.hpp
#pragma once
#include <string>
#include <iostream>
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; // 定价
};
book.cpp
#include <iomanip>
#include <iostream>
#include <string>
#include "book.hpp"
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;
}
booksale.hpp
#pragma once
#include <string>
#include <iostream>
#include "book.hpp"
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; // 销售数量
};
booksale.cpp
#include <iomanip>
#include <iostream>
#include <string>
#include "booksale.hpp"
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;
}
task2.cpp
#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:重载运算符 <<
-
重载了 2 处:
std::ostream& operator<<(std::ostream&, const Book&)std::ostream& operator<<(std::ostream&, const BookSale&)
-
使用重载 << 的代码:
cout << t << endl; // t 为 BookSale 对象
问题 2:图书销售统计
-
降序排序实现:
std::sort(sales_records.begin(), sales_records.end(), compare_by_amount);其中
compare_by_amount按get_amount()返回的 数量 做 > 比较。 -
匿名函数
std::sort(sales_records.begin(), sales_records.end(), [](const BookSale& a, const BookSale& b) { return a.get_amount() > b.get_amount(); });
任务四
机器宠物
源码
pet.hpp
#pragma once
#include <string>
class MachinePet {
public:
explicit MachinePet(const std::string& nickname_);
virtual ~MachinePet() = default;
const std::string& get_nickname() const { return nickname; }
virtual std::string talk() const = 0;
private:
std::string nickname;
};
inline MachinePet::MachinePet(const std::string& nickname_) : nickname(nickname_) {}
class PetCat : public MachinePet {
public:
using MachinePet::MachinePet;
std::string talk() const override { return "miao wu~"; }
};
class PetDog : public MachinePet {
public:
using MachinePet::MachinePet;
std::string talk() const override { return "wang wang~"; }
};
task4.cpp
#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() {
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();
}
结果

任务五
自定义类模板 Complex
源码
Complex.hpp
#pragma once
#include <iostream>
template <typename T>
class Complex {
public:
Complex() = default;
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;
}
friend Complex operator+(Complex lhs, const Complex& rhs) {
lhs += rhs;
return lhs;
}
friend bool operator==(const Complex& a, const Complex& b) {
return a.real == b.real && a.imag == b.imag;
}
friend std::ostream& operator<<(std::ostream& os, const Complex& c) {
os << c.real;
if (c.imag >= 0) os << '+';
os << c.imag << 'i';
return os;
}
friend std::istream& operator>>(std::istream& is, Complex& c) {
return is >> c.real >> c.imag;
}
private:
T real{};
T imag{};
};
task5.cpp
#include <iostream>
#include "Complex.hpp"
void test1() {
using std::cout;
using std::boolalpha;
Complex<int> c1(2, -5), c2(c1);
cout << "c1 = " << c1 << '\n';
cout << "c2 = " << c2 << '\n';
cout << "c1 + c2 = " << c1 + c2 << '\n';
c1 += c2;
cout << "c1 = " << c1 << '\n';
cout << boolalpha << (c1 == c2) << '\n';
}
void test2() {
using std::cin;
using std::cout;
Complex<double> c1, c2;
cout << "Enter c1 and c2: ";
cin >> c1 >> c2;
cout << "c1 = " << c1 << '\n';
cout << "c2 = " << c2 << '\n';
const Complex<double> c3(c1);
cout << "c3.real = " << c3.get_real() << '\n';
cout << "c3.imag = " << c3.get_imag() << '\n';
}
int main() {
std::cout << "自定义类模板Complex测试1: \n";
test1();
std::cout << "\n自定义类模板Complex测试2: \n";
test2();
}
结果


浙公网安备 33010602011771号