实验5

任务1
 1 1 #include <memory>
 2  2 #include <iostream>
 3  3 #include <vector>
 4  4 #include "publisher.hpp"
 5  5 void test1() {
 6  6 std::vector<Publisher *> v;
 7  7 v.push_back(new Book("Harry Potter", "J.K. Rowling"));
 8  8 v.push_back(new Film("The Godfather", "Francis Ford Coppola"));
 9  9 v.push_back(new Music("Blowing in the wind", "Bob Dylan"));
10 10 for(Publisher *ptr: v) {
11 11 ptr->publish();
12 12 ptr->use();
13 13 std::cout << '\n';
14 14 delete ptr;
15 15     }
16 16 }
17 17 /*void test2() {
18 18 std::vector<std::unique_ptr<Publisher>> v;
19 19 v.push_back(std::make_unique<Book>("Harry Potter", "J.K. Rowling"));
20 20 v.push_back(std::make_unique<Film>("The Godfather", "Francis Ford Coppola"));
21 21 v.push_back(std::make_unique<Music>("Blowing in the wind", "Bob Dylan"));
22 22 for(const auto &ptr: v) {
23 23 ptr->publish();
24 24 ptr->use();
25 25 std::cout << '\n';
26 26     }
27 27 }*/
28 28 void test3() {
29 29 Book book("A Philosophy of Software Design", "John Ousterhout");
30 30 book.publish();
31 31 book.use();
32 32 }
33 33 int main() {
34 34 std::cout << "运行时多态:纯虚函数、抽象类\n";
35 35 std::cout << "\n测试1: 使用原始指针\n";
36 36 test1();
37 37 //std::cout << "\n测试2: 使用智能指针\n";
38 38 //test2();
39 39 std::cout << "\n测试3: 直接使用类\n";
40 40 test3();
41 41 }
View Code

螢幕擷取畫面 2025-12-12 145403

 

 

 

任务2

 1 1 #include "booksale.hpp"
 2  2 #include <iostream>
 3  3 #include <string>
 4  4 #include <vector>
 5  5 #include <algorithm>
 6  6 // 按图书销售数额比较
 7  7 bool compare_by_amount(const BookSale &x1, const BookSale &x2) {
 8  8 return x1.get_amount() > x2.get_amount();
 9  9 }
10 10 void test() {
11 11 using namespace std;
12 12  vector<BookSale> sales_lst;         // 存放图书销售记录
13 13      int books_number;
14 14     cout << "录入图书数量: ";
15 15     cin >> books_number;
16 16     cout << "录入图书销售记录" << endl;
17 17     for(int i = 0; i < books_number; ++i) {
18 18         string name, author, translator, isbn;
19 19         float price;
20 20         cout << string(20, '-') << "" << i+1 << "本图书信息录入" << string(20, '-') << 
21 21 endl;
22 22         cout << "录入书名: "; cin >> name;
23 23         cout << "录入作者: "; cin >> author;
24 24         cout << "录入译者: "; cin >> translator;
25 25         cout << "录入isbn: "; cin >> isbn;
26 26         cout << "录入定价: "; cin >> price;
27 27         Book book(name, author, translator, isbn, price);
28 28         float sales_price;
29 29         int sales_amount;
30 30         cout << "录入售价: "; cin >> sales_price;
31 31         cout << "录入销售数量: "; cin >> sales_amount;
32 32         BookSale record(book, sales_price, sales_amount);
33 33         sales_lst.push_back(record);
34 34     }
35 35     // 按销售册数排序
36 36     sort(sales_lst.begin(), sales_lst.end(), compare_by_amount);
37 37     // 按销售册数降序输出图书销售信息
38 38     cout << string(20, '=') <<  "图书销售统计" << string(20, '=') << endl;
39 39     for(auto &t: sales_lst) {
40 40         cout << t << endl;
41 41         cout << string(40, '-') << endl;
42 42     }
43 43 }
44 44 int main() {
45 45     test();
46 46 }
View Code

螢幕擷取畫面 2025-12-12 145515

 

 

 

任务3

 1 1 #include <iostream>
 2  2 // 类A的定义
 3  3 class A {
 4  4 public:
 5  5 A(int x0, int y0);
 6  6 void display() const;
 7  7 private:
 8  8 int x, y;
 9  9 };
10 10 A::A(int x0, int y0): x{x0}, y{y0} {
11 11 }
12 12 void A::display() const {
13 13 std::cout << x << ", " << y << '\n';
14 14 }
15 15 // 类B的定义
16 16 class B {
17 17 public:
18 18 B(double x0, double y0);
19 19 void display() const;
20 20 private:
21 21 double x, y;
22 22 };
23 23 B::B(double x0, double y0): x{x0}, y{y0} {
24 24 }
25 25 void B::display() const {
26 26 std::cout << x << ", " << y << '\n';
27 27 }
28 28 void test() {
29 29 std::cout << "测试类A: " << '\n';
30 30 A a(3, 4);
31 31 a.display();
32 32 std::cout << "\n测试类B: " << '\n';
33 33 B b(3.2, 5.6);
34 34 b.display();
35 35 }
36 36 int main() {
37 37 test();
38 38 }
View Code

螢幕擷取畫面 2025-12-12 145609

 

 

 

任务4

 1 1 #include <iostream>
 2  2 #include <memory>
 3  3 #include <vector>
 4  4 #include "pets.hpp"
 5  5 void test1() {
 6  6 std::vector<MachinePet *> pets;
 7  7 pets.push_back(new PetCat("miku"));
 8  8 pets.push_back(new PetDog("da huang"));
 9  9 for(MachinePet *ptr: pets) {
10 10 std::cout << ptr->get_nickname() << " says " << ptr->talk() << '\n';
11 11 delete ptr;  // 须手动释放资源
12 12     }   
13 13 }
14 14 /*void test2() {
15 15 std::vector<std::unique_ptr<MachinePet>> pets;
16 16 pets.push_back(std::make_unique<PetCat>("miku"));
17 17 pets.push_back(std::make_unique<PetDog>("da huang"));
18 18 for(auto const &ptr: pets)
19 19     std::cout << ptr->get_nickname() << " says " << ptr->talk() << '\n';
20 20 }*/
21 21 void test3() {
22 22 // MachinePet pet("little cutie");   // 编译报错:无法定义抽象类对象
23 23 const PetCat cat("miku");
24 24 std::cout << cat.get_nickname() << " says " << cat.talk() << '\n';
25 25 const PetDog dog("da huang");
26 26 std::cout << dog.get_nickname() << " says " << dog.talk() << '\n';
27 27 }
28 28 int main() {
29 29 std::cout << "测试1: 使用原始指针\n";
30 30 test1();
31 31 //std::cout << "\n测试2: 使用智能指针\n";
32 32 //test2();
33 33 std::cout << "\n测试3: 直接使用类\n";
34 34 test3();
35 35 }
View Code

螢幕擷取畫面 2025-12-12 145650

 

 

 

任务5

 1 1 #include <iostream>
 2  2 #include "Complex.hpp"
 3  3 void test1() {
 4  4 using std::cout;
 5  5 using std::boolalpha;
 6  6 Complex<int> c1(2, -5), c2(c1);
 7  7 cout << "c1 = " << c1 << '\n';
 8  8 cout << "c2 = " << c2 << '\n';
 9  9 cout << "c1 + c2 = " << c1 + c2 << '\n';
10 10 c1 += c2;
11 11 cout << "c1 = " << c1 << '\n';
12 12 cout << boolalpha << (c1 == c2) << '\n';
13 13 }
14 14 void test2() {
15 15 using std::cin;
16 16 using std::cout;
17 17 Complex<double> c1, c2;
18 18 cout << "Enter c1 and c2: ";
19 19 cin >> c1 >> c2;
20 20 cout << "c1 = " << c1 << '\n';
21 21 cout << "c2 = " << c2 << '\n';
22 22 const Complex<double> c3(c1);
23 23 cout << "c3.real = " << c3.get_real() << '\n';
24 24 cout << "c3.imag = " << c3.get_imag() << '\n';
25 25 }
26 26 int main() {
27 27 std::cout << "自定义类模板Complex测试1: \n";
28 28 test1();
29 29 std::cout << "\n自定义类模板Complex测试2: \n";
30 30 test2();
31 31 }
View Code

螢幕擷取畫面 2025-12-12 145736

 

posted @ 2025-12-12 14:57  ToffeeMa  阅读(0)  评论(0)    收藏  举报