实验5 模板类与多态
作者:互联网
- 实验任务2
source code in Person.hpp :
1 #include <iostream> 2 #include <iomanip> 3 4 using namespace std; 5 6 class Person 7 { 8 private: 9 string name; 10 string telephone; 11 string email; 12 13 public: 14 Person(){ }; 15 Person(string init_name, string init_tel, string init_eml = " ") : name(init_name), telephone(init_tel), email(init_eml) {} 16 void reset_Tel(string new_ph) { telephone = new_ph; } 17 void reset_email(string new_email) { email = new_email; } 18 19 friend ostream& operator<<(ostream& os, const Person& p); 20 friend istream& operator>>(istream& in, Person& p); 21 friend bool operator==(const Person& p1, const Person& p2); 22 }; 23 24 ostream& operator<<(ostream& os, const Person& p){ 25 os << left<<setw(15) << p.name 26 << left << setw(15) << p.telephone 27 << left << setw(15) << p.email; 28 return os; 29 } 30 istream& operator>>(istream& in, Person& p){ 31 in >> p.name >> p.telephone >> p.email; 32 return in; 33 } 34 bool operator==(const Person& p1, const Person& p2){ 35 return (p1.name == p2.name && p1.telephone == p2.telephone); 36 }
The Running results :
- 回答问题
- ①operator<<(cout,i)
- ②operaotr<<(fout,i)
- 实验任务3
source code in 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 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 };
source code in player.cpp :
1 //======================= 2 // player.cpp 3 //======================= 4 5 6 // character's HP and MP resume 7 void player::reFill() 8 { 9 HP=HPmax; // HP and MP fully recovered 10 MP=MPmax; 11 } 12 13 // report whether character is dead 14 bool player::death() 15 { 16 return playerdeath; 17 } 18 19 // check whether character is dead 20 void player::isDead() 21 { 22 if(HP<=0) // HP less than 0, character is dead 23 { 24 cout<<name<<" is Dead." <<endl; 25 system("pause"); 26 playerdeath=1; // give the label of death value 1 27 } 28 } 29 30 // consume heal, irrelevant to job 31 bool player::useHeal() 32 { 33 if(bag.nOfHeal()>0) 34 { 35 HP=HP+100; 36 if(HP>HPmax) // HP cannot be larger than maximum value 37 HP=HPmax; // so assign it to HPmax, if necessary 38 cout<<name<<" used Heal, HP increased by 100."<<endl; 39 bag.useHeal(); // use heal 40 system("pause"); 41 return 1; // usage of heal succeed 42 } 43 else // If no more heal in bag, cannot use 44 { 45 cout<<"Sorry, you don't have heal to use."<<endl; 46 system("pause"); 47 return 0; // usage of heal failed 48 } 49 } 50 51 // consume magic water, irrelevant to job 52 bool player::useMW() 53 { 54 if(bag.nOfMW()>0) 55 { 56 MP=MP+100; 57 if(MP>MPmax) 58 MP=MPmax; 59 cout<<name<<" used Magic Water, MP increased by 100."<<endl; 60 bag.useMW(); 61 system("pause"); 62 return 1; // usage of magic water succeed 63 } 64 else 65 { 66 cout<<"Sorry, you don't have magic water to use."<<endl; 67 system("pause"); 68 return 0; // usage of magic water failed 69 } 70 } 71 72 // possess opponent's items after victory 73 void player::transfer(player &p) 74 { 75 cout<<name<<" got"<<p.bag.nOfHeal()<<" Heal, and "<<p.bag.nOfMW()<<" Magic Water."<<endl; 76 system("pause"); 77 bag.set(bag.nOfHeal()+p.bag.nOfHeal(), bag.nOfMW() + bag.nOfMW()); 78 // set the character's bag, get opponent's items 79 } 80 81 // display character's job 82 void player::showRole() 83 { 84 switch(role) 85 { 86 case sw: 87 cout<<"Swordsman"; 88 break; 89 case ar: 90 cout<<"Archer"; 91 break; 92 case mg: 93 cout<<"Mage"; 94 break; 95 default: 96 break; 97 } 98 } 99 100 101 // display character's job 102 void showinfo(player& p1, player& p2) 103 { 104 system("cls"); 105 cout<<"##############################################################"<<endl; 106 cout<<"# Player"<<setw(10)<<p1.name<<" LV. "<<setw(3) <<p1.LV 107 <<" # Opponent"<<setw(10)<<p2.name<<" LV. "<<setw(3) <<p2.LV<<" #"<<endl; 108 cout<<"# HP "<<setw(3)<<(p1.HP<=999?p1.HP:999)<<'/'<<setw(3)<<(p1.HPmax<=999?p1.HPmax:999) 109 <<" | MP "<<setw(3)<<(p1.MP<=999?p1.MP:999)<<'/'<<setw(3)<<(p1.MPmax<=999?p1.MPmax:999) 110 <<" # HP "<<setw(3)<<(p2.HP<=999?p2.HP:999)<<'/'<<setw(3)<<(p2.HPmax<=999?p2.HPmax:999) 111 <<" | MP "<<setw(3)<<(p2.MP<=999?p2.MP:999)<<'/'<<setw(3)<<(p2.MPmax<=999?p2.MPmax:999)<<" #"<<endl; 112 cout<<"# AP "<<setw(3)<<(p1.AP<=999?p1.AP:999) 113 <<" | DP "<<setw(3)<<(p1.DP<=999?p1.DP:999) 114 <<" | speed "<<setw(3)<<(p1.speed<=999?p1.speed:999) 115 <<" # AP "<<setw(3)<<(p2.AP<=999?p2.AP:999) 116 <<" | DP "<<setw(3)<<(p2.DP<=999?p2.DP:999) 117 <<" | speed "<<setw(3)<<(p2.speed<=999?p2.speed:999)<<" #"<<endl; 118 cout<<"# EXP"<<setw(7)<<p1.EXP<<" Job: "<<setw(7); 119 p1.showRole(); 120 cout<<" # EXP"<<setw(7)<<p2.EXP<<" Job: "<<setw(7); 121 p2.showRole(); 122 cout<<" #"<<endl; 123 cout<<"--------------------------------------------------------------"<<endl; 124 p1.bag.display(); 125 cout<<"##############################################################"<<endl; 126 }
source code in container.cpp:
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 }
source code in 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 #endif
source code in main.cpp:
1 //======================= 2 // main.cpp 3 //======================= 4 5 // main function for the RPG style game 6 7 #include <bits/stdc++.h> 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 16 int main() 17 { 18 string tempName; 19 bool success=0; //flag for storing whether operation is successful 20 cout <<"Please input player's name: "; 21 cin >>tempName; // get player's name from keyboard input 22 player *human; // use pointer of base class, convenience for polymorphism 23 int tempJob; // temp choice for job selection 24 do 25 { 26 cout <<"Please choose a job: 1 Swordsman, 2 Archer, 3 Mage"<<endl; 27 cin>>tempJob; 28 system("cls"); // clear the screen 29 switch(tempJob) 30 { 31 case 1: 32 human=new swordsman(1,tempName); // create the character with user inputted name and job 33 success=1; // operation succeed 34 break; 35 default: 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 } 114
The Running results :
标签:container,name,int,多态,player,实验,human,模板,string 来源: https://www.cnblogs.com/zmqh/p/15692768.html