實驗五:模板類與多型
一.實驗任務1.3
1 #include<iostream> 2 using std::cout; 3 using std::endl; 4 using std::ostream; 5 using std::istream; 6 template <typename T> 7 class Complex { 8 public: 9 Complex(T r = 0, T i = 0) :real{ r }, imag{ i }{} 10 Complex(const Complex<T> &c) : real{ c.real }, imag{ c.imag } {}View Code11 T get_real() const { return real; } 12 T get_imag() const { return imag; } 13 14 Complex<T> &operator+=(const Complex<T> &obj); 15 16 template <typename T1> 17 friend ostream& operator<<(ostream& out, const Complex<T1> &c1);18 19 template <typename T1> 20 friend istream& operator>>(istream& in, Complex<T1> &c1); 21 private: 22 T real,imag; 23 }; 24 25 template <typename T> 26 Complex<T>& Complex<T>::operator+=(const Complex<T>& obj) { 27 real += obj.real;28 imag += obj.imag; 29 return *this; 30 } 31 32 template <typename T> 33 Complex<T> operator+(const Complex<T>& c1, const Complex<T>& c2) 34 { 35 return Complex<T>(c1.get_real() + c2.get_real(), 36 c1.get_imag() + c2.get_imag()); 37 } 38 39 template <typename T> 40 bool operator==(const Complex<T>& c1, const Complex<T>& c2) 41 { 42 return c1.get_real() == c2.get_real() && c1.get_imag() == 43 c2.get_imag(); 44 } 45 46 template <typename T1> 47 ostream& operator<<(ostream& out, const Complex<T1>& c1) { 48 if (c1.imag == 0) 49 out << c1.real; 50 else if (c1.imag > 0) 51 out << c1.real << " + " << c1.imag << "i"; 52 else 53 out << c1.real << " - " << -c1.imag << "i"; 54 return out; 55 } 56 57 template <typename T1> 58 istream& operator>>(istream& in, Complex<T1>& c1) { 59 in >> c1.real >> c1.imag; 60 return in; 61 } 62 int main() { 63 using namespace std; 64 Complex<double> c1(2, 4), c2; 65 cin >> c2; 66 cout << c2.get_real() << " " << c2.get_imag() << endl; 67 c1 += c2; 68 cout << c1; 69 if (c1 == c2) 70 cout << "Yes!"; 71 else cout << "No!"; 72 return 0; 73 }
總結:
自己動手實現的過程發現的錯誤還是很多的:
如果用到標準模板庫中複數模板類compex,在輸入輸出時,都要以 (real, imag) 的格式,一開始不知道這個規則,輸入沒加括號,編譯器自動認為第二個數字是0。
1.3中,template <typename T>不是一勞永逸的 容易忽略的尤其是友元函式
friend ostream& operator<<(ostream& out, const Complex<T> &c1);
friend istream& operator>>(istream& in, Complex<T> &c1);
需要另外宣告template <typename T1>不另外寫T1 也可以,但一定要宣告,否則會報錯無法解析的外部命令。
二,實驗任務2
Person.hpp原始碼
1 #pragma once 2 #include<string> 3 #include<iomanip> 4 #include<iostream> 5 using namespace std; 6 class Person { 7 private: 8 string name; 9 string telephone; 10 string email; 11 public: 12 Person() {}; 13 Person(string n,string t, string e = " "):name{n},telephone{t},email{e}{} 14 void change_telephone(string t){ 15 telephone = t; 16 } 17 void change_email(string e) { 18 email = e; 19 } 20 friend ostream& operator<<(ostream& out, Person& p1); 21 friend istream& operator>>(istream& in, Person& p1); 22 friend bool operator==(Person&p1, Person& p2); 23 }; 24 ostream& operator<<(ostream& out, Person& p1) { 25 out << setiosflags(ios_base::left)<<setw(20)<< p1.name <<setw(20) << p1.telephone <<setw(20)<< p1.email; 26 return out; 27 } 28 29 istream& operator>>(istream& in, Person& p1){ 30 getline(in,p1.name); 31 getline(in, p1.telephone); 32 getline(in, p1.email); 33 in.ignore(numeric_limits<streamsize>::max(), '\n'); 34 return in; 35 } 36 bool operator==(Person& p1, Person& p2) { 37 if (p1.name == p2.name && p1.telephone == p2.telephone) 38 return true; 39 else return false; 40 }View Code
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 }View Code
執行結果截圖
回答問題 這個任務中,插入運算子<<被過載為友元函式。
① 在測試程式碼中, cout << i ,編譯器會將這個表示式轉換成什麼樣的函式呼叫?寫出具體的函 數呼叫形式。
cout<<i會轉換成函式 ostream& operator<<(ostream &out,const Person &i)
② 在測試程式碼中, fout << i ,編譯器會將這個表示式轉換成什麼樣的函式呼叫?寫出具體的函 數呼叫形式。
fout<<i會轉換成函式 ofstream& operater<<(ofstream &fout,const Person &i)
三,實驗任務3
1.container.h
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 #endifView Code
2.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 #include<string> 15 using namespace std; 16 17 enum job {sw, ar, mg}; /* define 3 jobs by enumerate type 18 sword man, archer, mage */ 19 class player 20 { 21 friend void showinfo(player &p1, player &p2); 22 friend class swordsman; 23 24 protected: 25 int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV; 26 // General properties of all characters 27 string name; // character name 28 job role; /* character's job, one of swordman, archer and mage, 29 as defined by the enumerate type */ 30 container bag; // character's inventory 31 32 public: 33 virtual bool attack(player &p)=0; // normal attack 34 virtual bool specialatt(player &p)=0; //special attack 35 virtual void isLevelUp()=0; // level up judgement 36 /* Attention! 37 These three methods are called "Pure virtual functions". 38 They have only declaration, but no definition. 39 The class with pure virtual functions are called "Abstract class", which can only be used to inherited, but not to constructor objects. 40 The detailed definition of these pure virtual functions will be given in subclasses. */ 41 42 void reFill(); // character's HP and MP resume 43 bool death(); // report whether character is dead 44 void isDead(); // check whether character is dead 45 bool useHeal(); // consume heal, irrelevant to job 46 bool useMW(); // consume magic water, irrelevant to job 47 void transfer(player &p); // possess opponent's items after victory 48 void showRole(); // display character's job 49 50 private: 51 bool playerdeath; // whether character is dead, doesn't need to be accessed or inherited 52 }; 53 54 #endifView Code
3.swordsman.h
1 //======================= 2 // swordsman.h 3 //======================= 4 5 // Derived from base class player 6 // For the job Swordsman 7 8 #include "player.h" 9 #include<string> 10 using namespace std; 11 class swordsman : public player // subclass swordsman publicly inherited from base player 12 { 13 public: 14 swordsman(int lv_in = 1, string name_in = "Not Given"); 15 // constructor with default level of 1 and name of "Not given" 16 void isLevelUp(); 17 bool attack (player &p); 18 bool specialatt(player &p); 19 /* These three are derived from the pure virtual functions of base class 20 The definition of them will be given in this subclass. */ 21 void AI(player &p); // Computer opponent 22 };View Code
4.container.cpp
1 //======================= 2 // container.cpp 3 //======================= 4 #include"container.h" 5 #include<iostream> 6 using namespace std; 7 8 // default constructor initialise the inventory as empty 9 container::container() 10 { 11 set(0,0); 12 } 13 14 // set the item numbers 15 void container::set(int heal_n, int mw_n) 16 { 17 numOfHeal=heal_n; 18 numOfMW=mw_n; 19 } 20 21 // get the number of heal 22 int container::nOfHeal() 23 { 24 return numOfHeal; 25 } 26 27 // get the number of magic water 28 int container::nOfMW() 29 { 30 return numOfMW; 31 } 32 33 // display the items; 34 void container::display() 35 { 36 cout<<"Your bag contains: "<<endl; 37 cout<<"Heal(HP+100): "<<numOfHeal<<endl; 38 cout<<"Magic Water (MP+80): "<<numOfMW<<endl; 39 } 40 41 //use heal 42 bool container::useHeal() 43 { 44 numOfHeal--; 45 return 1; // use heal successfully 46 } 47 48 //use magic water 49 bool container::useMW() 50 { 51 numOfMW--; 52 return 1; // use magic water successfully 53 }View Code
5.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 13 14 int main() 15 { 16 string tempName; 17 bool success=0; //flag for storing whether operation is successful 18 cout <<"Please input player's name: "; 19 cin >>tempName; // get player's name from keyboard input 20 player *human; // use pointer of base class, convenience for polymorphism使用基態指標,方便多型 21 int tempJob; // temp choice for job selection 22 do 23 { 24 cout <<"Please choose a job: 1 Swordsman, 2 Archer, 3 Mage"<<endl; 25 cin>>tempJob; 26 system("cls"); // clear the screen 27 switch(tempJob) 28 { 29 case 1: 30 human=new swordsman(1,tempName); 31 // create the character with user inputted name and job 32 success=1; // operation succeed 33 break; 34 default: 35 human = new swordsman(1, "0"); 36 break; // In this case, success=0, character creation failed 37 } 38 }while(success!=1); // so the loop will ask user to re-create a character 39 40 int tempCom; // temp command inputted by user 41 int nOpp=0; // the Nth opponent 42 for(int i=1;nOpp<5;i+=2) // i is opponent's level 43 { 44 nOpp++; 45 system("cls"); 46 cout<<"STAGE" <<nOpp<<endl; 47 cout<<"Your opponent, a Level "<<i<<" Swordsman."<<endl; 48 system("pause"); 49 swordsman enemy(i, "Warrior"); // Initialise an opponent, level i, name "Junior" 50 human->reFill(); // get HP/MP refill before start fight 51 52 while(!human->death() && !enemy.death()) // no died 53 { 54 success=0; 55 while (success!=1) 56 { 57 showinfo(*human,enemy); // show fighter's information 58 cout<<"Please give command: "<<endl; 59 cout<<"1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game"<<endl; 60 cin>>tempCom; 61 switch(tempCom) 62 { 63 case 0: 64 cout<<"Are you sure to exit? Y/N"<<endl; 65 char temp; 66 cin>>temp; 67 if(temp=='Y'||temp=='y') 68 return 0; 69 else 70 break; 71 case 1: 72 success=human->attack(enemy); 73 human->isLevelUp(); 74 enemy.isDead(); 75 break; 76 case 2: 77 success=human->specialatt(enemy); 78 human->isLevelUp(); 79 enemy.isDead(); 80 break; 81 case 3: 82 success=human->useHeal(); 83 break; 84 case 4: 85 success=human->useMW(); 86 break; 87 default: 88 break; 89 } 90 } 91 if(!enemy.death()) // If AI still alive 92 enemy.AI(*human); 93 else // AI died 94 { 95 cout<<"YOU WIN"<<endl; 96 human->transfer(enemy); // player got all AI's items 97 } 98 if (human->death()) 99 { 100 system("cls"); 101 cout<<endl<<setw(50)<<"GAME OVER"<<endl; 102 delete human; // player is dead, program is getting to its end, what should we do here? 103 system("pause"); 104 return 0; 105 } 106 } 107 } 108 delete human; // You win, program is getting to its end, what should we do here? 109 system("cls"); 110 cout<<"Congratulations! You defeated all opponents!!"<<endl; 111 system("pause"); 112 return 0; 113 }View Code
6.player.cpp
1 //======================= 2 // player.cpp 3 //======================= 4 5 #include"player.h" 6 #include<iostream> 7 using namespace std; 8 // character's HP and MP resume 9 void player::reFill() 10 { 11 HP=HPmax; // HP and MP fully recovered 12 MP=MPmax; 13 } 14 15 // report whether character is dead 16 bool player::death() 17 { 18 return playerdeath; 19 } 20 21 // check whether character is dead 22 void player::isDead() 23 { 24 if(HP<=0) // HP less than 0, character is dead 25 { 26 cout<<name<<" is Dead." <<endl; 27 system("pause"); 28 playerdeath=1; // give the label of death value 1 29 } 30 } 31 32 // consume heal, irrelevant to job 33 bool player::useHeal() 34 { 35 if(bag.nOfHeal()>0) 36 { 37 HP=HP+100; 38 if(HP>HPmax) // HP cannot be larger than maximum value 39 HP=HPmax; // so assign it to HPmax, if necessary 40 cout<<name<<" used Heal, HP increased by 100."<<endl; 41 bag.useHeal(); // use heal 42 system("pause"); 43 return 1; // usage of heal succeed 44 } 45 else // If no more heal in bag, cannot use 46 { 47 cout<<"Sorry, you don't have heal to use."<<endl; 48 system("pause"); 49 return 0; // usage of heal failed 50 } 51 } 52 53 // consume magic water, irrelevant to job 54 bool player::useMW() 55 { 56 if(bag.nOfMW()>0) 57 { 58 MP=MP+100; 59 if(MP>MPmax) 60 MP=MPmax; 61 cout<<name<<" used Magic Water, MP increased by 100."<<endl; 62 bag.useMW(); 63 system("pause"); 64 return 1; // usage of magic water succeed 65 } 66 else 67 { 68 cout<<"Sorry, you don't have magic water to use."<<endl; 69 system("pause"); 70 return 0; // usage of magic water failed 71 } 72 } 73 74 // possess opponent's items after victory 75 void player::transfer(player &p) 76 { 77 cout<<name<<" got"<<p.bag.nOfHeal()<<" Heal, and "<<p.bag.nOfMW()<<" Magic Water."<<endl; 78 system("pause"); 79 bag.set(bag.nOfHeal()+p.bag.nOfHeal(), bag.nOfMW() + p.bag.nOfMW()); 80 // set the character's bag, get opponent's items 81 } 82 83 // display character's job 84 void player::showRole() 85 { 86 switch(role) 87 { 88 case sw: 89 cout<<"Swordsman"; 90 break; 91 case ar: 92 cout<<"Archer"; 93 break; 94 case mg: 95 cout<<"Mage"; 96 break; 97 default: 98 break; 99 } 100 } 101 102 103 // display character's job 104 void showinfo(player& p1, player& p2) 105 { 106 system("cls"); 107 cout<<"##############################################################"<<endl; 108 cout<<"# Player"<<setw(10)<<p1.name<<" LV. "<<setw(3) <<p1.LV 109 <<" # Opponent"<<setw(10)<<p2.name<<" LV. "<<setw(3) <<p2.LV<<" #"<<endl; 110 cout<<"# HP "<<setw(3)<<(p1.HP<=999?p1.HP:999)<<'/'<<setw(3)<<(p1.HPmax<=999?p1.HPmax:999) 111 <<" | MP "<<setw(3)<<(p1.MP<=999?p1.MP:999)<<'/'<<setw(3)<<(p1.MPmax<=999?p1.MPmax:999) 112 <<" # HP "<<setw(3)<<(p2.HP<=999?p2.HP:999)<<'/'<<setw(3)<<(p2.HPmax<=999?p2.HPmax:999) 113 <<" | MP "<<setw(3)<<(p2.MP<=999?p2.MP:999)<<'/'<<setw(3)<<(p2.MPmax<=999?p2.MPmax:999)<<" #"<<endl; 114 cout<<"# AP "<<setw(3)<<(p1.AP<=999?p1.AP:999) 115 <<" | DP "<<setw(3)<<(p1.DP<=999?p1.DP:999) 116 <<" | speed "<<setw(3)<<(p1.speed<=999?p1.speed:999) 117 <<" # AP "<<setw(3)<<(p2.AP<=999?p2.AP:999) 118 <<" | DP "<<setw(3)<<(p2.DP<=999?p2.DP:999) 119 <<" | speed "<<setw(3)<<(p2.speed<=999?p2.speed:999)<<" #"<<endl; 120 cout<<"# EXP"<<setw(7)<<p1.EXP<<" Job: "<<setw(7); 121 p1.showRole(); 122 cout<<" # EXP"<<setw(7)<<p2.EXP<<" Job: "<<setw(7); 123 p2.showRole(); 124 cout<<" #"<<endl; 125 cout<<"--------------------------------------------------------------"<<endl; 126 p1.bag.display(); 127 cout<<"##############################################################"<<endl; 128 }View Code
7.swordsman.cpp
1 //======================= 2 // swordsman.cpp 3 //======================= 4 #include"swordsman.h" 5 #include<iostream> 6 // constructor. default values don't need to be repeated here 7 8 swordsman::swordsman(int lv_in, string name_in) 9 { 10 role = sw; // enumerate type of job 11 LV = lv_in; 12 name = name_in; 13 14 // Initialising the character's properties, based on his level 15 HPmax = 150 + 8 * (LV - 1); // HP increases 8 point2 per level 16 HP = HPmax; 17 MPmax = 75 + 2 * (LV - 1); // MP increases 2 points per level 18 MP = MPmax; 19 AP = 25 + 4 * (LV - 1); // AP increases 4 points per level 20 DP = 25 + 4 * (LV - 1); // DP increases 4 points per level 21 speed = 25 + 2 * (LV - 1); // speed increases 2 points per level 22 23 playerdeath = 0; 24 EXP = LV * LV * 75; 25 bag.set(lv_in, lv_in); 26 } 27 28 29 void swordsman::isLevelUp() 30 { 31 if(EXP>=LV*LV*75) 32 { 33 LV++; 34 AP+=4; 35 DP+=4; 36 HPmax+=8; 37 MPmax+=2; 38 speed+=2; 39 cout<<name<<" Level UP!"<<endl; 40 cout<<"HP improved 8 points to "<<HPmax<<endl; 41 cout<<"MP improved 2 points to "<<MPmax<<endl; 42 cout<<"Speed improved 2 points to "<<speed<<endl; 43 cout<<"AP improved 4 points to "<<AP<<endl; 44 cout<<"DP improved 5 points to "<<DP<<endl; 45 system("pause"); 46 isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp 47 } 48 } 49 50 bool swordsman::attack(player &p) 51 { 52 double HPtemp=0; // opponent's HP decrement 53 double EXPtemp=0; // player obtained exp 54 double hit=1; // attach factor, probably give critical attack 55 srand((unsigned)time(NULL)); // generating random seed based on system time 56 57 // If speed greater than opponent, you have some possibility to do double attack 58 if ((speed>p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100 59 { 60 HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance 61 cout<<name<<"'s quick strike hit "<<p.name<<", "<<p.name<<"'s HP decreased "<<HPtemp<<endl; 62 p.HP=int(p.HP-HPtemp); 63 EXPtemp=(int)(HPtemp*1.2); 64 } 65 66 // If speed smaller than opponent, the opponent has possibility to evade 67 if ((speed<p.speed) && (rand()%50<1)) 68 { 69 cout<<name<<"'s attack has been evaded by "<<p.name<<endl; 70 system("pause"); 71 return 1; 72 } 73 74 // 10% chance give critical attack 75 if (rand()%100<=10) 76 { 77 hit=1.5; 78 cout<<"Critical attack: "; 79 } 80 81 // Normal attack 82 HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); 83 cout<<name<<" uses bash, "<<p.name<<"'s HP decreases "<<HPtemp<<endl; 84 EXPtemp=(int)(EXPtemp+HPtemp*1.2); 85 p.HP=(int)(p.HP-HPtemp); 86 cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl; 87 EXP=(int)(EXP+EXPtemp); 88 system("pause"); 89 return 1; // Attack success 90 } 91 92 bool swordsman::specialatt(player &p) 93 { 94 if(MP<40) 95 { 96 cout<<"You don't have enough magic points!"<<endl; 97 system("pause"); 98 return 0; // Attack failed 99 } 100 else 101 { 102 MP-=40; // consume 40 MP to do special attack 103 104 //10% chance opponent evades 105 if(rand()%100<=10) 106 { 107 cout<<name<<"'s leap attack has been evaded by "<<p.name<<endl; 108 system("pause"); 109 return 1; 110 } 111 112 double HPtemp=0; 113 double EXPtemp=0; 114 //double hit=1; 115 //srand(time(NULL)); 116 HPtemp=(int)(AP*1.2+20); // not related to opponent's DP 117 EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience 118 cout<<name<<" uses leap attack, "<<p.name<<"'s HP decreases "<<HPtemp<<endl; 119 cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl; 120 p.HP=(int)(p.HP-HPtemp); 121 EXP=(int)(EXP+EXPtemp); 122 system("pause"); 123 } 124 return 1; // special attack succeed 125 } 126 127 // Computer opponent 128 void swordsman::AI(player &p) 129 { 130 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))) 131 // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round 132 { 133 useHeal(); 134 } 135 else 136 { 137 if(MP>=40 && HP>0.5*HPmax && rand()%100<=30) 138 // AI has enough MP, it has 30% to make special attack 139 { 140 specialatt(p); 141 p.isDead(); // check whether player is dead 142 } 143 else 144 { 145 if (MP<40 && HP>0.5*HPmax && bag.nOfMW()) 146 // Not enough MP && HP is safe && still has magic water 147 { 148 useMW(); 149 } 150 else 151 { 152 attack(p); // normal attack 153 p.isDead(); 154 } 155 } 156 } 157 }View Code
四,實驗總結
1,實驗任務二中,過載>>時,一開始沒理解,只是機械的複製已有的out格式,輸入不了。後來將函式型別改成istream,才沒有報錯。
Ostream中的const不要忘記加,這樣可以起到保護作用。
格式控制中,setw()只能控制這之後一次的輸出。
2,實驗任務三中,從遊戲的編寫中學到了不少新的知識。
首先要吐槽的是,可能是我太太太菜了,第二關沒通關過,遊戲體驗極差!想給自己初始化多一點回血道具和藍buff。