实验五
任务一:
1.源代码:
(1)publisher.hpp:
#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; // 音乐艺术家名称 };
(2)publisher.cpp:
#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'; }
(3)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(); }
(注:由于使用编译器是DevC++,版本较低,导致不能够使用注释掉的部分(智能指针部分));
2.运行代码测试结果(不含注释部分):

3.回答问题:
问题一:
(1)就实际类功能而言,publisher没有实际含义,就代码部分而言,由于析构函数和接口为纯虚函数而导致其为抽象类;
(2)不能;因为抽象类不能实例化;
问题二:
(1)void publish() const override; // 接口
void use() const override; // 接口;
(2)prototype for 'void Film::publish()'does not match any in class 'Film';也就是没有该函数声明,因为在原类成员函数中,对应的函数是有const声明的。
问题三:
(1)Publisher 类型的指针;
(2)Book,Film,Music;
(3)防止产生内存泄漏,也就是在使用基类指针删除派生类对象时,能够让派生类对象资源释放;会造成内存泄漏,派生类对象没能释放对应的资源;
任务二:
1.源代码:
(1)book.hpp:
#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; // 定价 };
(2)book.cpp:
#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; }
(3)booksale.hpp:
#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; // 销售数量 };
(4)booksale.cpp:
#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; }
(5)task2.cpp:
#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; // 销售数量 };
2.测试代码运行结果:
3.回答问题:
问题一:
(1)两处;friend std::ostream& operator<<(std::ostream &out, const Book &book)此处用于Book类型;friend std::ostream& operator<<(std::ostream &out, const BookSale &item)用于BookSale类型;
(2)out << item.rb << '\n';(输出Book对象)cout << record << '\n';(输出BookSale对象);
问题二:
(1)使用算法库的sort函数,从迭代器的begin到end,通过函数compare_by_amount函数的逻辑去排序;
(2)sort(sales_records.begin(), sales_records.end(),[](const BookSale& a, const BookSale& b) {
return a.get_amount() < b.get_amount();});
任务三:
1.源代码:
(1)task3_1.cpp:
#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(); }
(2)task3_2:
#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(); }
2.测试代码运行结果:
(1)task3_1.cpp:

(2)task3_2.cpp:

任务四:
(1)pet.hpp:
#pragma once #include <string> #include <iostream> using namespace std; class MachinePet { public: MachinePet(const string& name = " ") { this->name = name; }; virtual string get_nickname() const = 0; virtual string talk() const = 0; virtual ~MachinePet() = default; protected: string name; }; class PetCat : public MachinePet { public: PetCat(const string& name = " ") { this->name = name; } string get_nickname() const override { return this->name; } string talk() const override { string a = "miao wu~"; return a; } ~PetCat() = default; private: string name; }; class PetDog : public MachinePet { public: PetDog(const string& name = " ") { this->name = name; } string get_nickname() const override { return this->name; } string talk() const override { string a = "wang wang~"; return a; } ~PetDog() = default; private: string name; };
(2)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() { // 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(); }
2.测试代码运行结果:

任务五:
1.源代码:
(1)Complex.hpp:
#pragma once #include <iostream> template<typename T> class Complex { public: Complex(T real_ = 0, T image_ = 0) { real = real_; image = image_; } Complex(const Complex& c) { real = c.real; image = c.image; } Complex& operator+= (const Complex<T>& c) { real += c.real; image += c.image; return *this; } double get_real() const { return real; } double get_imag() const { return image; } friend Complex operator+(const Complex<T>& a, const Complex<T>& b) { return Complex(a.real + b.real, a.image + b.image); } friend std::istream& operator >>(std::istream& in,Complex<T>& c) { in >> c.real >> c.image; return in; } friend std::ostream& operator <<(std::ostream& out,const Complex<T>& c) { if (c.image >= 0) { out << c.real << " + " << c.image << "i"; }else out << c.real <<" - " << -c.image << "i"; return out; } friend bool operator ==(const Complex<T>& a, const Complex<T>& b) { return ((a.real == b.real) && (a.image == b.image)); } ~Complex() = default; private: T real; T image; };
(2)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(); }
2.测试代码运行结果:

实验总结:
(1)学会使用operator去重载运算符,其中,特别注意const的对应,在istream中,对操作对象不能加上const;
(2)灵活使用模板函数方式,可以实现泛化类型,以减少多余重复代码的产生,使代码更加简洁;
(3)合理运用智能指针可以减少对于泄漏内存的疏忽,使得代码更加安全,也可以让程序员更加注重业务逻辑,提高了开发效率;

浙公网安备 33010602011771号