实验五 模板类与多态
实验五 模板类与多态
四、实验结论
1.实验二
#ifndef PERSON_HPP
#define PERSON_HPP
#include <iostream>
#include <string>
#include <utility>
using namespace std;
class Person {
private:
string name;
string telephone;
string email;
public:
Person() = default;
Person(string n, string t, string e = "") {
name = std::move(n);
telephone = std::move(t);
email = std::move(e);
}
void change_phone() {
string temp;
cin >> temp;
telephone = temp;
}
void change_email() {
string temp;
cin >> temp;
email = temp;
}
friend ostream &operator<<(ostream &out, const Person &p);
friend istream &operator>>(istream &in, Person &p);
friend bool operator==(const Person &p1, const Person &p2);
~Person() = default;
};
ostream &operator<<(ostream &out, const Person &p) {
out <<left<< p.name<<endl
<<left<< p.telephone<<endl
<<left<< p.email<<endl ;
return out;
}
istream &operator>>(istream &in, Person &p) {
getline(in>>ws, p.name);
getline(in>>ws, p.telephone);
getline(in>>ws, p.email);
return in;
}
bool operator==(const Person &p1, const Person &p2) {
if (p1.name == p2.name && p1.telephone == p2.telephone) {
return true;
}
return false;
}
#endif
#include <iostream>
#include <fstream>
#include <vector>
#include "Person.hpp"
int main() {
using namespace std;
vector<Person> phone_book;
Person p;
while (cin >> p) {
phone_book.push_back(p);
}
for (auto &i: phone_book)
cout << i << endl;
cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl;
ofstream fout;
fout.open("phone_book.txt");
if (!fout.is_open()) {
cerr << "fail to open file phone_book.txt\n";
return 1;
}
for (auto &i: phone_book)
fout << i << endl;
fout.close();
}
2.实验三
//=======================
// main.cpp
//=======================
// main function for the RPG style game
#include <iostream>
#include <string>
using namespace std;
#include "swordsman.h"
int main() {
string tempName;
bool success = 0; //flag for storing whether operation is successful
cout << "Please input player's name: ";
cin >> tempName; // get player's name from keyboard input
player *human; // use pointer of base class, convenience for polymorphism
int tempJob; // temp choice for job selection
do {
cout << "Please choose a job: 1 Swordsman, 2 Archer, 3 Mage" << endl;
cin >> tempJob;
system("cls"); // clear the screen
switch (tempJob) {
case 1:
human = new swordsman(1, tempName); // create the character with user inputted name and job
success = 1; // operation succeed
break;
default:
break; // In this case, success=0, character creation failed
}
} while (success != 1); // so the loop will ask user to re-create a character
int tempCom; // temp command inputted by user
int nOpp = 0; // the Nth opponent
for (int i = 1; nOpp < 5; i += 2) // i is opponent's level
{
nOpp++;
system("cls");
cout << "STAGE" << nOpp << endl;
cout << "Your opponent, a Level " << i << " Swordsman." << endl;
system("pause");
swordsman enemy(i, "Warrior"); // Initialise an opponent, level i, name "Junior"
human->reFill(); // get HP/MP refill before start fight
while (!human->death() && !enemy.death()) // no died
{
success = 0;
while (success != 1) {
showinfo(*human, enemy); // show fighter's information
cout << "Please give command: " << endl;
cout << "1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game" << endl;
cin >> tempCom;
switch (tempCom) {
case 0:
cout << "Are you sure to exit? Y/N" << endl;
char temp;
cin >> temp;
if (temp == 'Y' || temp == 'y')
return 0;
else
break;
case 1:
success = human->attack(enemy);
human->isLevelUp();
enemy.isDead();
break;
case 2:
success = human->specialatt(enemy);
human->isLevelUp();
enemy.isDead();
break;
case 3:
success = human->useHeal();
break;
case 4:
success = human->useMW();
break;
default:
break;
}
}
if (!enemy.death()) // If AI still alive
enemy.AI(*human);
else // AI died
{
cout << "YOU WIN" << endl;
human->transfer(enemy); // player got all AI's items
}
if (human->death()) {
system("cls");
cout << endl << setw(50) << "GAME OVER" << endl;
delete human; // player is dead, program is getting to its end, what should we do here?
system("pause");
return 0;
}
}
}
delete human; // You win, program is getting to its end, what should we do here?
system("cls");
cout << "Congratulations! You defeated all opponents!!" << endl;
system("pause");
return 0;
}
//=======================
// container.cpp
//=======================
#include "container.h"
#include "iostream"
using namespace std;
// default constructor initialise the inventory as empty
container::container() {
set(0, 0);
}
// set the item numbers
void container::set(int heal_n, int mw_n) {
numOfHeal = heal_n;
numOfMW = mw_n;
}
// get the number of heal
int container::nOfHeal() {
return numOfHeal;
}
// get the number of magic water
int container::nOfMW() {
return numOfMW;
}
// display the items;
void container::display() {
cout << "Your bag contains: " << endl;
cout << "Heal(HP+100): " << numOfHeal << endl;
cout << "Magic Water (MP+80): " << numOfMW << endl;
}
//use heal
bool container::useHeal() {
numOfHeal--;
return 1; // use heal successfully
}
//use magic water
bool container::useMW() {
numOfMW--;
return 1; // use magic water successfully
}
//=======================
// container.h
//=======================
// The so-called inventory of a player in RPG games
// contains two items, heal and magic water
#ifndef _CONTAINER // Conditional compilation
#define _CONTAINER
class container // Inventory
{
protected:
int numOfHeal; // number of heal
int numOfMW; // number of magic water
public:
container(); // constuctor
void set(int heal_n, int mw_n); // set the items numbers
int nOfHeal(); // get the number of heal
int nOfMW(); // get the number of magic water
void display(); // display the items;
bool useHeal(); // use heal
bool useMW(); // use magic water
};
#endif
//=======================
// player.cpp
//=======================
#include "player.h"
#include "iostream"
using namespace std;
// character's HP and MP resume
void player::reFill() {
HP = HPmax; // HP and MP fully recovered
MP = MPmax;
}
// report whether character is dead
bool player::death() {
return playerdeath;
}
// check whether character is dead
void player::isDead() {
if (HP <= 0) // HP less than 0, character is dead
{
cout << name << " is Dead." << endl;
system("pause");
playerdeath = 1; // give the label of death value 1
}
}
// consume heal, irrelevant to job
bool player::useHeal() {
if (bag.nOfHeal() > 0) {
HP = HP + 100;
if (HP > HPmax) // HP cannot be larger than maximum value
HP = HPmax; // so assign it to HPmax, if necessary
cout << name << " used Heal, HP increased by 100." << endl;
bag.useHeal(); // use heal
system("pause");
return 1; // usage of heal succeed
} else // If no more heal in bag, cannot use
{
cout << "Sorry, you don't have heal to use." << endl;
system("pause");
return 0; // usage of heal failed
}
}
// consume magic water, irrelevant to job
bool player::useMW() {
if (bag.nOfMW() > 0) {
MP = MP + 100;
if (MP > MPmax)
MP = MPmax;
cout << name << " used Magic Water, MP increased by 100." << endl;
bag.useMW();
system("pause");
return 1; // usage of magic water succeed
} else {
cout << "Sorry, you don't have magic water to use." << endl;
system("pause");
return 0; // usage of magic water failed
}
}
// possess opponent's items after victory
void player::transfer(player &p) {
cout << name << " got" << p.bag.nOfHeal() << " Heal, and " << p.bag.nOfMW() << " Magic Water." << endl;
system("pause");
bag.set(bag.nOfHeal() + p.bag.nOfHeal(), bag.nOfMW() + p.bag.nOfMW());
// set the character's bag, get opponent's items
}
// display character's job
void player::showRole() {
switch (role) {
case sw:
cout << "Swordsman";
break;
case ar:
cout << "Archer";
break;
case mg:
cout << "Mage";
break;
default:
break;
}
}
// display character's job
void showinfo(player &p1, player &p2) {
system("cls");
cout << "##############################################################" << endl;
cout << "# Player" << setw(10) << p1.name << " LV. " << setw(3) << p1.LV
<< " # Opponent" << setw(10) << p2.name << " LV. " << setw(3) << p2.LV << " #" << endl;
cout << "# HP " << setw(3) << (p1.HP <= 999 ? p1.HP : 999) << '/' << setw(3) << (p1.HPmax <= 999 ? p1.HPmax : 999)
<< " | MP " << setw(3) << (p1.MP <= 999 ? p1.MP : 999) <<