task1_1
程序源码
task1_1.cpp
1 #include <iostream> 2 #include <map> 3 using namespace std; 4 int main() { 5 map<int, char> grade_dict{ {1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'}, {5, 'E'} }; 6 int grade; 7 while (cin >> grade && grade <= 5 && grade >= 1) { 8 cout << grade_dict[grade] << endl; 9 } 10 11 return 0; 12 }
程序运行截图:
task1_2.cpp
1 #include <iostream> 2 #include <map> 3 #include <algorithm> 4 #include <string> 5 using namespace std; 6 7 int main() { 8 map<int, char> dec_hex_dict{ {10, 'A'}, {11, 'B'}, {12, 'C'}, {13, 'D'}, {14, 'E'} 9 , {15, 'F'} }; 10 for (int i = 0; i < 16; i++) { 11 cout << dec_hex_dict[i]; 12 } 13 cout << endl; 14 15 string s1, s2, s3; 16 s1 = "gaoyuan"; 17 cout << s1 << endl << endl; 18 reverse(s1.begin(), s1.end()); 19 cout << s1 << endl; 20 21 s3 = s1; 22 s2 = string(s3.rbegin(), s3.rend()); 23 24 25 26 cout << "s2: " << s2 << endl; 27 cout << "s3: " << s3 << endl; 28 return 0; 29 }
程序运行截图:
这个与python里的字典很像
task2
程序源码:
graph.hpp
1 #include <iostream> 2 #include <typeinfo> 3 4 using namespace std; 5 class Graph { 6 public: 7 void draw() { 8 cout << "graph" << endl; 9 } 10 private: 11 string bgcolor; 12 13 }; 14 15 class Rectangle: public Graph { 16 public: 17 void draw() { cout << "draw a rectangle " << endl; } 18 19 }; 20 21 class Circle : public Graph { 22 public: 23 void draw() { cout << "draw a circle " << endl; } 24 };
task2.cpp
1 #include <iostream> 2 #include "graph.hpp" 3 using namespace std; 4 void func(Graph* ptr) { 5 cout << "pointer name:" << typeid(ptr).name() << endl; 6 cout << "RTTI type:" << typeid(*ptr).name() << endl; 7 ptr->draw(); 8 } 9 10 void test() { 11 Rectangle r1; 12 func(&r1); 13 14 Circle r2; 15 func(&r2); 16 17 18 } 19 int main() { 20 test(); 21 return 0; 22 }
程序运行截图:
这里主要是考察了虚函数的使用
task3
程序源码:
Complex.hpp
1 #pragma once 2 #include <iostream> 3 using namespace std; 4 5 6 class complex { 7 public: 8 complex(double r = 0.0, double i = 0.0) { 9 real = r; 10 imag = i; 11 } 12 complex(const complex& c); 13 ~complex() = default; 14 double get_real() const { return real; } 15 double get_imag() const { return imag; } 16 complex& operator+=(const complex& c); 17 friend ostream& operator<<(ostream& out, const complex& c); 18 friend istream& operator>>(istream& in, complex& c); 19 private: 20 double real, imag; 21 }; 22 istream& operator>>(istream& in, complex& c) { 23 in >> c.real >> c.imag; 24 return in; 25 } 26 ostream& operator<<(ostream& out, const complex& c) { 27 out << "(" << c.real << "," << c.imag << ")"; 28 return out; 29 } 30 31 complex::complex(const complex& c) { 32 real = c.real; 33 imag = c.imag; 34 } 35 36 complex& complex::operator+=(const complex& c) { 37 real += c.real; 38 imag += c.imag; 39 return *this; 40 } 41 42 complex operator+(const complex& c1, const complex& c2) { 43 return (complex(c1.get_real() + c2.get_real(), c1.get_imag() + c2.get_imag())); 44 } 45 46 bool operator==(const complex& c1, const complex& c2) { 47 if (c1.get_real() == c2.get_real() && c2.get_imag() == c2.get_imag()) 48 return true; 49 else 50 return false; 51 }
程序运行截图
task4
程序源码:
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 class MachinePets { 6 public: 7 MachinePets(const string s) : nickname(s) {} 8 string get_name() const { return nickname; } 9 string talk() { return "machinepets shout"; } 10 private: 11 string nickname; 12 13 }; 14 15 class PetCats : public MachinePets { 16 public: 17 PetCats(const string s) : MachinePets(s) {} 18 string talk() { return "miaomiao"; } 19 20 }; 21 22 23 24 class PetDogs : public MachinePets { 25 public: 26 PetDogs(const string s) : MachinePets(s) {} 27 string talk() { return "wangwang"; } 28 };
task4.cpp
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include "pets.hpp" 3 #include <iostream> 4 using namespace std; 5 void play(MachinePets* p) { 6 cout << p->get_name() << "发出声音:" << p->talk() << endl; 7 } 8 void test1() { 9 PetCats cat("kitty"); 10 PetDogs dog("dahuang"); 11 play(&cat); 12 play(&dog); 13 } 14 int main() { 15 test1(); 16 17 return 0; 18 }
程序运行截图:
这里就是因为有无虚函数而导致运行结果的不同。
task5
程序源码:
Person.hpp
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include <iostream> 3 #include <string> 4 #include <iomanip> 5 using namespace std; 6 class Person { 7 public: 8 Person() {} 9 Person(string n, string t, string e = " ") : name(n), telephone(t), email(e) {} 10 Person(const Person& p) { 11 name = p.name; 12 telephone = p.telephone; 13 email = p.email; 14 } 15 void update_telephone(); 16 void update_email(); 17 friend ostream& operator<<(ostream& out, const Person& p); 18 friend istream& operator>>(istream& in, Person& p); 19 friend bool operator==(const Person& p1, const Person& p2); 20 private: 21 string name; 22 string telephone; 23 string email; 24 }; 25 void Person::update_email() { 26 string change; 27 cin.clear(); 28 cout << "Enter updated email" << endl; 29 cin >> change; 30 email = change; 31 cout << "email has been updated" << endl; 32 } 33 void Person::update_telephone() { 34 string change; 35 cout << "Enter updated telephone" << endl; 36 cin.clear(); 37 cin >> change; 38 telephone = change; 39 cout << "telephone has been updated" << endl; 40 } 41 ostream& operator<<(ostream& out, const Person& p) { 42 out << left << setw(20) << p.name << setw(20) << p.telephone 43 << setw(20) << p.email; 44 return out; 45 } 46 47 istream& operator>>(istream& in, Person& p) { 48 cout << "输入信息" << endl; 49 in >> p.name >> p.telephone >> p.email; 50 return in; 51 } 52 53 bool operator==(const Person& p1, const Person& p2) { 54 if (p1.name == p2.name && p1.telephone == p2.telephone) 55 return true; 56 else 57 return false; 58 }
task5.hpp
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include "Person.hpp" 3 #include <iostream> 4 #include <vector> 5 using namespace std; 6 void test() { 7 vector<Person> phone_book; 8 Person p; 9 cout << "Enter information until enter z" << endl; 10 while (cin >> p) { 11 phone_book.push_back(p); 12 13 } 14 cout << "update person information" << endl; 15 phone_book.at(0).update_telephone(); 16 phone_book.at(0).update_email(); 17 18 cout << "show " << endl; 19 for (auto& obj : phone_book) { 20 cout << obj << endl; 21 } 22 23 cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl; 24 } 25 26 int main() { 27 test(); 28 29 }
程序运行截图:
task6
程序源码:
main.cpp
1 #define _CRT_SECURE_NO_WARNINGS 1 2 //======================= 3 // main.cpp 4 //======================= 5 6 // main function for the RPG style game 7 8 #include <iostream> 9 #include <string> 10 using namespace std; 11 12 #include "swordsman.h" 13 14 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 = NULL; // 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 << setw(50) << "GAME OVER" << endl; 101 delete human; // player is dead, program is getting to its end, what should we do here? 102 system("pause"); 103 return 0; 104 } 105 } 106 } 107 if (human->death()) { // You win, program is getting to its end, what should we do here? 108 system("cls"); 109 cout << "Congratulations! You defeated all opponents!!" << endl; 110 system("pause"); 111 return 0; 112 } 113 }
container.cpp
1 #pragma once 2 //======================= 3 // container.cpp 4 //======================= 5 #include "container.h" 6 #include <iostream> 7 using namespace std; 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 numOfMW--; 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 }
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 };
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 #pragma once // 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 };
swordsman.cpp
1 //======================= 2 // swordsman.cpp 3 //======================= 4 #include "swordsman.h" 5 #include <iostream> 6 using namespace std; 7 // constructor. default values don't need to be repeated here 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 void swordsman::isLevelUp() 29 { 30 if (EXP >= LV * LV * 75) 31 { 32 LV++; 33 AP += 4; 34 DP += 4; 35 HPmax += 8; 36 MPmax += 2; 37 speed += 2; 38 cout << name << " Level UP!" << endl; 39 cout << "HP improved 8 points to " << HPmax << endl; 40 cout << "MP improved 2 points to " << MPmax << endl; 41 cout << "Speed improved 2 points to " << speed << endl; 42 cout << "AP improved 4 points to " << AP << endl; 43 cout << "DP improved 5 points to " << DP << endl; 44 system("pause"); 45 isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp 46 } 47 } 48 49 bool swordsman::attack(player& p) 50 { 51 double HPtemp = 0; // opponent's HP decrement 52 double EXPtemp = 0; // player obtained exp 53 double hit = 1; // attach factor, probably give critical attack 54 srand((unsigned)time(NULL)); // generating random seed based on system time 55 56 // If speed greater than opponent, you have some possibility to do double attack 57 if ((speed > p.speed) && (rand() % 100 < (speed - p.speed))) // rand()%100 means generates a number no greater than 100 58 { 59 HPtemp = (int)((1.0 * AP / p.DP) * AP * 5 / (rand() % 4 + 10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance 60 cout << name << "'s quick strike hit " << p.name << ", " << p.name << "'s HP decreased " << HPtemp << endl; 61 p.HP = int(p.HP - HPtemp); 62 EXPtemp = (int)(HPtemp * 1.2); 63 } 64 65 // If speed smaller than opponent, the opponent has possibility to evade 66 if ((speed < p.speed) && (rand() % 50 < 1)) 67 { 68 cout << name << "'s attack has been evaded by " << p.name << endl; 69 system("pause"); 70 return 1; 71 } 72 73 // 10% chance give critical attack 74 if (rand() % 100 <= 10) 75 { 76 hit = 1.5; 77 cout << "Critical attack: "; 78 } 79 80 // Normal attack 81 HPtemp = (int)((1.0 * AP / p.DP) * AP * 5 / (rand() % 4 + 10)); 82 cout << name << " uses bash, " << p.name << "'s HP decreases " << HPtemp << endl; 83 EXPtemp = (int)(EXPtemp + HPtemp * 1.2); 84 p.HP = (int)(p.HP - HPtemp); 85 cout << name << " obtained " << EXPtemp << " experience." << endl; 86 EXP = (int)(EXP + EXPtemp); 87 system("pause"); 88 return 1; // Attack success 89 } 90 91 bool swordsman::specialatt(player& p) 92 { 93 if (MP < 40) 94 { 95 cout << "You don't have enough magic points!" << endl; 96 system("pause"); 97 return 0; // Attack failed 98 } 99 else 100 { 101 MP -= 40; // consume 40 MP to do special attack 102 103 //10% chance opponent evades 104 if (rand() % 100 <= 10) 105 { 106 cout << name << "'s leap attack has been evaded by " << p.name << endl; 107 system("pause"); 108 return 1; 109 } 110 111 double HPtemp = 0; 112 double EXPtemp = 0; 113 //double hit=1; 114 //srand(time(NULL)); 115 HPtemp = (int)(AP * 1.2 + 20); // not related to opponent's DP 116 EXPtemp = (int)(HPtemp * 1.5); // special attack provides more experience 117 cout << name << " uses leap attack, " << p.name << "'s HP decreases " << HPtemp << endl; 118 cout << name << " obtained " << EXPtemp << " experience." << endl; 119 p.HP = (int)(p.HP - HPtemp); 120 EXP = (int)(EXP + EXPtemp); 121 system("pause"); 122 } 123 return 1; // special attack succeed 124 } 125 126 // Computer opponent 127 void swordsman::AI(player& p) 128 { 129 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))) 130 // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round 131 { 132 useHeal(); 133 } 134 else 135 { 136 if (MP >= 40 && HP > 0.5 * HPmax && rand() % 100 <= 30) 137 // AI has enough MP, it has 30% to make special attack 138 { 139 specialatt(p); 140 p.isDead(); // check whether player is dead 141 } 142 else 143 { 144 if (MP < 40 && HP>0.5 * HPmax && bag.nOfMW()) 145 // Not enough MP && HP is safe && still has magic water 146 { 147 useMW(); 148 } 149 else 150 { 151 attack(p); // normal attack 152 p.isDead(); 153 } 154 } 155 } 156 }
player.cpp
1 //======================= 2 // player.cpp 3 //======================= 4 5 #include "player.h" 6 #include <iostream> 7 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 std::cout << name << " is Dead." << std::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 std::cout << name << " used Heal, HP increased by 100." << std::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 std::cout << "Sorry, you don't have heal to use." << std::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 std::cout << name << " used Magic Water, MP increased by 100." << std::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 std::cout << name << " got" << p.bag.nOfHeal() << " Heal, and " << p.bag.nOfMW() << " Magic Water." << std::endl; 78 system("pause"); 79 HP += p.bag.nOfHeal(); 80 MP += p.bag.nOfMW(); 81 // set the character's bag, get opponent's items 82 } 83 84 // display character's job 85 void player::showRole() 86 { 87 switch (role) 88 { 89 case sw: 90 cout << "Swordsman"; 91 break; 92 case ar: 93 cout << "Archer"; 94 break; 95 case mg: 96 cout << "Mage"; 97 break; 98 default: 99 break; 100 } 101 } 102 103 104 // display character's job 105 void showinfo(player& p1, player& p2) 106 { 107 using namespace std; 108 system("cls"); 109 cout << "##############################################################" << endl; 110 cout << "# Player" << setw(10) << p1.name << " LV. " << setw(3) << p1.LV 111 << " # Opponent" << setw(10) << p2.name << " LV. " << setw(3) << p2.LV << " #" << endl; 112 cout << "# HP " << setw(3) << (p1.HP <= 999 ? p1.HP : 999) << '/' << setw(3) << (p1.HPmax <= 999 ? p1.HPmax : 999) 113 << " | MP " << setw(3) << (p1.MP <= 999 ? p1.MP : 999) << '/' << setw(3) << (p1.MPmax <= 999 ? p1.MPmax : 999) 114 << " # HP " << setw(3) << (p2.HP <= 999 ? p2.HP : 999) << '/' << setw(3) << (p2.HPmax <= 999 ? p2.HPmax : 999) 115 << " | MP " << setw(3) << (p2.MP <= 999 ? p2.MP : 999) << '/' << setw(3) << (p2.MPmax <= 999 ? p2.MPmax : 999) << " #" << endl; 116 cout << "# AP " << setw(3) << (p1.AP <= 999 ? p1.AP : 999) 117 << " | DP " << setw(3) << (p1.DP <= 999 ? p1.DP : 999) 118 << " | speed " << setw(3) << (p1.speed <= 999 ? p1.speed : 999) 119 << " # AP " << setw(3) << (p2.AP <= 999 ? p2.AP : 999) 120 << " | DP " << setw(3) << (p2.DP <= 999 ? p2.DP : 999) 121 << " | speed " << setw(3) << (p2.speed <= 999 ? p2.speed : 999) << " #" << endl; 122 cout << "# EXP" << setw(7) << p1.EXP << " Job: " << setw(7); 123 p1.showRole(); 124 cout << " # EXP" << setw(7) << p2.EXP << " Job: " << setw(7); 125 p2.showRole(); 126 cout << " #" << endl; 127 cout << "--------------------------------------------------------------" << endl; 128 p1.bag.display(); 129 cout << "##############################################################" << endl; 130 }
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 #include <iostream> 16 using namespace std; 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 #endif
程序运行截图
运行的时候报错说指针未初始化,于是我将指针改为null
标签:cout,继承,void,多态,player,int,oop,include,string From: https://www.cnblogs.com/abcdefg-gaoyuan/p/16925854.html