实验五

实验任务4
pet.hpp

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<string>
 5 
 6 using namespace std;
 7 
 8 class MachinePets {
 9 public:
10     //MachinePets(){}
11     MachinePets(const string s) :nickname{ s } {}
12     const string get_nickname() { return nickname; }
13     virtual string talk()=0;
14 private:
15     string nickname;
16 };
17 
18 class PetCats :public MachinePets {
19 public:
20     PetCats(const string s) :MachinePets{ s } {}
21     string talk() { return "miao wu~"; }
22 };
23 
24 class PetDogs :public MachinePets {
25 public:
26     PetDogs(const string s) :MachinePets{ s } {}
27     string talk() { return "wang wang~"; }
28 };

 

task4.cpp

 1 #include <iostream>
 2 #include "pets.hpp"
 3 
 4 void play(MachinePets& obj) {
 5     std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl;
 6 }
 7 
 8 void test() {
 9     PetCats cat("miku");
10     PetDogs dog("da huang");
11 
12     play(cat);
13     play(dog);
14 }
15 
16 int main() {
17     test();
18 }

 

 

 

 实验任务五

Person.hpp

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<string>
 5 #include<iomanip>
 6 
 7 using namespace std;
 8 
 9 class Person {
10 public:
11     Person():name{""},telephone{""},email{""}{}
12     Person(const string a,const string b):name{a},telephone{b},email{""}{}
13     Person(const Person& obj);
14     ~Person() = default;
15 
16     void update_telephone() {
17         cout << "enter the telephone number:" << endl;
18         string t;
19         cin.clear();
20         if (cin >> t) {
21             cin >> telephone;
22             cout << "telephone number has been updated..." << endl;
23         }
24         
25     }
26     void update_email() {
27         cout<<"enter the email address:" << endl;
28         string t;
29         cin.clear();
30         if (cin >> t) {
31             cin >> email;
32             cout << "email address has been updated..." << endl;
33         }
34         
35     }
36     friend ostream& operator<<(ostream& out, const Person& p);
37     friend istream& operator>>(istream& in, Person& p);
38     friend bool operator==(const Person& p1, const Person& p2);
39 private:
40     string name;
41     string telephone;
42     string email;
43 };
44 
45 Person::Person(const Person& obj) {
46     name = obj.name;
47     telephone = obj.telephone;
48     email = obj.email;
49 }
50 
51 ostream& operator<<(ostream& out, const Person& p) {
52     out << p.name;
53     out << setw(12) << p.telephone;
54     out<< setw(12) << p.email;
55     return out;
56 }
57 
58 istream& operator>>(istream& in, Person& p) {
59     in >> p.name;
60     in >> p.telephone;
61     in >> p.email;
62     return in;
63 }
64 
65 bool operator==(const Person& p1, const Person& p2) {
66     return (p1.name == p2.name && p1.telephone == p2.telephone);
67 } 

task5.cpp

 1 #include <iostream>
 2 #include <fstream>
 3 #include <vector>
 4 #include "Person.hpp"
 5 
 6 void test() {
 7     using namespace std;
 8 
 9     vector<Person> phone_book;
10     Person p;
11 
12     cout << "Enter person's contact until press Ctrl + Z" << endl;
13     while (cin >> p)
14         phone_book.push_back(p);
15 
16     cout << "\nupdate someone's contact: \n";
17     phone_book.at(0).update_telephone();
18     phone_book.at(0).update_email();
19 
20     cout << "\ndisplay all contacts' info\n";
21     for (auto& phone : phone_book)
22         cout << phone << endl;
23 
24     cout << "\ntest whether the same contact\n";
25     cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl;
26 }
27 
28 int main() {
29     test();
30 }

 

 

 

 实验任务六

 1 //=======================
 2 //        container.h
 3 //=======================
 4 
 5 // The so-called inventory of a player in RPG games
 6 // contains two items, heal and magic water
 7 
 8 #ifndef    _CONTAINER    // Conditional compilation
 9 #define _CONTAINER
10 
11 class container        // Inventory
12 {
13 protected:
14     int numOfHeal;            // number of heal
15     int numOfMW;            // number of magic water
16 public:
17     container();            // constuctor
18     void set(int heal_n, int mw_n);    // set the items numbers
19     int nOfHeal();            // get the number of heal
20     int nOfMW();            // get the number of magic water
21     void display();            // display the items;
22     bool useHeal();            // use heal
23     bool useMW();            // use magic water
24 };
25 
26 #endif
 1 //=======================
 2 //        container.cpp
 3 //=======================
 4 
 5 // default constructor initialise the inventory as empty
 6 container::container()
 7 {
 8     set(0,0);
 9 }
10 
11 // set the item numbers
12 void container::set(int heal_n, int mw_n)
13 {
14     numOfHeal=heal_n;
15     numOfMW=mw_n;
16 }
17 
18 // get the number of heal
19 int container::nOfHeal()
20 {
21     return numOfHeal;
22 }
23 
24 // get the number of magic water
25 int container::nOfMW()
26 {
27     return numOfMW;
28 }
29 
30 // display the items;
31 void container::display()
32 {
33     cout<<"Your bag contains: "<<endl;
34     cout<<"Heal(HP+100): "<<numOfHeal<<endl;
35     cout<<"Magic Water (MP+80): "<<numOfMW<<endl;
36 }
37 
38 //use heal
39 bool container::useHeal()
40 {
41     numOfHeal--;
42     return 1;        // use heal successfully
43 }
44 
45 //use magic water
46 bool container::useMW()
47 {
48     numOfMW--;
49     return 1;        // use magic water successfully
50 }

 

posted @ 2022-11-29 23:40  Z`x  阅读(19)  评论(0编辑  收藏  举报