回分類題庫
d288: 105OOP Lab Exercise: App Downloads(Inheritance)
出處:

Difficulity : N/A
Accepted : 55 Times | Submit :71 Times | Clicks : 481
Accepted : 51 Users | Submit : 51 Users | Accepted rate : 100%
Time Limit :10000 ms | Memory Limit : 64000 KBytes
題目加入時間 : 2016-11-16 21:25

Content :

請實作一父類別App及兩子類別JJBox和NoseBook

 

(以下,+表示public,-表示private) 

類別App的成員:

+ getDownload // 回傳download數值

+ increaseDownload // download數值+1

- download // int

 

 類別JJBox的成員:

+ getDownload // 回傳download數值

+ increaseDownload // download數值+1

- download // int

+ getSongs // 回傳songs數值

+ setSongs // songs數值 = download數值 * 5

- songs // int

 

 類別NoseBook的成員:

+ getDownload // 回傳download數值

+ increaseDownload // download數值+1

- download // int

+ getFriends // 回傳friends數值

+ setFriends // friends數值 = download數值 * 3 + 5

- friends // int

 

 

請依據輸入指令做成員數值改變,並在每次輸入指令皆印出當下的值。

需實作三個類別,兩個子類別分別初始一個物件。(父類別不用初始物件) 

 

請完成以下程式碼 

*************************************

#include <iostream>

using namespace std;

 

class App {

    public:

        App(int d) { download = d; }

        ~App(){};

        int getDownload() { return download; }

        void increaseDownload() { ++download; }

    private:

        int download;

};

 

/**

 *  PUT YOUR CODE BELOW

 **/

class JJBox {

 

    private:

        int songs;

};

 

class NoseBook {

 

    private:

        int friends;

};

 

int main() {

    int command;

    cin >> command;

    JJBox jjbox(command);

    cin >> command;

    NoseBook nosebook(command);

    cin >> command;

    while (command != -1) {

        switch(command) {

            case 1:

                jjbox.increaseDownload();

                break;

            case 2:

                nosebook.increaseDownload();

                break;

            case 3:

                jjbox.increaseDownload();

                nosebook.increaseDownload();

                break;

            case 4:

                jjbox.setSongs();

                break;

            case 5:

                nosebook.setFriends();

                break;

        }

        cout << "JJBox now has " << jjbox.getSongs() << " songs." << endl;

        cout << "NoseBook now has " << nosebook.getFriends() << " friends." << endl;

        cout << "==========" << endl;

 

        cin >> command;

    }

 

    return 0;

Input :

首行會有兩個數字

第一個數字表示JJBox物件預設download值

第二個數字表示NoseBook物件預設download值

 

第二行會出現不等數量的指令作以下運算: 

1 : JJBox物件download值+1

2 : NoseBook物件download值+1

3 : 兩物件download值皆+1

4 : 重設JJBox物件的songs數值

5 : 重設NoseBook物件的friends數值

-1 : 程式結束

********注意:download值增加時,不會同時更動到songs及friends數值。

********直到指令4,5時,才會去更動songs或friends數值。 

 

 

Output :

每個指令皆須印出三行:(XXX表示數值)

JJBox now has XXX songs.

NoseBook now has XXX friends.

========== 

 

*********** 第一行初始兩物件時不用輸出

*********** -1 程式結束不用輸出。 

Sample Input :

100 300
1 4 2 5 3 4 5 -1

Sample Output :

JJBox now has 500 songs.
NoseBook now has 905 friends.
==========
JJBox now has 505 songs.
NoseBook now has 905 friends.
==========
JJBox now has 505 songs.
NoseBook now has 905 friends.
==========
JJBox now has 505 songs.
NoseBook now has 908 friends.
==========
JJBox now has 505 songs.
NoseBook now has 908 friends.
==========
JJBox now has 510 songs.
NoseBook now has 908 friends.
==========
JJBox now has 510 songs.
NoseBook now has 911 friends.
==========

Hint :


  

Author :

(管理員:oopTA)

  Solve it!   Status Forum (0)

C++
C
JAVA
76081. oop104703033 (4 ms , 372KB)
76080. oop104703051 (4 ms , 368KB)
76040. oop104703035 (4 ms , 368KB)
76022. oop104703023 (4 ms , 366KB)
75926. oop104703012 (4 ms , 358KB)
沒有解題記錄 沒有解題記錄

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