1. 程式人生 > 其它 >實驗5 模板類與多型

實驗5 模板類與多型

實驗任務1

1、Complex類的運算子過載

2、自定義複數模板類Complex

實驗任務2

模擬電話薄的資訊新增、儲存

<Person.hpp>

 1 #ifndef PERSON_HPP
 2 #define PERSON_HPP
 3 
 4 using namespace std;
 5 #include <iostream>
 6 #include <iomanip>
 7 
 8 class Person
 9 {
10 public:
11     Person();
12     Person(string n, string t, string
e = " ") : name(n), telephone(t), email(e) {} 13 void change_phone(string newphone) { telephone = newphone; } 14 void change_email(string newemail) { email = newemail; } 15 16 friend ostream &operator<<(ostream &os, const Person &p); 17 friend istream &operator
>>(istream &is, Person &p); 18 friend bool operator==(const Person &p1, const Person &p2); 19 20 private: 21 string name; 22 string telephone; 23 string email; 24 }; 25 Person::Person() 26 { 27 name = "yaobai"; 28 telephone = "201983300444"; 29 email = "
[email protected]"; 30 } 31 32 ostream &operator<<(ostream &os, const Person &p) 33 { 34 os.setf(ios::left); 35 os << setw(15) << p.name 36 << setw(15) << p.telephone 37 << setw(15) << p.email; 38 return os; 39 } 40 istream &operator>>(istream &is, Person &p) 41 { 42 is >> p.name >> p.telephone >> p.email; 43 return is; 44 } 45 bool operator==(const Person &p1, const Person &p2) 46 { 47 return (p1.name == p2.name && p1.telephone == p2.telephone); 48 } 49 50 #endif

<task2.cpp>

 1 #include <iostream>
 2 #include <fstream>
 3 #include <vector>
 4 #include "Person.hpp"
 5 
 6 int main()
 7 {
 8     using namespace std;
 9 
10     vector<Person> phone_book;
11     Person p;
12 
13     while (cin >> p)
14         phone_book.push_back(p);
15 
16     for (auto &i : phone_book)
17         cout << i << endl;
18 
19     cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl;
20 
21     ofstream fout;
22 
23     fout.open("phone_book.txt");
24 
25     if (!fout.is_open())
26     {
27         cerr << "fail to open file phone_book.txt\n";
28         return 1;
29     }
30 
31     for (auto &i : phone_book)
32         fout << i << endl;
33 
34     fout.close();
35 }

測試結果:

檔案內容:

回答問題

這個任務中,插入運算子<<被過載為友元函式。

① 在測試程式碼中, cout << i ,編譯器會將這個表示式轉換成什麼樣的函式呼叫?寫出具體的函式呼叫形式。

operator<<(cout,i)

② 在測試程式碼中, fout << i ,編譯器會將這個表示式轉換成什麼樣的函式呼叫?寫出具體的函式呼叫形式。

operaotr<<(fout,i)

實驗任務3

簡單RPG遊戲的補足

<container.hpp>

 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 //1_?????????????
 8 #ifndef _CONTAINER        // Conditional compilation
 9 #define _CONTAINER
10 
11 class container        // Inventory
12 {
13     friend class player; //ąăÓÚplayerŔŕˇĂÎĘnumOfHealŁŹnumOfMW
14 protected:
15     int numOfHeal;            // number of heal
16     int numOfMW;            // number of magic water
17 public:
18     container();            // constuctor
19     void set(int heal_n, int mw_n);    // set the items numbers
20     int nOfHeal();            // get the number of heal
21     int nOfMW();            // get the number of magic water
22     void display();            // display the items;
23     bool useHeal();            // use heal
24     bool useMW();            // use magic water
25 };
26 #endif

<container.cpp>

 1 //=======================
 2 //        container.cpp
 3 //=======================
 4 
 5 // default constructor initialise the inventory as empty
 6 #include "container.h"
 7 #include <iostream>
 8 using std::cout;
 9 using std::endl;
10 container::container()
11 {
12     set(0, 0);
13 }
14 
15 // set the item numbers
16 void container::set(int heal_n, int mw_n)
17 {
18     numOfHeal = heal_n;
19     numOfMW = mw_n;
20 }
21 
22 // get the number of heal
23 int container::nOfHeal()
24 {
25     return numOfHeal;
26 }
27 
28 // get the number of magic water
29 int container::nOfMW()
30 {
31     return numOfMW;
32 }
33 
34 // display the items;
35 void container::display()
36 {
37     cout << "Your bag contains: " << endl;
38     cout << "Heal(HP+100): " << numOfHeal << endl;
39     cout << "Magic Water (MP+80): " << numOfMW << endl;
40 }
41 
42 // use heal
43 bool container::useHeal()
44 {
45     // 2_????????
46     numOfHeal--;
47     return 1; // use heal successfully
48 }
49 
50 // use magic water
51 bool container::useMW()
52 {
53     numOfMW--;
54     return 1; // use magic water successfully
55 }

<player.h>

 1 //=======================
 2 //        player.h
 3 //=======================
 4 
 5 // The base class of player
 6 // including the general properties and methods related to a character
 7 
 8 #ifndef _PLAYER
 9 #define _PLAYER
10 
11 #include <iomanip> // use for setting field width
12 #include <time.h>  // use for generating random factor
13 #include "container.h"
14 
15 using std::string;
16 
17 enum job
18 {
19     sw,
20     ar,
21     mg
22 }; /* define 3 jobs by enumerate type
23           sword man, archer, mage */
24 class player
25 {
26     friend void showinfo(player &p1, player &p2);
27     friend class swordsman;
28 
29 protected:
30     int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV;
31     // General properties of all characters
32     string name;   // character name
33     job role;       /* character's job, one of swordman, archer and mage,
34                       as defined by the enumerate type */
35     container bag; // character's inventory
36 
37 public:
38     virtual bool attack(player &p) = 0;        // normal attack
39     virtual bool specialatt(player &p) = 0; // special attack
40     virtual void isLevelUp() = 0;            // level up judgement
41     /* Attention!
42     These three methods are called "Pure virtual functions".
43     They have only declaration, but no definition.
44     The class with pure virtual functions are called "Abstract class", which can only be used to inherited, but not to constructor objects.
45     The detailed definition of these pure virtual functions will be given in subclasses. */
46 
47     void reFill();              // character's HP and MP resume
48     bool death();              // report whether character is dead
49     void isDead();              // check whether character is dead
50     bool useHeal();              // consume heal, irrelevant to job
51     bool useMW();              // consume magic water, irrelevant to job
52     void transfer(player &p); // possess opponent's items after victory
53     void showRole();          // display character's job
54 private:
55     bool playerdeath; // whether character is dead, doesn't need to be accessed or inherited
56 };
57 
58 #endif

<player.cpp>

  1 //=======================
  2 //        player.cpp
  3 //=======================
  4 #include "player.h"
  5 #include "container.h"
  6 #include <iostream>
  7 #include <iomanip>
  8 
  9 using namespace std;
 10 
 11 // character's HP and MP resume
 12 void player::reFill()
 13 {
 14     HP = HPmax; // HP and MP fully recovered
 15     MP = MPmax;
 16 }
 17 
 18 // report whether character is dead
 19 bool player::death()
 20 {
 21     return playerdeath;
 22 }
 23 
 24 // check whether character is dead
 25 void player::isDead()
 26 {
 27     if (HP <= 0) // HP less than 0, character is dead
 28     {
 29         cout << name << " is Dead." << endl;
 30         system("pause");
 31         playerdeath = 1; // give the label of death value 1
 32     }
 33 }
 34 
 35 // consume heal, irrelevant to job
 36 bool player::useHeal()
 37 {
 38     if (bag.nOfHeal() > 0)
 39     {
 40         HP = HP + 100;
 41         if (HP > HPmax) // HP cannot be larger than maximum value
 42             HP = HPmax; // so assign it to HPmax, if necessary
 43         cout << name << " used Heal, HP increased by 100." << endl;
 44         bag.useHeal(); // use heal
 45         system("pause");
 46         return 1; // usage of heal succeed
 47     }
 48     else // If no more heal in bag, cannot use
 49     {
 50         cout << "Sorry, you don't have heal to use." << endl;
 51         system("pause");
 52         return 0; // usage of heal failed
 53     }
 54 }
 55 
 56 // consume magic water, irrelevant to job
 57 bool player::useMW()
 58 {
 59     if (bag.nOfMW() > 0)
 60     {
 61         MP = MP + 100;
 62         if (MP > MPmax)
 63             MP = MPmax;
 64         cout << name << " used Magic Water, MP increased by 100." << endl;
 65         bag.useMW();
 66         system("pause");
 67         return 1; // usage of magic water succeed
 68     }
 69     else
 70     {
 71         cout << "Sorry, you don't have magic water to use." << endl;
 72         system("pause");
 73         return 0; // usage of magic water failed
 74     }
 75 }
 76 
 77 // possess opponent's items after victory
 78 void player::transfer(player &p)
 79 {
 80     cout << name << " got" << p.bag.nOfHeal() << " Heal, and " << p.bag.nOfMW() << " Magic Water." << endl;
 81     system("pause");
 82     // 3_???????????
 83     bag.set(bag.numOfHeal + p.bag.nOfHeal(), bag.numOfMW + bag.nOfMW());
 84     // set the character's bag, get opponent's items
 85 }
 86 // display character's job
 87 void player::showRole()
 88 {
 89     switch (role)
 90     {
 91     case sw:
 92         cout << "Swordsman";
 93         break;
 94     case ar:
 95         cout << "Archer";
 96         break;
 97     case mg:
 98         cout << "Mage";
 99         break;
100     default:
101         break;
102     }
103 }
104 
105 // display character's job
106 // 4_??????????????
107 void showinfo(player &p1, player &p2)
108 {
109     system("cls");
110     cout << "##############################################################" << endl;
111     cout << "# Player" << setw(10) << p1.name << "   LV. " << setw(3) << p1.LV
112          << "  # Opponent" << setw(10) << p2.name << "   LV. " << setw(3) << p2.LV << " #" << endl;
113     cout << "# HP " << setw(3) << (p1.HP <= 999 ? p1.HP : 999) << '/' << setw(3) << (p1.HPmax <= 999 ? p1.HPmax : 999)
114          << " | MP " << setw(3) << (p1.MP <= 999 ? p1.MP : 999) << '/' << setw(3) << (p1.MPmax <= 999 ? p1.MPmax : 999)
115          << "     # HP " << setw(3) << (p2.HP <= 999 ? p2.HP : 999) << '/' << setw(3) << (p2.HPmax <= 999 ? p2.HPmax : 999)
116          << " | MP " << setw(3) << (p2.MP <= 999 ? p2.MP : 999) << '/' << setw(3) << (p2.MPmax <= 999 ? p2.MPmax : 999) << "      #" << endl;
117     cout << "# AP " << setw(3) << (p1.AP <= 999 ? p1.AP : 999)
118          << " | DP " << setw(3) << (p1.DP <= 999 ? p1.DP : 999)
119          << " | speed " << setw(3) << (p1.speed <= 999 ? p1.speed : 999)
120          << " # AP " << setw(3) << (p2.AP <= 999 ? p2.AP : 999)
121          << " | DP " << setw(3) << (p2.DP <= 999 ? p2.DP : 999)
122          << " | speed " << setw(3) << (p2.speed <= 999 ? p2.speed : 999) << "  #" << endl;
123     cout << "# EXP" << setw(7) << p1.EXP << " Job: " << setw(7);
124     p1.showRole();
125     cout << "   # EXP" << setw(7) << p2.EXP << " Job: " << setw(7);
126     p2.showRole();
127     cout << "    #" << endl;
128     cout << "--------------------------------------------------------------" << endl;
129     p1.bag.display();
130     cout << "##############################################################" << endl;
131 }

<swordsman.h>

 1 //=======================
 2 //        swordsman.h
 3 //=======================
 4 
 5 // Derived from base class player
 6 // For the job Swordsman
 7 #ifndef _SWORDSMAN
 8 #define _SWORDSMAN
 9 
10 #include "player.h"
11 //5_?????????
12 class swordsman : public player        // subclass swordsman publicly inherited from base player
13 {
14 public:
15     swordsman(int lv_in=1, string name_in="Not Given");    
16         // constructor with default level of 1 and name of "Not given"
17     void isLevelUp();
18     bool attack (player &p);
19     bool specialatt(player &p);
20         /* These three are derived from the pure virtual functions of base class
21            The definition of them will be given in this subclass. */
22     void AI(player &p);                // Computer opponent
23 };
24 #endif

<swordsman.cpp>

  1 //=======================
  2 //        swordsman.cpp
  3 //=======================
  4 
  5 // constructor. default values don't need to be repeated here
  6 
  7 #include "swordsman.h"
  8 #include <iostream>
  9 using std::cout;
 10 using std::endl;
 11 swordsman::swordsman(int lv_in, string name_in)
 12 {
 13     role = sw; // enumerate type of job
 14     LV = lv_in;
 15     name = name_in;
 16 
 17     // Initialising the character's properties, based on his level
 18     HPmax = 150 + 8 * (LV - 1); // HP increases 8 point2 per level
 19     HP = HPmax;
 20     MPmax = 75 + 2 * (LV - 1); // MP increases 2 points per level
 21     MP = MPmax;
 22     AP = 25 + 4 * (LV - 1);       // AP increases 4 points per level
 23     DP = 25 + 4 * (LV - 1);       // DP increases 4 points per level
 24     speed = 25 + 2 * (LV - 1); // speed increases 2 points per level
 25 
 26     playerdeath = 0;
 27     EXP = LV * LV * 75;
 28     bag.set(lv_in, lv_in);
 29 }
 30 
 31 void swordsman::isLevelUp()
 32 {
 33     if (EXP >= LV * LV * 75)
 34     {
 35         LV++;
 36         AP += 4;
 37         DP += 4;
 38         HPmax += 8;
 39         MPmax += 2;
 40         speed += 2;
 41         cout << name << " Level UP!" << endl;
 42         cout << "HP improved 8 points to " << HPmax << endl;
 43         cout << "MP improved 2 points to " << MPmax << endl;
 44         cout << "Speed improved 2 points to " << speed << endl;
 45         cout << "AP improved 4 points to " << AP << endl;
 46         cout << "DP improved 5 points to " << DP << endl;
 47         system("pause");
 48         isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp
 49     }
 50 }
 51 
 52 bool swordsman::attack(player &p)
 53 {
 54     double HPtemp = 0;             // opponent's HP decrement
 55     double EXPtemp = 0;             // player obtained exp
 56     double hit = 1;                 // attach factor, probably give critical attack
 57     srand((unsigned)time(NULL)); // generating random seed based on system time
 58 
 59     // If speed greater than opponent, you have some possibility to do double attack
 60     if ((speed > p.speed) && (rand() % 100 < (speed - p.speed))) // rand()%100 means generates a number no greater than 100
 61     {
 62         HPtemp = (int)((1.0 * AP / p.DP) * AP * 5 / (rand() % 4 + 10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance
 63         cout << name << "'s quick strike hit " << p.name << ", " << p.name << "'s HP decreased " << HPtemp << endl;
 64         p.HP = int(p.HP - HPtemp);
 65         EXPtemp = (int)(HPtemp * 1.2);
 66     }
 67 
 68     // If speed smaller than opponent, the opponent has possibility to evade
 69     if ((speed < p.speed) && (rand() % 50 < 1))
 70     {
 71         cout << name << "'s attack has been evaded by " << p.name << endl;
 72         system("pause");
 73         return 1;
 74     }
 75 
 76     // 10% chance give critical attack
 77     if (rand() % 100 <= 10)
 78     {
 79         hit = 1.5;
 80         cout << "Critical attack: ";
 81     }
 82 
 83     // Normal attack
 84     HPtemp = (int)((1.0 * AP / p.DP) * AP * 5 / (rand() % 4 + 10));
 85     cout << name << " uses bash, " << p.name << "'s HP decreases " << HPtemp << endl;
 86     EXPtemp = (int)(EXPtemp + HPtemp * 1.2);
 87     p.HP = (int)(p.HP - HPtemp);
 88     cout << name << " obtained " << EXPtemp << " experience." << endl;
 89     EXP = (int)(EXP + EXPtemp);
 90     system("pause");
 91     return 1; // Attack success
 92 }
 93 
 94 bool swordsman::specialatt(player &p)
 95 {
 96     if (MP < 40)
 97     {
 98         cout << "You don't have enough magic points!" << endl;
 99         system("pause");
100         return 0; // Attack failed
101     }
102     else
103     {
104         MP -= 40; // consume 40 MP to do special attack
105 
106         // 10% chance opponent evades
107         if (rand() % 100 <= 10)
108         {
109             cout << name << "'s leap attack has been evaded by " << p.name << endl;
110             system("pause");
111             return 1;
112         }
113 
114         double HPtemp = 0;
115         double EXPtemp = 0;
116         // double hit=1;
117         // srand(time(NULL));
118         HPtemp = (int)(AP * 1.2 + 20); // not related to opponent's DP
119         EXPtemp = (int)(HPtemp * 1.5); // special attack provides more experience
120         cout << name << " uses leap attack, " << p.name << "'s HP decreases " << HPtemp << endl;
121         cout << name << " obtained " << EXPtemp << " experience." << endl;
122         p.HP = (int)(p.HP - HPtemp);
123         EXP = (int)(EXP + EXPtemp);
124         system("pause");
125     }
126     return 1; // special attack succeed
127 }
128 
129 // Computer opponent
130 void swordsman::AI(player &p)
131 {
132     if ((HP < (int)((1.0 * p.AP / DP) * p.AP * 1.5)) && (HP + 100 <= 1.1 * HPmax) && (bag.nOfHeal() > 0) && (HP > (int)((1.0 * p.AP / DP) * p.AP * 0.5)))
133     // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
134     {
135         useHeal();
136     }
137     else
138     {
139         if (MP >= 40 && HP > 0.5 * HPmax && rand() % 100 <= 30)
140         // AI has enough MP, it has 30% to make special attack
141         {
142             specialatt(p);
143             p.isDead(); // check whether player is dead
144         }
145         else
146         {
147             if (MP < 40 && HP > 0.5 * HPmax && bag.nOfMW())
148             // Not enough MP && HP is safe && still has magic water
149             {
150                 useMW();
151             }
152             else
153             {
154                 attack(p); // normal attack
155                 p.isDead();
156             }
157         }
158     }
159 }

<main.cpp>

  1 //=======================
  2 //        main.cpp
  3 //=======================
  4 
  5 // main function for the RPG style game
  6 
  7 #include <iostream>
  8 #include <string>
  9 using namespace std;
 10 
 11 #include "swordsman.h"
 12 #include "swordsman.cpp"
 13 #include "player.cpp"
 14 #include "container.cpp"
 15 int main()
 16 {
 17     string tempName;
 18     bool success = 0; // flag for storing whether operation is successful
 19     cout << "Please input player's name: ";
 20     cin >> tempName; // get player's name from keyboard input
 21     player *human;     // use pointer of base class, convenience for polymorphism
 22     int tempJob;     // temp choice for job selection
 23     do
 24     {
 25         cout << "Please choose a job: 1 Swordsman, 2 Archer, 3 Mage" << endl;
 26         cin >> tempJob;
 27         system("cls"); // clear the screen
 28         switch (tempJob)
 29         {
 30         case 1:
 31             human = new swordsman(1, tempName); // create the character with user inputted name and job
 32             success = 1;                        // operation succeed
 33             break;
 34         default:
 35             break; // In this case, success=0, character creation failed
 36         }
 37     } while (success != 1); // so the loop will ask user to re-create a character
 38 
 39     int tempCom;                      // temp command inputted by user
 40     int nOpp = 0;                      // the Nth opponent
 41     for (int i = 1; nOpp < 5; i += 2) // i is opponent's level
 42     {
 43         nOpp++;
 44         system("cls");
 45         cout << "STAGE" << nOpp << endl;
 46         cout << "Your opponent, a Level " << i << " Swordsman." << endl;
 47         system("pause");
 48         swordsman enemy(i, "Warrior"); // Initialise an opponent, level i, name "Junior"
 49         human->reFill();               // get HP/MP refill before start fight
 50 
 51         while (!human->death() && !enemy.death()) // no died
 52         {
 53             success = 0;
 54             while (success != 1)
 55             {
 56                 showinfo(*human, enemy); // show fighter's information
 57                 cout << "Please give command: " << endl;
 58                 cout << "1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game" << endl;
 59                 cin >> tempCom;
 60                 switch (tempCom)
 61                 {
 62                 case 0:
 63                     cout << "Are you sure to exit? Y/N" << endl;
 64                     char temp;
 65                     cin >> temp;
 66                     if (temp == 'Y' || temp == 'y')
 67                         return 0;
 68                     else
 69                         break;
 70                 case 1:
 71                     success = human->attack(enemy);
 72                     human->isLevelUp();
 73                     enemy.isDead();
 74                     break;
 75                 case 2:
 76                     success = human->specialatt(enemy);
 77                     human->isLevelUp();
 78                     enemy.isDead();
 79                     break;
 80                 case 3:
 81                     success = human->useHeal();
 82                     break;
 83                 case 4:
 84                     success = human->useMW();
 85                     break;
 86                 default:
 87                     break;
 88                 }
 89             }
 90             if (!enemy.death()) // If AI still alive
 91                 enemy.AI(*human);
 92             else // AI died
 93             {
 94                 cout << "YOU WIN" << endl;
 95                 human->transfer(enemy); // player got all AI's items
 96             }
 97             if (human->death())
 98             {
 99                 system("cls");
100                 cout << endl
101                      << setw(50) << "GAME OVER" << endl;
102                 // 6_???????????
103                 delete human;
104                 human = nullptr; // player is dead, program is getting to its end, what should we do here?
105                 system("pause");
106                 return 0;
107             }
108         }
109     }
110     // 7_???????????
111     delete human;
112     human = nullptr; // You win, program is getting to its end, what should we do here?
113     system("cls");
114     cout << "Congratulations! You defeated all opponents!!" << endl;
115     system("pause");
116     return 0;
117 }

測試結果:

回合制人機對戰(部分遊戲截圖如下)

普通攻擊:

特殊攻擊:

使用治癒藥水:

血量恢復:

使用魔法藥水:

被擊敗:

遊戲結束:

心得與體會:

1、如果需要訪問保護成員或私有成員,可以在被訪問類中宣告訪問類為friend class,如本實驗在container類中將player類宣告為friend class

2、使用完指向物件的指標後,要作如下處理:

delete 指標指向的物件

使指標指向nullptr

3、基類指標和基類引用可以指向多種派生類,實現多型