回分類題庫
d282: 104OOP Lab Exercise:Multiple Inheritance Practice
出處:

Difficulity : N/A
Accepted : 62 Times | Submit :115 Times | Clicks : 662
Accepted : 58 Users | Submit : 58 Users | Accepted rate : 100%
Time Limit :10000 ms | Memory Limit : 64000 KBytes
題目加入時間 : 2015-12-09 17:20

Content :

現有一個Soldier類別
Soldier類別擁有兩個私有成員
-name,型態為string
-stamina,型態為int
-可以透過getName取得name字串的內容
-可以透過getStamina取得stamina的數值
-可以透過setStamina設定stamina的數值
 
練習多重繼承以及虛擬繼承
Cavalry類別繼承Soldier類別
Archer類別繼承Soldier類別
CavalryArcher類別繼承Cavalry和Archer類別
Cavalry類別的私有成員為horseRank,型態為int
Archer類別的私有成員為arrowNumber,型態為int
CavalryArcher類別的私有成員為horsebackArcherySkill,型態為int
 
 完成下列程式碼
 
#include <iostream>
#include <vector>
using namespace std;

// class Soldier ......... your code
// class Cavalry ......... your code
// class Archer .......... your code
// class CavalryArcher ... your code

void printData(const CavalryArcher &cavalryArcher);
int main()
{
    string name;
    int stamina, horseRank, arrowNumber, horsebackArcherySkill;
    int command;
    while(cin >> name >> stamina >> horseRank >> arrowNumber >> horsebackArcherySkill){
        CavalryArcher cavalryArcher(name, stamina, horseRank, arrowNumber, horsebackArcherySkill);
        printData(cavalryArcher);
        while(1){
            // Input your command(1: setStamina, 2: setHorseRank, 3: setHorseRank, 4: setHorsebackArcherySkill, 5: exit program)
            cin >> command;
            switch(command)
            {
                case 1:
                    cin >> stamina;
                    cavalryArcher.setStamina(stamina);
                    printData(cavalryArcher);
                    break;
                case 2:
                    cin >> horseRank;
                    cavalryArcher.setHorseRank(horseRank);
                    printData(cavalryArcher);
                    break;
                case 3:
                    cin >> arrowNumber;
                    cavalryArcher.setArrowNumber(arrowNumber);
                    printData(cavalryArcher);
                    break;
                case 4:
                    cin >> horsebackArcherySkill;
                    cavalryArcher.setHorsebackArcherySkill(horsebackArcherySkill);
                    printData(cavalryArcher);
                    break;
                case 5:
                    printData(cavalryArcher);
                    return 0;
                default:
                    break;
            }
        }
    }
    return 0;
}
void printData(const CavalryArcher &cavalryArcher){
    cout << "==============CavalryArcher==============" << endl;
    cout << "Name = " << cavalryArcher.getName() << endl;
    cout << "Stamina = " << cavalryArcher.getStamina() << endl;
    cout << "HorseRank = " << cavalryArcher.getHorseRank() << endl;
    cout << "ArrowNumber = " << cavalryArcher.getArrowNumber() << endl;
    cout << "HorsebackArcherySkill = " << cavalryArcher.getHorsebackArcherySkill() << endl;
    cout << "======================================" << endl;
}




 

Input :

第一行是CavalryArcher的基本屬性,格式如下
name stamina horseRank arrowNumber horsebackArcherySkill
例: John 100 5 20 80
第二行之後為指令,指令代號及內容如下
1: setStamina,格式:1 stamina,例如:1 100
2: setHorseRank,格式:2 horseRank,例如:2 5
3: setArrowNumber,格式:3 arrowNumber,例如:3 20
4: setHorsebackArcherySkill,格式:4 horsebackArcherySkill,例如:4 80
5: exit program

Output :

-每一筆的CavalryArcher在建立時需依正確順序印出constructor
-同理, CavalryArcher在銷毀時,需依正確順序印出destructor
-執行任一行指令,都要呼叫printData印出CavalryArcher目前狀態

Sample Input :

John 100 5 20 80
1 80
4 85
5
Tom 70 2 30 75
1 90
3 35
5

Sample Output :

Constructor of Soldier
Constructor of Cavalry
Constructor of Archer
Constructor of CavalryArcher
==============CavalryArcher==============
Name = John
Stamina = 100
HorseRank = 5
ArrowNumber = 20
HorsebackArcherySkill = 80
======================================
==============CavalryArcher==============
Name = John
Stamina = 80
HorseRank = 5
ArrowNumber = 20
HorsebackArcherySkill = 80
======================================
==============CavalryArcher==============
Name = John
Stamina = 80
HorseRank = 5
ArrowNumber = 20
HorsebackArcherySkill = 85
======================================
==============CavalryArcher==============
Name = John
Stamina = 80
HorseRank = 5
ArrowNumber = 20
HorsebackArcherySkill = 85
======================================
Destructor of CavalryArcher
Destructor of Archer
Destructor of Cavalry
Destructor of Soldier
Constructor of Soldier
Constructor of Cavalry
Constructor of Archer
Constructor of CavalryArcher
==============CavalryArcher==============
Name = Tom
Stamina = 70
HorseRank = 2
ArrowNumber = 30
HorsebackArcherySkill = 75
======================================
==============CavalryArcher==============
Name = Tom
Stamina = 90
HorseRank = 2
ArrowNumber = 30
HorsebackArcherySkill = 75
======================================
==============CavalryArcher==============
Name = Tom
Stamina = 90
HorseRank = 2
ArrowNumber = 35
HorsebackArcherySkill = 75
======================================
==============CavalryArcher==============
Name = Tom
Stamina = 90
HorseRank = 2
ArrowNumber = 35
HorsebackArcherySkill = 75
======================================
Destructor of CavalryArcher
Destructor of Archer
Destructor of Cavalry
Destructor of Soldier

Hint :


  

Author :

(管理員:oopTA)

  Solve it!   Status Forum (0)

C++
C
JAVA
70357. oop103703033 (7 ms , 428KB)
70346. oop103703005 (7 ms , 424KB)
70339. oop103703014 (7 ms , 432KB)
70317. oop103703028_ (7 ms , 426KB)
70313. oop101703023_1 (7 ms , 430KB)
沒有解題記錄 86392. nwgs524513cja (84 ms , 266KB)

執行時間會受很多因素影響因此僅供參考,主機等級請看這裡