實驗5 繼承和多型

小鱼戏小新發表於2024-12-03

實驗任務3:

pets.hpp

實驗5 繼承和多型
 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<string>
 5 
 6 using std::string;
 7 using std::cin;
 8 using std::cout;
 9 using std::endl;
10 
11 
12 class MachinePets
13 {
14 public:
15     MachinePets(const string& s);
16     virtual string talk() = 0;
17     string get_nickname()const;
18 private:
19     string nickname;
20 };
21 
22 MachinePets::MachinePets(const string& s):nickname{s}{}
23 
24 string MachinePets::get_nickname()const
25 {
26     return nickname;
27 }
28 
29 class PetCats :public MachinePets
30 {
31 public:
32     PetCats(const string& s);
33     string talk() override;
34 private:
35 
36 };
37 
38 PetCats::PetCats(const string& s):MachinePets{s}{}
39 
40 string PetCats::talk()
41 {
42     return string("miao~ miao~ ");
43 }
44 
45 class PetDogs :public MachinePets
46 {
47 public:
48     PetDogs(const string& s);
49     string talk() override;
50 private:
51 
52 };
53 PetDogs::PetDogs(const string& s):MachinePets{s}{}
54 
55 string PetDogs::talk()
56 {
57     return string("wang! wang!");
58 }
pets.hpp

task3.cpp

實驗5 繼承和多型
 1 #include <iostream>
 2 #include <vector>
 3 #include "pets.hpp"
 4 
 5 void test() {
 6     using namespace std;
 7 
 8     vector<MachinePets*> pets;
 9 
10     pets.push_back(new PetCats("miku"));
11     pets.push_back(new PetDogs("da huang"));
12 
13     for (auto& ptr : pets)
14         cout << ptr->get_nickname() << " says " << ptr->talk() << endl;
15 }
16 
17 int main() {
18     test();
19 }
task3.cpp

實驗任務4:

film.hpp

實驗5 繼承和多型
 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 #include<iomanip>
 5 using std::cin;
 6 using std::cout;
 7 using std::string;
 8 using std::istream;
 9 using std::ostream;
10 using std::setw;
11 using std::left;
12 
13 class Film
14 {
15 public:
16     Film(){}
17     friend istream& operator>>(istream& in,Film& f);
18     friend ostream& operator<<(ostream& out,const Film& f);
19     friend bool compare_by_year(const Film& f1, const Film& f2);
20 private:
21     string film_name;
22     string director_name;
23     string film_area;
24     int year;
25 };
26 
27 istream& operator>>(istream& in, Film& f)
28 {
29     cout << "錄入片名:"; in >> f.film_name;
30     cout << "錄入導演:"; in >> f.director_name;
31     cout << "錄入製片國家/地區:"; in >> f.film_area;
32     cout << "錄入上映年份:"; in >> f.year;
33     return in;
34 }
35 
36 ostream& operator<<(ostream& out, const Film& f)
37 {
38     out << left;
39     out << setw(15) << f.film_name
40         << setw(15) << f.director_name
41         << setw(15) << f.film_area
42         << setw(15) << f.year;
43     return out;
44 }
45 
46 bool compare_by_year(const Film& f1, const Film& f2)
47 {
48     return f1.year < f2.year;
49 }
film.hpp

task4.cpp

實驗5 繼承和多型
 1 #include "film.hpp"
 2 #include <iostream>
 3 #include <string>
 4 #include <vector>
 5 #include <algorithm>
 6 
 7 void test() {
 8     using namespace std;
 9 
10     int n;
11     cout << "輸入電影數目: ";
12     cin >> n;
13 
14     cout << "錄入" << n << "部影片資訊" << endl;
15     vector<Film> film_lst;
16     for (int i = 0; i < n; ++i) {
17         Film f;
18         cout << string(20, '-') << "" << i + 1 << "部影片錄入" << string(20, '-') << endl;
19         cin >> f;
20         film_lst.push_back(f);
21     }
22 
23     // 按發行年份升序排序
24     sort(film_lst.begin(), film_lst.end(), compare_by_year);
25 
26     cout << string(20, '=') + "電影資訊(按發行年份)" + string(20, '=') << endl;
27     for (auto& f : film_lst)
28         cout << f << endl;
29 }
30 
31 int main() {
32     test();
33 }
task4.cpp

實驗任務5:

Complex.hpp

實驗5 繼承和多型
 1 #include<iostream>
 2 #include<cmath>
 3 using std::istream;
 4 using std::ostream;
 5 
 6 template <typename T>
 7 class Complex
 8 {
 9 public:
10     Complex(T r=0,T i=0):real{r},imag{i}{}
11     Complex(const Complex<T>& c) { real = c.real, imag = c.imag; }
12 
13     T get_real()const { return real; }
14     T get_imag()const { return imag; }
15 
16     bool operator== (const Complex<T>&c);
17     Complex<T> operator+=(const Complex<T>& c);
18 
19     template<typename U>
20     friend Complex<U> operator+(const Complex<U>& c1, const Complex<U>& c2);
21 
22     template<typename U>
23     friend istream& operator>>(istream& in, Complex<U>& c);
24     template<typename U>
25     friend ostream& operator<<(ostream& out, const Complex<U>& c);
26 
27 private:
28     T real;
29     T imag;
30 };
31 template<typename T>
32 bool Complex<T>::operator==(const Complex<T>& c)
33 {
34     return real == c.real && imag == c.imag;
35 }
36 
37 template<typename T>
38 Complex<T> Complex<T>::operator+=(const Complex<T>& c)
39 {
40     real += c.real;
41     imag += c.imag;
42     return *this;
43 }
44 
45 template<typename T>
46 Complex<T> operator+(const Complex<T>& c1, const Complex<T>& c2)
47 {
48     return Complex<T>(c1.real + c2.real, c1.imag + c2.imag);
49 }
50 
51 template<typename T>
52 istream& operator>>(istream& in, Complex<T>& c)
53 {
54     in >> c.real >> c.imag;
55     return in;
56 }
57 
58 template<typename T>
59 ostream& operator<<(ostream& out, const Complex<T>& c)
60 {
61     if (c.imag >= 0) { out << c.real << " + " << abs(c.imag) << "i"; }
62     else { out << c.real << " - " << abs(c.imag) << "i"; }
63     return out;
64 }
Complex.hpp

task5.cpp

實驗5 繼承和多型
 1 #include "Complex.hpp"
 2 #include <iostream>
 3 
 4 using std::cin;
 5 using std::cout;
 6 using std::endl;
 7 using std::boolalpha;
 8 
 9 void test1() {
10     Complex<int> c1(2, -5), c2(c1);
11 
12     cout << "c1 = " << c1 << endl;
13     cout << "c2 = " << c2 << endl;
14     cout << "c1 + c2 = " << c1 + c2 << endl;
15 
16     c1 += c2;
17     cout << "c1 = " << c1 << endl;
18     cout << boolalpha << (c1 == c2) << endl;
19 }
20 
21 void test2() {
22     Complex<double> c1, c2;
23     cout << "Enter c1 and c2: ";
24     cin >> c1 >> c2;
25     cout << "c1 = " << c1 << endl;
26     cout << "c2 = " << c2 << endl;
27 
28     cout << "c1.real = " << c1.get_real() << endl;
29     cout << "c1.imag = " << c1.get_imag() << endl;
30 }
31 
32 int main() {
33     cout << "自定義類别範本Complex測試1: " << endl;
34     test1();
35 
36     cout << endl;
37 
38     cout << "自定義類别範本Complex測試2: " << endl;
39     test2();
40 }
task5.cpp

實驗任務6:

date.h

實驗5 繼承和多型
 1 //date.h
 2 #ifndef _DATE_H_
 3 #define _DATE_H_
 4 class Date
 5 {
 6 private:
 7         int year;
 8         int month;
 9         int day;
10         int totalDays;
11 public:
12     Date(int year,int month,int day);
13     int getYear()const{return year;}
14     int getMonth()const{return month;}
15     int getDay()const {return day;}
16     int getMaxDay()const;
17     bool isLeapYear()const
18     {
19         return year%4==0&&year%100!=0||year%400==0;
20     }
21     void show()const;
22     int operator-(const Date&date)const
23     {
24         return totalDays-date.totalDays;
25     }
26  };
27  
28  #endif//_DATE_H_ 
date.h

date.cpp

實驗5 繼承和多型
 1 #include"date.h"
 2 #include<iostream>
 3 #include<cstdlib>
 4 using namespace std;
 5 namespace
 6 {
 7     const int DAYS_BEFORE_MONTH[]={0,31,59,90,120,151,181,212,243,273,304,334,365};
 8 }
 9 
10 Date::Date(int year,int month,int day):year{year},month{month},day{day}
11 {
12     if(day<=0||day>getMaxDay())
13     {
14         cout<<"Invalid date:";
15         show();
16         cout<<endl;
17         exit(1);
18     }
19     int years=year-1;
20     totalDays=years*365+years/4-years/100+years/400+DAYS_BEFORE_MONTH[month-1]+day;
21     if(isLeapYear()&&month>2)totalDays++; 
22 }
23 
24 int Date::getMaxDay()const
25 {
26     if(isLeapYear()&&month==2){return 29;
27     }
28     else{return DAYS_BEFORE_MONTH[month]-DAYS_BEFORE_MONTH[month-1];
29     }
30 }
31 
32 void Date::show()const
33 {
34     cout<<getYear()<<"-"<<getMonth()<<"-"<<getDay();
35 }
date.cpp

accumulator.h

實驗5 繼承和多型
 1 //accumulator.h
 2 #ifndef _ACCUMULATOR_H_
 3 #define _ACCUMULATOR_H_
 4 #include "date.h"
 5 
 6 class Accumulator {
 7     //將某個數值按日累加
 8 private:
 9     Date lastDate;     //上次變更數值的時期
10     double value;      //數值的當前值
11     double sum;        //數值按日累加之和
12 public:
13     //建構函式,date為開始累加的日期,value為初始值
14     Accumulator(const Date &date, double value)
15         : lastDate(date), value(value), sum(0) {}
16     //獲得日期date的累加結果
17     double getSum(const Date &date) const {
18         return sum + value * (date-lastDate);
19     }
20     //在date將數值變更為value
21     void change(const Date &date, double value) {
22         sum = getSum(date);
23         lastDate = date;
24         this->value = value;
25     }
26     //初始化,將日期變為date,數值變為value,累加器清零
27     void reset(const Date &date, double value) {
28         lastDate = date;
29         this->value = value;
30         sum = 0;
31     }
32 };
33 #endif //_ACCUMULATOR_H_
accumulator.h

account.h

實驗5 繼承和多型
 1 //accouth.h
 2 #ifndef _ACCOUNT_H_
 3 #define _ACCOUNT_H_
 4 #include "date.h"
 5 #include "accumulator.h"
 6 #include <string>
 7 
 8 class Account {
 9 private:
10     std::string id;
11     double balance;
12     static double total;
13 protected:
14     // 建構函式
15     Account(const Date& date, const std::string& id);
16     void record(const Date& date, double amount, const std::string& desc);
17     // 報告錯誤資訊
18     void error(const std::string& msg) const;
19 public:
20     // 獲取賬戶ID
21     const std::string& getId() const;
22     // 獲取賬戶餘額
23     double getBalance() const;
24     // 獲取所有賬戶的總金額
25     static double getTotal();
26     // 存入現金,date為日期,amount為金額,desc為款項說明
27     virtual void deposit(const Date& date, double amount, const std::string& desc)=0;
28     // 取出現金,date為日期,amount為金額,desc為款項說明
29     virtual void withdraw(const Date& date, double amount, const std::string& desc)=0;
30     // 結算(計算利息、年費等),每月結算一次,date為結算日期
31     virtual void settle(const Date& date)=0;
32     // 顯示賬戶資訊
33     virtual void show() const;
34 };
35 class SavingsAccount:public Account
36 {
37 private:
38     Accumulator acc;        //輔助計算利息的累加器
39     double rate;            //存款的年利率
40 public:
41     //建構函式
42     SavingsAccount(const Date &date, const std::string &id, double rate);
43     double getRate() const { return rate; }
44     //存入現金
45     void deposit(const Date &date, double amount, const std::string &desc);
46     //取出現金
47     void withdraw(const Date &date, double amount, const std::string &desc);
48     //結算利息,每年1月1日呼叫一次該函式
49     void settle(const Date &date);
50 };
51 
52 class CreditAccount : public Account {    //信用賬戶類
53 private:
54     Accumulator acc;        //輔助計算利息的累加器
55     double credit;          //信用額度
56     double rate;            //欠款的日利率
57     double fee;             //信用卡年費
58     double getDebt() const {
59         double balance = getBalance();
60         return (balance < 0? balance : 0);
61     }
62 public:
63     //建構函式
64     CreditAccount(const Date &date, const std::string &id, double credit, double rate, double fee);
65     double getCredit() const { return credit; }
66     double getRate() const { return rate; }
67     double getFee() const { return fee; }
68     double getAvailableCredit() const {
69     if (getBalance() < 0)
70         return credit + getBalance();
71     else
72         return credit;
73     }
74     //存入現金
75     void deposit(const Date &date, double amount, const std::string &desc);
76     //取出現金
77     void withdraw(const Date &date, double amount, const std::string &desc);
78     void settle(const Date &date);
79     //顯示賬戶資訊
80     void show() const;
81 };
82 #endif
account.h

account.cpp

實驗5 繼承和多型
  1 //account.cpp
  2 #include "account.h"
  3 #include <cmath>
  4 #include <iostream>
  5 using namespace std;
  6 
  7 double Account::total = 0;
  8 
  9 //Account類的實現
 10 Account::Account(const Date &date, const string &id)
 11     : id(id), balance(0) {
 12     date.show();
 13     cout << "\t#" << id << " created" << endl;
 14 }
 15 
 16 void Account::record(const Date &date, double amount, const string &desc) {
 17     amount = floor(amount * 100 + 0.5) / 100; //保留小數點後兩位
 18     balance += amount;
 19     total += amount;
 20     date.show();
 21     cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
 22 }
 23 
 24 void Account::show() const {
 25     cout << id << "\tBalance: " << balance;
 26 }
 27 
 28 void Account::error(const string &msg) const {
 29     cout << "Error(#" << id << "): " << msg << endl;
 30 }
 31 
 32 double Account::getBalance()const
 33 {
 34     return balance;
 35 }
 36 
 37 double Account::getTotal()
 38 {
 39     return total;
 40 }
 41 
 42 //SavingsAccount類相關成員函式的實現
 43 SavingsAccount::SavingsAccount(const Date &date, const string &id, double rate)
 44     : Account(date, id), rate(rate), acc(date, 0) {}
 45 
 46 void SavingsAccount::deposit(const Date &date, double amount, const string &desc) {
 47     record(date, amount, desc);
 48     acc.change(date, getBalance());
 49 }
 50 void SavingsAccount::withdraw(const Date &date, double amount, const string &desc) {
 51     if (amount > getBalance()) {
 52         error("not enough money");
 53     } else {
 54         record(date, -amount, desc);
 55         acc.change(date, getBalance());
 56     }
 57 }
 58 
 59 void SavingsAccount::settle(const Date &date) {
 60     if(date.getMonth()==1)
 61     {
 62         double interest=acc.getSum(date)*rate/(date-Date(date.getYear()-1,1,1));
 63         if(interest!=0)
 64         {
 65             record(date,interest,"interest");
 66         }
 67         acc.reset(date,getBalance());
 68     }
 69 }
 70 
 71 //CreditAccount類相關成員函式的實現
 72 CreditAccount::CreditAccount(const Date &date, const string &id, double credit, double rate, double fee)
 73     : Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) {}
 74 
 75 void CreditAccount::deposit(const Date &date, double amount, const string &desc) {
 76     record(date, amount, desc);
 77     acc.change(date, getDebt());
 78 }
 79 
 80 void CreditAccount::withdraw(const Date &date, double amount, const string &desc) {
 81     if (amount - getBalance() > credit) {
 82         error("not enough credit");
 83     } else {
 84         record(date, -amount, desc);
 85         acc.change(date, getDebt());
 86     }
 87 }
 88 
 89 void CreditAccount::settle(const Date &date) {
 90     double interest = acc.getSum(date) * rate;
 91     if (interest!= 0)
 92         record(date, interest, "interest");
 93     if (date.getMonth() == 1)
 94         record(date, -fee, "annual fee");
 95     acc.reset(date, getDebt());
 96 }
 97 
 98 void CreditAccount::show() const {
 99     Account::show();
100     cout << "\tAvailable credit: " << getAvailableCredit();
101 }
account.cpp

8_8.cpp

實驗5 繼承和多型
 1 // 8_8.cpp
 2 #include "account.h"
 3 #include <iostream>
 4 using namespace std;
 5 
 6 int main() {
 7     Date date(2008, 11, 1); // 起始日期
 8 
 9     // 建立幾個賬戶
10     SavingsAccount sa1(date, "S3755217", 0.015);
11     SavingsAccount sa2(date, "02342342", 0.015);
12     CreditAccount ca(date, "C5392394", 10000, 0.0005, 50);
13     Account *accounts[] = {&sa1, &sa2, &ca};
14 
15     const int n = sizeof(accounts) / sizeof(Account *);
16 
17     cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl;
18 
19     char cmd;
20     do {
21         // 顯示日期和總金額
22         date.show();
23         cout << "\tTotal: " << Account::getTotal() << "\tcommand>";
24 
25         int index, day;
26         double amount;
27         string desc;
28         cin >> cmd;
29 
30         switch (cmd) {
31         case 'd': // 存入現金
32             cin >> index >> amount;
33             getline(cin, desc);
34             accounts[index]->deposit(date, amount, desc);
35             break;
36         case 'w': // 取出現金
37             cin >> index >> amount;
38             getline(cin, desc);
39             accounts[index]->withdraw(date, amount, desc);
40             break;
41         case 's': // 查詢各賬戶資訊
42             for (int i = 0; i < n; i++) {
43                 cout << "[" << i << "] ";
44                 accounts[i]->show();
45                 cout << endl;
46             }
47             break;
48         case 'c': // 改變日期
49             cin >> day;
50             if (day < date.getDay())
51                 cout << "You cannot specify a previous day";
52             else if (day > date.getMaxDay())
53                 cout << "Invalid day";
54             else
55                 date = Date(date.getYear(), date.getMonth(), day);
56             break;
57         case 'n': // 進入下個月
58             if (date.getMonth() == 12)
59                 date = Date(date.getYear() + 1, 1, 1);
60             else
61                 date = Date(date.getYear(), date.getMonth() + 1, 1);
62             for (int i = 0; i < n; i++)
63                 accounts[i]->settle(date);
64             break;
65         }
66     } while (cmd!= 'e');
67 
68     return 0;
69 }
8_8.cpp

實驗任務7:

container.h

實驗5 繼承和多型
 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
container.h

container.cpp

實驗5 繼承和多型
 1 //=======================
 2 //        container.cpp
 3 //=======================
 4 #include"container.h"
 5 #include<iostream>
 6 using std::cout;
 7 using std::endl;
 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 }
container.cpp

player.h

實驗5 繼承和多型
 1 //=======================
 2 //        player.h
 3 //=======================
 4 #include<string>
 5 using std::string;
 6 // The base class of player
 7 // including the general properties and methods related to a character
 8 
 9 #ifndef _PLAYER
10 #define _PLAYER
11 
12 #include <iomanip> // use for setting field width
13 #include <time.h>  // use for generating random factor
14 #include "container.h"
15 
16 enum job
17 {
18     sw,
19     ar,
20     mg
21 }; /* define 3 jobs by enumerate type
22           sword man, archer, mage */
23 class player
24 {
25     friend void showinfo(player &p1, player &p2);
26     friend class swordsman;
27 
28 protected:
29     int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV;
30     // General properties of all characters
31     string name;   // character name
32     job role;       /* character's job, one of swordman, archer and mage,
33                       as defined by the enumerate type */
34     container bag; // character's inventory
35 
36 public:
37     virtual bool attack(player &p) = 0;        // normal attack
38     virtual bool specialatt(player &p) = 0; // special attack
39     virtual void isLevelUp() = 0;            // level up judgement
40     /* Attention!
41     These three methods are called "Pure virtual functions".
42     They have only declaration, but no definition.
43     The class with pure virtual functions are called "Abstract class", which can only be used to inherited, but not to constructor objects.
44     The detailed definition of these pure virtual functions will be given in subclasses. */
45 
46     void reFill();              // character's HP and MP resume
47     bool death();              // report whether character is dead
48     void isDead();              // check whether character is dead
49     bool useHeal();              // consume heal, irrelevant to job
50     bool useMW();              // consume magic water, irrelevant to job
51     void transfer(player &p); // possess opponent's items after victory
52     void showRole();          // display character's job
53 
54 private:
55     bool playerdeath; // whether character is dead, doesn't need to be accessed or inherited
56 };
57 
58 #endif
player.h

player.cpp

實驗5 繼承和多型
  1 //=======================
  2 //        player.cpp
  3 //=======================
  4 #include"player.h"
  5 #include<iostream>
  6 #include<iomanip>
  7 using std::setw;
  8 using std::cout;
  9 using std::endl;
 10 // character's HP and MP resume
 11 void player::reFill()
 12 {
 13     HP = HPmax; // HP and MP fully recovered
 14     MP = MPmax;
 15 }
 16 
 17 // report whether character is dead
 18 bool player::death()
 19 {
 20     return playerdeath;
 21 }
 22 
 23 // check whether character is dead
 24 void player::isDead()
 25 {
 26     if (HP <= 0) // HP less than 0, character is dead
 27     {
 28         cout << name << " is Dead." << endl;
 29         system("pause");
 30         playerdeath = 1; // give the label of death value 1
 31     }
 32 }
 33 
 34 // consume heal, irrelevant to job
 35 bool player::useHeal()
 36 {
 37     if (bag.nOfHeal() > 0)
 38     {
 39         HP = HP + 100;
 40         if (HP > HPmax) // HP cannot be larger than maximum value
 41             HP = HPmax; // so assign it to HPmax, if necessary
 42         cout << name << " used Heal, HP increased by 100." << endl;
 43         bag.useHeal(); // use heal
 44         system("pause");
 45         return 1; // usage of heal succeed
 46     }
 47     else // If no more heal in bag, cannot use
 48     {
 49         cout << "Sorry, you don't have heal to use." << endl;
 50         system("pause");
 51         return 0; // usage of heal failed
 52     }
 53 }
 54 
 55 // consume magic water, irrelevant to job
 56 bool player::useMW()
 57 {
 58     if (bag.nOfMW() > 0)
 59     {
 60         MP = MP + 100;
 61         if (MP > MPmax)
 62             MP = MPmax;
 63         cout << name << " used Magic Water, MP increased by 100." << endl;
 64         bag.useMW();
 65         system("pause");
 66         return 1; // usage of magic water succeed
 67     }
 68     else
 69     {
 70         cout << "Sorry, you don't have magic water to use." << endl;
 71         system("pause");
 72         return 0; // usage of magic water failed
 73     }
 74 }
 75 
 76 // possess opponent's items after victory
 77 void player::transfer(player &p)
 78 {
 79     cout << name << " got" << p.bag.nOfHeal() << " Heal, and " << p.bag.nOfMW() << " Magic Water." << endl;
 80     system("pause");
 81     // set the character's bag, get opponent's items
 82     bag.set(bag.nOfHeal() + p.bag.nOfHeal(), bag.nOfMW() + p.bag.nOfMW());
 83     
 84 }
 85 
 86 // display character's job
 87 void player::showRole()
 88 {
 89     switch (role)
 90     {
 91     case sw:
 92         cout << "Swordsman";
 93         break;
 94     case ar:
 95         cout << "Archer";
 96         break;
 97     case mg:
 98         cout << "Mage";
 99         break;
100     default:
101         break;
102     }
103 }
104 
105 // display character's job
106 void showinfo(player& p1, player& p2)
107 {
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.cpp

swordsman.h

實驗5 繼承和多型
 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 };
swordsman.h

swordsman.cpp

實驗5 繼承和多型
  1 //=======================
  2 //        swordsman.cpp
  3 //=======================
  4 #include"swordsman.h"
  5 #include<iostream>
  6 #include<string>
  7 using std::string;
  8 using std::cout;
  9 using std::endl;
 10 // constructor. default values don't need to be repeated here
 11 swordsman::swordsman(int lv_in, string name_in)
 12 {
 13     role = sw; // enumerate type of job
 14     LV = lv_in;
 15     name = name_in;
 16 
 17     // Initialising the character's properties, based on his level
 18     HPmax = 150 + 8 * (LV - 1); // HP increases 8 point2 per level
 19     HP = HPmax;
 20     MPmax = 75 + 2 * (LV - 1); // MP increases 2 points per level
 21     MP = MPmax;
 22     AP = 25 + 4 * (LV - 1);       // AP increases 4 points per level
 23     DP = 25 + 4 * (LV - 1);       // DP increases 4 points per level
 24     speed = 25 + 2 * (LV - 1); // speed increases 2 points per level
 25 
 26     playerdeath = 0;
 27     EXP = LV * LV * 75;
 28     bag.set(lv_in, lv_in);
 29 }
 30 
 31 void swordsman::isLevelUp()
 32 {
 33     if (EXP >= LV * LV * 75)
 34     {
 35         LV++;
 36         AP += 4;
 37         DP += 4;
 38         HPmax += 8;
 39         MPmax += 2;
 40         speed += 2;
 41         cout << name << " Level UP!" << endl;
 42         cout << "HP improved 8 points to " << HPmax << endl;
 43         cout << "MP improved 2 points to " << MPmax << endl;
 44         cout << "Speed improved 2 points to " << speed << endl;
 45         cout << "AP improved 4 points to " << AP << endl;
 46         cout << "DP improved 5 points to " << DP << endl;
 47         system("pause");
 48         isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp
 49     }
 50 }
 51 
 52 bool swordsman::attack(player &p)
 53 {
 54     double HPtemp = 0;             // opponent's HP decrement
 55     double EXPtemp = 0;             // player obtained exp
 56     double hit = 1;                 // attach factor, probably give critical attack
 57     srand((unsigned)time(NULL)); // generating random seed based on system time
 58 
 59     // If speed greater than opponent, you have some possibility to do double attack
 60     if ((speed > p.speed) && (rand() % 100 < (speed - p.speed))) // rand()%100 means generates a number no greater than 100
 61     {
 62         HPtemp = (int)((1.0 * AP / p.DP) * AP * 5 / (rand() % 4 + 10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance
 63         cout << name << "'s quick strike hit " << p.name << ", " << p.name << "'s HP decreased " << HPtemp << endl;
 64         p.HP = int(p.HP - HPtemp);
 65         EXPtemp = (int)(HPtemp * 1.2);
 66     }
 67 
 68     // If speed smaller than opponent, the opponent has possibility to evade
 69     if ((speed < p.speed) && (rand() % 50 < 1))
 70     {
 71         cout << name << "'s attack has been evaded by " << p.name << endl;
 72         system("pause");
 73         return 1;
 74     }
 75 
 76     // 10% chance give critical attack
 77     if (rand() % 100 <= 10)
 78     {
 79         hit = 1.5;
 80         cout << "Critical attack: ";
 81     }
 82 
 83     // Normal attack
 84     HPtemp = (int)((1.0 * AP / p.DP) * AP * 5 / (rand() % 4 + 10));
 85     cout << name << " uses bash, " << p.name << "'s HP decreases " << HPtemp << endl;
 86     EXPtemp = (int)(EXPtemp + HPtemp * 1.2);
 87     p.HP = (int)(p.HP - HPtemp);
 88     cout << name << " obtained " << EXPtemp << " experience." << endl;
 89     EXP = (int)(EXP + EXPtemp);
 90     system("pause");
 91     return 1; // Attack success
 92 }
 93 
 94 bool swordsman::specialatt(player &p)
 95 {
 96     if (MP < 40)
 97     {
 98         cout << "You don't have enough magic points!" << endl;
 99         system("pause");
100         return 0; // Attack failed
101     }
102     else
103     {
104         MP -= 40; // consume 40 MP to do special attack
105 
106         // 10% chance opponent evades
107         if (rand() % 100 <= 10)
108         {
109             cout << name << "'s leap attack has been evaded by " << p.name << endl;
110             system("pause");
111             return 1;
112         }
113 
114         double HPtemp = 0;
115         double EXPtemp = 0;
116         // double hit=1;
117         // srand(time(NULL));
118         HPtemp = (int)(AP * 1.2 + 20); // not related to opponent's DP
119         EXPtemp = (int)(HPtemp * 1.5); // special attack provides more experience
120         cout << name << " uses leap attack, " << p.name << "'s HP decreases " << HPtemp << endl;
121         cout << name << " obtained " << EXPtemp << " experience." << endl;
122         p.HP = (int)(p.HP - HPtemp);
123         EXP = (int)(EXP + EXPtemp);
124         system("pause");
125     }
126     return 1; // special attack succeed
127 }
128 
129 // Computer opponent
130 void swordsman::AI(player &p)
131 {
132     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)))
133     // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
134     {
135         useHeal();
136     }
137     else
138     {
139         if (MP >= 40 && HP > 0.5 * HPmax && rand() % 100 <= 30)
140         // AI has enough MP, it has 30% to make special attack
141         {
142             specialatt(p);
143             p.isDead(); // check whether player is dead
144         }
145         else
146         {
147             if (MP < 40 && HP > 0.5 * HPmax && bag.nOfMW())
148             // Not enough MP && HP is safe && still has magic water
149             {
150                 useMW();
151             }
152             else
153             {
154                 attack(p); // normal attack
155                 p.isDead();
156             }
157         }
158     }
159 }
swordsman.cpp

main.cpp

實驗5 繼承和多型
  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 #include"player.h"
 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=nullptr;     // 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); // 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
100                      << 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     delete human; // 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 }
main.cpp

相關文章