首页 > 其他分享 >实验5 继承和多态

实验5 继承和多态

时间:2022-11-29 21:27:31浏览次数:35  
标签:string 继承 void 多态 player int 实验 include cout

实验任务4

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 
 6 class MachinePets {
 7 private:
 8     string nickname;
 9 public:
10     string get_nickname() { return nickname; }
11     MachinePets(const string s) :nickname{ s }{}
12     virtual string talk() = 0;
13 };
14 
15 class PetCats :public MachinePets {
16 public:
17     PetCats(const string s) :MachinePets{ s }{}
18     string talk(){
19         return "miao wu~";
20     }
21 };
22 
23 class PetDogs :public MachinePets {
24 public:
25     PetDogs(const string s) :MachinePets{ s } {}
26     string talk() {
27         return "wang wang~";
28     }
29 };
pets.hpp
 1 #include <iostream>
 2 #include "pets.hpp"
 3 void play(MachinePets& obj) {
 4     std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl;
 5 }
 6 void test() {
 7     PetCats cat("miku");
 8     PetDogs dog("da huang");
 9     play(cat);
10     play(dog);
11 }
12 int main() {
13     test();
14 }
task4.cpp

测试截图:

 

 实验任务5

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 #include<vector>
 5 using namespace std;
 6 
 7 class Person {
 8 private:
 9     string name;
10     string telephone;
11     string email;
12 public:
13     Person(){}
14     Person(string name0, string telephone0);
15     Person(string name0, string telephone0, string email0);
16     Person(const Person& p);
17     void update_telephone();
18     void update_email();
19     
20     string get_name() const { return name; }
21     string get_telephone() const { return telephone; }
22     string get_email() const { return email; }
23 
24     friend std::ostream& operator<<(std::ostream& out, const Person& p);
25     friend std::istream& operator>>(std::istream& in, Person& p);
26     friend bool operator==(const Person& p1, const Person& p2);
27 };
28 Person::Person(string name0, string telephone0) {
29     name = name0;
30     telephone = telephone0;
31 }
32 
33 Person::Person(string name0, string telephone0, string email0) {
34     name = name0;
35     telephone = telephone0;
36     email = email0;
37 }
38 
39 Person::Person(const Person& p) {
40     name = p.name;
41     telephone = p.telephone;
42     email = p.email;
43     
44 }
45 
46 void Person::update_telephone() {
47     cout << "Enter the telephone number:";
48     cin.clear();
49     string t;
50     cin >> t;
51     telephone = t;
52     cout << "telephone number has been updated..." << endl;
53 }
54 
55 void Person::update_email() {
56     cout << "Enter the email address:";
57     cin.clear();
58     string e;
59     cin >> e;
60     email = e;
61     cout << "email adsress has been updated..." << endl;
62 }
63 
64 std::ostream& operator<<(std::ostream& out, const Person& p) {
65     out << p.name << endl << p.telephone << endl << p.email << endl;
66     return out;
67 }
68 
69 std::istream& operator>>(std::istream& in, Person& p) {
70     in >> p.name;
71     cin.sync();
72     in >> p.telephone;
73     cin.sync();
74     in >> p.email;
75     cout << endl;
76     return in;
77 }
78 bool operator==(const Person& p1, const Person& p2) {
79     return (p1.get_name() == p2.get_name() && p1.get_telephone() == p2.get_telephone());
80 }
Person.hpp
 1 #include <iostream>
 2 #include "pets.hpp"
 3 void play(MachinePets& obj) {
 4     std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl;
 5 }
 6 void test() {
 7     PetCats cat("miku");
 8     PetDogs dog("da huang");
 9     play(cat);
10     play(dog);
11 }
12 int main() {
13     test();
14 }
task5.cpp

测试截图:

 

 

实验任务6

 1 #pragma once
 2 //=======================
 3 //        container.h
 4 //=======================
 5 
 6 // The so-called inventory of a player in RPG games
 7 // contains two items, heal and magic water
 8 
 9 #ifndef _CONTAINER
10 #define _CONTAINER
11 
12 class container        // Inventory
13 {
14 protected:
15     int numOfHeal;            // number of heal
16     int numOfMW;            // number of magic water
17 public:
18     container();            // constuctor
19     void set(int heal_n, int mw_n);    // set the items numbers
20     int nOfHeal();            // get the number of heal
21     int nOfMW();            // get the number of magic water
22     void display();            // display the items;
23     bool useHeal();            // use heal
24     bool useMW();            // use magic water
25 };
26 
27 #endif
container.h
 1 #pragma once
 2 //=======================
 3 //        swordsman.h
 4 //=======================
 5 
 6 // Derived from base class player
 7 // For the job Swordsman
 8 #include<string>
 9 #include "player.h"
10 
11 using std::string;
12 
13 class swordsman : public player        // subclass swordsman publicly inherited from base player
14 {
15 public:
16     swordsman(int lv_in = 1, string name_in = "Not Given");
17     // constructor with default level of 1 and name of "Not given"
18     void isLevelUp();
19     bool attack(player& p);
20     bool specialatt(player& p);
21     /* These three are derived from the pure virtual functions of base class
22        The definition of them will be given in this subclass. */
23     void AI(player& p);                // Computer opponent
24 };
swordsman.h
 1 #pragma once
 2 //=======================
 3 //        player.h
 4 //=======================
 5 
 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<string>
13 #include <iomanip>        // use for setting field width
14 #include <time.h>      // use for generating random factor
15 
16 #include "container.h"
17 
18 using std::string;
19 
20 enum job { sw, ar, mg };    /* define 3 jobs by enumerate type
21                                sword man, archer, mage */
22 class player
23 {
24     friend void showinfo(player& p1, player& p2);
25     friend class swordsman;
26 
27 protected:
28     int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV;
29     // General properties of all characters
30     string name;    // character name
31     job role;        /* character's job, one of swordman, archer and mage,
32                        as defined by the enumerate type */
33     container bag;    // character's inventory
34 
35 public:
36     virtual bool attack(player& p) = 0;    // normal attack
37     virtual bool specialatt(player& p) = 0;    //special attack
38     virtual void isLevelUp() = 0;            // level up judgement
39     /* Attention!
40     These three methods are called "Pure virtual functions".
41     They have only declaration, but no definition.
42     The class with pure virtual functions are called "Abstract class", which can only be used to inherited, but not to constructor objects.
43     The detailed definition of these pure virtual functions will be given in subclasses. */
44 
45     void reFill();        // character's HP and MP resume
46     bool death();        // report whether character is dead
47     void isDead();        // check whether character is dead
48     bool useHeal();        // consume heal, irrelevant to job
49     bool useMW();        // consume magic water, irrelevant to job
50     void transfer(player& p);    // possess opponent's items after victory
51     void showRole();    // display character's job
52 
53 private:
54     bool playerdeath;            // whether character is dead, doesn't need to be accessed or inherited
55 };
56 
57 #endif
player.h
 1 //=======================
 2 //        container.cpp
 3 //=======================
 4 #include"container.h"
 5 #include<iostream>
 6 
 7 using std::cout;
 8 using std::endl;
 9 // default constructor initialise the inventory as empty
10 container::container()
11 {
12     set(0, 0);
13 }
14 
15 // set the item numbers
16 void container::set(int heal_n, int mw_n)
17 {
18     numOfHeal = heal_n;
19     numOfMW = mw_n;
20 }
21 
22 // get the number of heal
23 int container::nOfHeal()
24 {
25     return numOfHeal;
26 }
27 
28 // get the number of magic water
29 int container::nOfMW()
30 {
31     return numOfMW;
32 }
33 
34 // display the items;
35 void container::display()
36 {
37     cout << "Your bag contains: " << endl;
38     cout << "Heal(HP+100): " << numOfHeal << endl;
39     cout << "Magic Water (MP+80): " << numOfMW << endl;
40 }
41 
42 //use heal
43 bool container::useHeal()
44 {
45     numOfHeal--;
46     return 1;        // use heal successfully
47 }
48 
49 //use magic water
50 bool container::useMW()
51 {
52     numOfMW--;
53     return 1;        // use magic water successfully
54 }
container.cpp
  1 //=======================
  2 //        swordsman.cpp
  3 //=======================
  4 
  5 #include"swordsman.h"
  6 #include"player.h"
  7 #include<iostream>
  8 
  9 using std::cout;
 10 using std::endl;
 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)((1.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)((1.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     }
103     else
104     {
105         MP -= 40;            // consume 40 MP to do special attack
106 
107         //10% chance opponent evades
108         if (rand() % 100 <= 10)
109         {
110             cout << name << "'s leap attack has been evaded by " << p.name << endl;
111             system("pause");
112             return 1;
113         }
114 
115         double HPtemp = 0;
116         double EXPtemp = 0;
117         //double hit=1;            
118         //srand(time(NULL));        
119         HPtemp = (int)(AP * 1.2 + 20);        // not related to opponent's DP
120         EXPtemp = (int)(HPtemp * 1.5);        // special attack provides more experience
121         cout << name << " uses leap attack, " << p.name << "'s HP decreases " << HPtemp << endl;
122         cout << name << " obtained " << EXPtemp << " experience." << endl;
123         p.HP = (int)(p.HP - HPtemp);
124         EXP = (int)(EXP + EXPtemp);
125         system("pause");
126     }
127     return 1;    // special attack succeed
128 }
129 
130 // Computer opponent
131 void swordsman::AI(player& p)
132 {
133     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)))
134         // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
135     {
136         useHeal();
137     }
138     else
139     {
140         if (MP >= 40 && HP > 0.5 * HPmax && rand() % 100 <= 30)
141             // AI has enough MP, it has 30% to make special attack
142         {
143             specialatt(p);
144             p.isDead();        // check whether player is dead
145         }
146         else
147         {
148             if (MP < 40 && HP>0.5 * HPmax && bag.nOfMW())
149                 // Not enough MP && HP is safe && still has magic water
150             {
151                 useMW();
152             }
153             else
154             {
155                 attack(p);    // normal attack
156                 p.isDead();
157             }
158         }
159     }
160 }
swordsman.cpp
  1 //=======================
  2 //        player.cpp
  3 //=======================
  4 
  5 #include"player.h"
  6 #include<iostream>
  7 
  8 using std::cout;
  9 using std::endl;
 10 using std::setw;
 11 // character's HP and MP resume
 12 void player::reFill()
 13 {
 14     HP = HPmax;        // HP and MP fully recovered
 15     MP = MPmax;
 16 }
 17 
 18 // report whether character is dead
 19 bool player::death()
 20 {
 21     return playerdeath;
 22 }
 23 
 24 // check whether character is dead
 25 void player::isDead()
 26 {
 27     if (HP <= 0)        // HP less than 0, character is dead
 28     {
 29         cout << name << " is Dead." << endl;
 30         system("pause");
 31         playerdeath = 1;    // give the label of death value 1
 32     }
 33 }
 34 
 35 // consume heal, irrelevant to job
 36 bool player::useHeal()
 37 {
 38     if (bag.nOfHeal() > 0)
 39     {
 40         HP = HP + 100;
 41         if (HP > HPmax)        // HP cannot be larger than maximum value
 42             HP = HPmax;        // so assign it to HPmax, if necessary
 43         cout << name << " used Heal, HP increased by 100." << endl;
 44         bag.useHeal();        // use heal
 45         system("pause");
 46         return 1;    // usage of heal succeed
 47     }
 48     else                // If no more heal in bag, cannot use
 49     {
 50         cout << "Sorry, you don't have heal to use." << endl;
 51         system("pause");
 52         return 0;    // usage of heal failed
 53     }
 54 }
 55 
 56 // consume magic water, irrelevant to job
 57 bool player::useMW()
 58 {
 59     if (bag.nOfMW() > 0)
 60     {
 61         MP = MP + 100;
 62         if (MP > MPmax)
 63             MP = MPmax;
 64         cout << name << " used Magic Water, MP increased by 100." << endl;
 65         bag.useMW();
 66         system("pause");
 67         return 1;    // usage of magic water succeed
 68     }
 69     else
 70     {
 71         cout << "Sorry, you don't have magic water to use." << endl;
 72         system("pause");
 73         return 0;    // usage of magic water failed
 74     }
 75 }
 76 
 77 // possess opponent's items after victory
 78 void player::transfer(player& p)
 79 {
 80     cout << name << " got" << p.bag.nOfHeal() << " Heal, and " << p.bag.nOfMW() << " Magic Water." << endl;
 81     system("pause");
 82     bag.set(p.bag.nOfHeal() + bag.nOfHeal(), p.bag.nOfMW() + bag.nOfMW());
 83     // set the character's bag, get opponent's items
 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 
106 // display character's job
107 void showinfo(player& p1, player& p2)
108 {
109     system("cls");
110     cout << "##############################################################" << endl;
111     cout << "# Player" << setw(10) << p1.name << "   LV. " << setw(3) << p1.LV
112         << "  # Opponent" << setw(10) << p2.name << "   LV. " << setw(3) << p2.LV << " #" << endl;
113     cout << "# HP " << setw(3) << (p1.HP <= 999 ? p1.HP : 999) << '/' << setw(3) << (p1.HPmax <= 999 ? p1.HPmax : 999)
114         << " | MP " << setw(3) << (p1.MP <= 999 ? p1.MP : 999) << '/' << setw(3) << (p1.MPmax <= 999 ? p1.MPmax : 999)
115         << "     # HP " << setw(3) << (p2.HP <= 999 ? p2.HP : 999) << '/' << setw(3) << (p2.HPmax <= 999 ? p2.HPmax : 999)
116         << " | MP " << setw(3) << (p2.MP <= 999 ? p2.MP : 999) << '/' << setw(3) << (p2.MPmax <= 999 ? p2.MPmax : 999) << "      #" << endl;
117     cout << "# AP " << setw(3) << (p1.AP <= 999 ? p1.AP : 999)
118         << " | DP " << setw(3) << (p1.DP <= 999 ? p1.DP : 999)
119         << " | speed " << setw(3) << (p1.speed <= 999 ? p1.speed : 999)
120         << " # AP " << setw(3) << (p2.AP <= 999 ? p2.AP : 999)
121         << " | DP " << setw(3) << (p2.DP <= 999 ? p2.DP : 999)
122         << " | speed " << setw(3) << (p2.speed <= 999 ? p2.speed : 999) << "  #" << endl;
123     cout << "# EXP" << setw(7) << p1.EXP << " Job: " << setw(7);
124     p1.showRole();
125     cout << "   # EXP" << setw(7) << p2.EXP << " Job: " << setw(7);
126     p2.showRole();
127     cout << "    #" << endl;
128     cout << "--------------------------------------------------------------" << endl;
129     p1.bag.display();
130     cout << "##############################################################" << endl;
131 }
player.cpp
  1 //=======================
  2 //        main.cpp
  3 //=======================
  4 
  5 // main function for the RPG style game
  6 
  7 #include <iostream>
  8 #include <string>
  9 #include "swordsman.h"
 10 using namespace std;
 11 
 12 int main()
 13 {
 14     string tempName;
 15     bool success = 0;        //flag for storing whether operation is successful
 16     cout << "Please input player's name: ";
 17     cin >> tempName;        // get player's name from keyboard input
 18     player* human;        // use pointer of base class, convenience for polymorphism
 19     int tempJob;        // temp choice for job selection
 20     do
 21     {
 22         cout << "Please choose a job: 1 Swordsman, 2 Archer, 3 Mage" << endl;
 23         cin >> tempJob;
 24         system("cls");        // clear the screen
 25         switch (tempJob)
 26         {
 27         case 1:
 28             human = new swordsman(1, tempName);    // create the character with user inputted name and job
 29             success = 1;        // operation succeed
 30             break;
 31         default:
 32             break;                // In this case, success=0, character creation failed
 33         }
 34     } while (success != 1);        // so the loop will ask user to re-create a character
 35 
 36     int tempCom;            // temp command inputted by user
 37     int nOpp = 0;                // the Nth opponent
 38     for (int i = 1; nOpp < 5; i += 2)    // i is opponent's level
 39     {
 40         nOpp++;
 41         system("cls");
 42         cout << "STAGE" << nOpp << endl;
 43         cout << "Your opponent, a Level " << i << " Swordsman." << endl;
 44         system("pause");
 45         swordsman enemy(i, "Warrior");    // Initialise an opponent, level i, name "Junior"
 46         human->reFill();                // get HP/MP refill before start fight
 47 
 48         while (!human->death() && !enemy.death())    // no died
 49         {
 50             success = 0;
 51             while (success != 1)
 52             {
 53                 showinfo(*human, enemy);                // show fighter's information
 54                 cout << "Please give command: " << endl;
 55                 cout << "1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game" << endl;
 56                 cin >> tempCom;
 57                 switch (tempCom)
 58                 {
 59                 case 0:
 60                     cout << "Are you sure to exit? Y/N" << endl;
 61                     char temp;
 62                     cin >> temp;
 63                     if (temp == 'Y' || temp == 'y')
 64                         return 0;
 65                     else
 66                         break;
 67                 case 1:
 68                     success = human->attack(enemy);
 69                     human->isLevelUp();
 70                     enemy.isDead();
 71                     break;
 72                 case 2:
 73                     success = human->specialatt(enemy);
 74                     human->isLevelUp();
 75                     enemy.isDead();
 76                     break;
 77                 case 3:
 78                     success = human->useHeal();
 79                     break;
 80                 case 4:
 81                     success = human->useMW();
 82                     break;
 83                 default:
 84                     break;
 85                 }
 86             }
 87             if (!enemy.death())        // If AI still alive
 88                 enemy.AI(*human);
 89             else                            // AI died
 90             {
 91                 cout << "YOU WIN" << endl;
 92                 human->transfer(enemy);        // player got all AI's items
 93             }
 94             if (human->death())
 95             {
 96                 system("cls");
 97                 cout << endl << setw(50) << "GAME OVER" << endl;
 98                 delete human;        // player is dead, program is getting to its end, what should we do here?
 99                 system("pause");
100                 return 0;
101             }
102         }
103     }
104     delete human;        // You win, program is getting to its end, what should we do here?
105     system("cls");
106     cout << "Congratulations! You defeated all opponents!!" << endl;
107     system("pause");
108     return 0;
109 }
main.cpp

 

测试截图: 

 

 

实验总结

该实验任务5中,发现连续输入name,telephone和email通过回车键完成输入时,'\n'会进入输入缓冲区从而进行下一个需输入的字符串,导致输入异常或无法输入的问题,针对此问题,我查阅课本及网络资料,总结出一下几种解决方法。

1.getline函数

istream类具有成员函数getline,其功能是允许从输人流中读取多个字符,并且允许指定输入终止字符(默认值是换行字符),在读取完成后,从读取的内容中删除该终止字符,然而该成员函数只能将输入结果存在字符数组中,字符数组的大小是不能自动扩展的,造成了使用上的不便。非成员函数getline能够完成相同的功能,但可以将结果保存在string类型的对象中,更加方便。这一函数可以接收两个参数,前两个分别表示输入流和保存结果的string对象,第三个参数可选,表示终止字符。使用非成员的getline函数的声明在string 头文件中。

2.getchar函数

在两次输入之间加一个getchar()函数,用getchar()将换行符吸收

3.cin.clear()和cin.sync()

cin.clear()的作用为清楚cin流错误状态的语句,cin.sync()的作用为清除缓存区的数据流。通常两个函数需联合起来使用。
 

标签:string,继承,void,多态,player,int,实验,include,cout
From: https://www.cnblogs.com/Terrence0403/p/16925932.html

相关文章

  • 实验五
    //=======================//player.h//=======================//Thebaseclassofplayer//includingthegeneralpropertiesandmethodsrelatedt......
  • 实验五
    pets.hpp#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststrings):nickname(s){}st......
  • C++多态性
    虚函数是C++中用于实现多态(polymorphism)的机制。核心理念就是通过基类访问派生类定义的函数。虚函数    是在基类中使用关键字virtual声明的函数。在派生类中重......
  • 实验5 继承与多态
    task4#pragmaonce#include<iostream>usingnamespacestd;classMachinePets{public:MachinePets(conststrings):nickname(s){}stringget_nickname(......
  • 对象原型 原型链 原型继承 constructor属性
    原型:   每个构造函数身上都有一个prototype原型  原型身上有一个对象   被称为原型对象(构造函数的this和原型上的this都指向实例化对象)<body>......
  • 实验5 继承和多态
    实验任务4:pets.hpp:#include<iostream>usingnamespacestd;classMachinePets{public:MachinePets(conststrings):nickname{s}{}virtual......
  • 实验五 多态和继承
    1#pragmaocne2#include<iostream>3#include<string>4usingnamespacestd;5classMachinePets6{7stringnickname;8public:9......
  • 实验五
    实验四pets.hpp1#pragmaonce2#include<iostream>3#include<string>4usingnamespacestd;5classMachinePets{6public:7MachinePets()=defau......
  • 实验四
    #include<stdio.h>#defineN2#defineM4intmain(){inta[N][M]={{1,9,8,4},{2,0,2,2}};charb[N][M]={{'1','9','8','4'},{'2','0','2','2'}......
  • 多线程的创建(继承Thread类)
    多线程的创建方式一:继承Thread类Java是通过java.lang.Thread类来代表线程的。按照面向对象的思想,Thread类应该提供了实现多线程的方式。步骤:定义一个子类MyThread继承......