实验五 模板类与多态
Person.hpp
1 #ifndef PERSON_HPP 2 #define PERSON_HPP 3 4 #include<string> 5 #include<iostream> 6 7 using namespace std; 8 9 class Person 10 { 11 public: 12 Person(string n="ole olsen",string t="13352196559",string e="none"):name{n},telephone{t},email{e} {} 13 void change_phone(string t){telephone = t;} 14 void change_email(string e){email = e;} 15 friend ostream& operator<<(ostream &out,Person &p); 16 friend istream& operator>>(istream &in,Person &p); 17 friend bool operator==(Person &p1,Person &p2); 18 private: 19 string name,telephone,email; 20 }; 21 22 ostream &operator<<(ostream &out,Person &p) 23 { 24 out << p.name << " " << p.telephone << " " << p.email; 25 return out; 26 } 27 28 istream &operator>>(istream &in,Person &p) 29 { 30 getline(in,p.name); 31 getline(in,p.telephone); 32 getline(in,p.email); 33 return in; 34 } 35 36 bool operator==(Person &p1,Person &p2) 37 { 38 if(p1.name==p2.name&&p1.telephone==p2.telephone&&p1.email==p2.email) 39 return true; 40 else 41 return false; 42 } 43 44 #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 12 Person p; 13 14 while(cin>>p) 15 phone_book.push_back(p); 16 17 for(auto &i:phone_book) 18 cout << i << endl; 19 20 cout << boolalpha << (phone_book.at(0)==phone_book.at(1)); 21 22 ofstream fout; 23 24 fout.open("phone_book.txt"); 25 26 if(!fout.is_open()) 27 { 28 cerr << "fail to open file phone_book.txt\n"; 29 return 1; 30 } 31 32 for(auto &i:phone_book) 33 fout << i << endl; 34 35 fout.close(); 36 }
运行截图:
问题一回答:ostream &operator<<(ostream &out,Person &p)
{
out << p.name << "" << p.telephone << "" << p.email;
return out;
}
问题二回答:ofstream流的读写操作。
1 //======================= 2 // swordsman.h 3 //======================= 4 5 // Derived from base class player 6 // For the job Swordsman 7 8 #include ";player.h"; 9 class swordsman : public player/Ǜ_????????? // subclass swordsman publicly inherited from base player 10 { 11 public: 12 swordsman(int lv_in=1, string name_in=";Not Given";); 13 // constructor with default level of 1 and name of ";Not given"; 14 void isLevelUp(); 15 bool attack (player &p); 16 bool specialatt(player &p); 17 /* These three are derived from the pure virtual functions of base class 18 The definition of them will be given in this subclass. */ 19 void AI(player &p); // Computer opponent 20 }; 21
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 10 using namespace std; 11 12 swordsman::swordsman(int lv_in, string name_in) 13 { 14 role=sw; // enumerate type of job 15 LV=lv_in; 16 name=name_in; 17 18 // Initialising the character';s properties, based on his level 19 HPmax=150+8*(LV-1); // HP increases 8 point2 per level 20 HP=HPmax; 21 MPmax=75+2*(LV-1); // MP increases 2 points per level 22 MP=MPmax; 23 AP=25+4*(LV-1); // AP increases 4 points per level 24 DP=25+4*(LV-1); // DP increases 4 points per level 25 speed=25+2*(LV-1); // speed increases 2 points per level 26 27 playerdeath=0; 28 EXP=LV*LV*75; 29 bag.set(lv_in, lv_in); 30 } 31 32 void swordsman::isLevelUp() 33 { 34 if(EXP>=LV*LV*75) 35 { 36 LV++; 37 AP+=4; 38 DP+=4; 39 HPmax+=8; 40 MPmax+=2; 41 speed+=2; 42 cout<<name<<"; Level UP!";<<endl; 43 cout<<";HP improved 8 points to ";<<HPmax<<endl; 44 cout<<";MP improved 2 points to ";<<MPmax<<endl; 45 cout<<";Speed improved 2 points to ";<<speed<<endl; 46 cout<<";AP improved 4 points to ";<<AP<<endl; 47 cout<<";DP improved 5 points to ";<<DP<<endl; 48 system(";pause";); 49 isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp 50 } 51 } 52 53 bool swordsman::attack(player &p) 54 { 55 double HPtemp=0; // opponent';s HP decrement 56 double EXPtemp=0; // player obtained exp 57 double hit=1; // attach factor, probably give critical attack 58 srand((unsigned)time(NULL)); // generating random seed based on system time 59 60 // If speed greater than opponent, you have some possibility to do double attack 61 if ((speed>p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100 62 { 63 HPtemp=(int)(Ƒ.0*AP/p.DP)*AP*5/(rand()%4+10)); // opponent';s HP decrement calculated based their AP/DP, and uncertain chance 64 cout<<name<<";';s quick strike hit ";<<p.name<<";, ";<<p.name<<";';s HP decreased ";<<HPtemp<<endl; 65 p.HP=int(p.HP-HPtemp); 66 EXPtemp=(int)(HPtemp*1.2); 67 } 68 69 // If speed smaller than opponent, the opponent has possibility to evade 70 if ((speed<p.speed) && (rand()%50<1)) 71 { 72 cout<<name<<";';s attack has been evaded by ";<<p.name<<endl; 73 system(";pause";); 74 return 1; 75 } 76 77 // 10% chance give critical attack 78 if (rand()%100<=10) 79 { 80 hit=1.5; 81 cout<<";Critical attack: ";; 82 } 83 84 // Normal attack 85 HPtemp=(int)(Ƒ.0*AP/p.DP)*AP*5/(rand()%4+10)); 86 cout<<name<<"; uses bash, ";<<p.name<<";';s HP decreases ";<<HPtemp<<endl; 87 EXPtemp=(int)(EXPtemp+HPtemp*1.2); 88 p.HP=(int)(p.HP-HPtemp); 89 cout<<name<<"; obtained ";<<EXPtemp<<"; experience.";<<endl; 90 EXP=(int)(EXP+EXPtemp); 91 system(";pause";); 92 return 1; // Attack success 93 } 94 95 bool swordsman::specialatt(player &p) 96 { 97 if(MP<40) 98 { 99 cout<<";You don';t have enough magic points!";<<endl; 100 system(";pause";); 101 return 0; // Attack failed 102
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_H // Conditional compilation 9 #define CONTAINER_H 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 27
1 //======================= 2 // container.cpp 3 //======================= 4 5 // default constructor initialise the inventory as empty 6 7 #include";container.h"; 8 #include<iostream> 9 10 using namespace std; 11 12 container::container() 13 { 14 setƐ,0); 15 } 16 17 // set the item numbers 18 void container::set(int heal_n, int mw_n) 19 { 20 numOfHeal=heal_n; 21 numOfMW=mw_n; 22 } 23 24 // get the number of heal 25 int container::nOfHeal() 26 { 27 return numOfHeal; 28 } 29 30 // get the number of magic water 31 int container::nOfMW() 32 { 33 return numOfMW; 34 } 35 36 // display the items; 37 void container::display() 38 { 39 cout<<";Your bag contains: ";<<endl; 40 cout<<";Heal(HP+100): ";<<numOfHeal<<endl; 41 cout<<";Magic Water (MP+80): ";<<numOfMW<<endl; 42 } 43 44 //use heal 45 bool container::useHeal() 46 { 47 /ǘ_???????? 48 numOfHeal--; 49 return 1; // use heal successfully 50 } 51 52 //use magic water 53 bool container::useMW() 54 { 55 numOfMW--; 56 return 1; // use magic water successfully 57 } 58
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 16 using namespace std; 17 18 enum job {sw, ar, mg}; /* define 3 jobs by enumerate type 19 sword man, archer, mage */ 20 class player 21 { 22 friend void showinfo(player &p1, player &p2); 23 friend class swordsman; 24 25 protected: 26 int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV; 27 // General properties of all characters 28 string name; // character name 29 job role; /* character';s job, one of swordman, archer and mage, 30 as defined by the enumerate type */ 31 container bag; // character';s inventory 32 33 public: 34 virtual bool attack(player &p)=0; // normal attack 35 virtual bool specialatt(player &p)=0; //special attack 36 virtual void isLevelUp()=0; // level up judgement 37 /* Attention! 38 These three methods are called ";Pure virtual functions";. 39 They have only declaration, but no definition. 40 The class with pure virtual functions are called ";Abstract class";, which can only be used to inherited, but not to constructor objects. 41 The detailed definition of these pure virtual functions will be given in subclasses. */ 42 43 void reFill(); // character';s HP and MP resume 44 bool death(); // report whether character is dead 45 void isDead(); // check whether character is dead 46 bool useHeal(); // consume heal, irrelevant to job 47 bool useMW(); // consume magic water, irrelevant to job 48 void transfer(player &p); // possess opponent';s items after victory 49 void showRole(); // display character';s job 50 51 private: 52 bool playerdeath; // whether character is dead, doesn';t need to be accessed or inherited 53 }; 54 55 #endif 56
1 //======================= 2 // player.cpp 3 //======================= 4 5 6 // character';s HP and MP resume 7 8 #include";player.h"; 9 #include <iostream> 10 11 using namespace std; 12 13 void player::reFill() 14 { 15 HP=HPmax; // HP and MP fully recovered 16 MP=MPmax; 17 } 18 19 // report whether character is dead 20 bool player::death() 21 { 22 return playerdeath; 23 } 24 25 // check whether character is dead 26 void player::isDead() 27 { 28 if(HP<=0) // HP less than 0, character is dead 29 { 30 cout<<name<<"; is Dead."; <<endl; 31 system(";pause";); 32 playerdeath=1; // give the label of death value 1 33 } 34 } 35 36 // consume heal, irrelevant to job 37 bool player::useHeal() 38 { 39 if(bag.nOfHeal()>0) 40 { 41 HP=HP+100; 42 if(HP>HPmax) // HP cannot be larger than maximum value 43 HP=HPmax; // so assign it to HPmax, if necessary 44 cout<<name<<"; used Heal, HP increased by 100.";<<endl; 45 bag.useHeal(); // use heal 46 system(";pause";); 47 return 1; // usage of heal succeed 48 } 49 else // If no more heal in bag, cannot use 50 { 51 cout<<";Sorry, you don';t have heal to use.";<<endl; 52 system(";pause";); 53 return 0; // usage of heal failed 54 } 55 } 56 57 // consume magic water, irrelevant to job 58 bool player::useMW() 59 { 60 if(bag.nOfMW()>0) 61 { 62 MP=MP+100; 63 if(MP>MPmax) 64 MP=MPmax; 65 cout<<name<<"; used Magic Water, MP increased by 100.";<<endl; 66 bag.useMW(); 67 system(";pause";); 68 return 1; // usage of magic water succeed 69 } 70 else 71 { 72 cout<<";Sorry, you don';t have magic water to use.";<<endl; 73 system(";pause";); 74 return 0; // usage of magic water failed 75 } 76 } 77 78 // possess opponent';s items after victory 79 void player::transfer(player &p) 80 { 81 cout<<name<<"; got";<<p.bag.nOfHeal()<<"; Heal, and ";<<p.bag.nOfMW()<<"; Magic Water.";<<endl; 82 system(";pause";); 83 /Ǚ_??????????? 84 bag.set(bag.nOfHeal() + p.bag.nOfHeal(), bag.nOfMW() + p.bag.nOfMW()); 85 // set the character';s bag, get opponent';s items 86 } 87 88 // display character';s job 89 void player::showRole() 90 { 91 switch(role) 92 { 93 case sw: 94 cout<<";Swordsman";; 95 break; 96 case ar: 97 cout<<";Archer";; 98 break; 99 case mg: 100 cout<<";Mage";; 101 break; 102
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Ƒ,tempName); // create the character with user inputted name and job 31 success=1; // operation succeed 32 break; 33 default: 34 break; // In this case, success=0, character creation failed 35 } 36 }while(success!=1); // so the loop will ask user to re-create a character 37 38 int tempCom; // temp command inputted by user 39 int nOpp=0; // the Nth opponent 40 for(int i=1;nOpp<5;i+=2) // i is opponent';s level 41 { 42 nOpp++; 43 system(";cls";); 44 cout<<";STAGE"; <<nOpp<<endl; 45 cout<<";Your opponent, a Level ";<<i<<"; Swordsman.";<<endl; 46 system(";pause";); 47 swordsman enemy(i, ";Warrior";); // Initialise an opponent, level i, name ";Junior"; 48 human->reFill(); // get HP/MP refill before start fight 49 50 while(!human->death() && !enemy.death()) // no died 51 { 52 success=0; 53 while (success!=1) 54 { 55 showinfo(*human,enemy); // show fighter';s information 56 cout<<";Please give command: ";<<endl; 57 cout<<";1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game";<<endl; 58 cin>>tempCom; 59 switch(tempCom) 60 { 61 case 0: 62 cout<<";Are you sure to exit? Y/N";<<endl; 63 char temp; 64 cin>>temp; 65 if(temp==';Y';||temp==';y';) 66 return 0; 67 else 68 break; 69 case 1: 70 success=human->attack(enemy); 71 human->isLevelUp(); 72 enemy.isDead(); 73 break; 74 case 2: 75 success=human->specialatt(enemy); 76 human->isLevelUp(); 77 enemy.isDead(); 78 break; 79 case 3: 80 success=human->useHeal(); 81 break; 82 case 4: 83 success=human->useMW(); 84 break; 85 default: 86 break; 87 } 88 } 89 if(!enemy.death()) // If AI still alive 90 enemy.AI(*human); 91 else // AI died 92 { 93 cout<<";YOU WIN";<<endl; 94 human->transfer(enemy); // player got all AI';s items 95 } 96 if (human->death()) 97 { 98 system(";cls";); 99 cout<<endl<<setw࿒)<<";GAME OVER";<<endl; 100 /ǜ_??????????? // player is dead, program is getting to its end, what should we do here? 101 delete human; 102
运行截图: