回分類題庫
d228: 101OOP Lab Exercise: Class Inheritance
出處:

Difficulity : N/A
Accepted : 49 Times | Submit :65 Times | Clicks : 1003
Accepted : 45 Users | Submit : 45 Users | Accepted rate : 100%
Time Limit :10000 ms | Memory Limit : 64000 KBytes
題目加入時間 : 2012-11-25 19:46

Content :

We have written a class called "GameObject", that is record the GameObject’s coordinate – X and Y. You can also set X or set Y to modify the GameObject’s coordinate. And You can call GetX() and GetY() to get the value of GameObject’s coordinate.

Like the following code:

class GameObject {

    public:

        GameObject();

        GameObject(int X,int Y):m_X(X),m_Y(Y){ cout<<"Constructor of GameObject"<<endl;}

        ~GameObject();

        void SetX(int X);

        void SetY(int Y);

        int GetX();

        int GetY();

    private:

        int m_X;

        int m_Y;

};

GameObject::GameObject()

{

    m_X=0;

    m_Y=0;

    cout<<"Constructor of GameObject"<<endl;

}

GameObject::~GameObject()

{

    cout<<"Destructor of GameObject"<<endl;

}

void GameObject::SetX(int X)

{

    m_X=X;

}

void GameObject::SetY(int Y)

{

    m_Y=Y;

}

int GameObject::GetX()

{

    return m_X;

}

int GameObject::GetY()

{

    return m_Y;

}

=================================================================

There is the application of Multiple & Virtual Inheritance in this case.

1.      The class “Dragon” is the Derived Class from the class “GameObject”

2.      The class “Monster” is the Derived Class from the class “GameObject”

3.      The class “BossDragon” is the Derived Class from the class “Dragon” and “Monster”

4.      The private member of class “Dragon” are m_Hp ,m_Attack.

5.      The private member of class “Monster”  is m_Attackrange.

6.      The private member of class “BossDragon” is m_Revivetime.

       GameObject

       -m_x:int

       -m_y:int

         ↑

  ┌──────┴─────┐

 Dragon          Monster

 -m_Hp:int         -m_Attackrange:int

 -m_ Attack:int

  ↑            ↑

  └─────┬─────┘

         │

       BossDragon

       - m_Revivetime:int

=================================================================

The thing you have to do is to complete the following code:

#include<iostream>

using namespace std;

// class GameObject…your code

// class Dragon…your code

// class Monster…your code

// class BossDragon…your code

void PrintData(BossDragon &boss)

{

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

    cout<<"Coordinate = ("<<boss.GetX()<<","<<boss.GetY()<<")"<<endl;

    cout<<"Hp = "<<boss.GetHp()<<endl;

    cout<<"Attack = "<<boss.GetAttack()<<endl;

    cout<<"Attackrange = "<<boss.GetAttackrange()<<endl;

    cout<<"Revivetime = "<<boss.GetRevivetime()<<endl;

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

}

int main()

{

    int X, Y, Hp, Attack, Attackrange, Revivetime;

    int command;

    while(cin>>X>>Y>>Hp>>Attack>>Attackrange>>Revivetime){

        BossDragon boss( X, Y, Hp, Attack, Attackrange, Revivetime );

        PrintData( boss );

        while(1)

        {

            //Input your command(0:SetX, 1:SetY, 2:SetHp, 3:SetAttack, 4:SetAttackrange, 5:SetRevivetime, 6:Quit)

            cin>>command;

            switch(command)

            {

                case 0:

                    cin>>X;

                    boss.SetX(X);

                    break;

                case 1:

                    cin>>Y;

                    boss.SetY(Y);

                    break;

                case 2:

                    cin>>Hp;

                    boss.SetHp(Hp);

                    break;

                case 3:

                    cin>>Attack;

                    boss.SetAttack(Attack);

                    break;

                case 4:

                    cin>>Attackrange;

                    boss.SetAttackrange(Attackrange);

                    break;

                case 5:

                    cin>>Revivetime;

                    boss.SetRevivetime(Revivetime);

                    break;

                default:

                    break;

            }

            if(command==6)

                break;

            PrintData(boss);

        }

    }

    return 0;

}

 

Input :

Each line is a set and contains six integers which indicated BossDragon's initial parameter: X, Y, Attack, Hp, Attackrange, Revivetime.

 

And there are six commands : 0:SetX, 1:SetY, 2:SetHp, 3:SetAttack, 4:SetAttackrange, 5:SetRevivetime, 6:Quit

You are asked to complete the program in order to get this code segment sucessfully compiled and accepted by system.

Output :

The order of Constructor and Destructor are print at the start and the end of each test.

Sample Input :

1 2 3 4 5 6

0 123

1 456

6

100 100 100 100 100 100

2 200

3 300

4 400

5 500

6

Sample Output :

Constructor of GameObject
Constructor of Dragon
Constructor of Monster
Constructor of BossDragon
=================BossDragon=================
Coordinate = (1,2)
Hp = 3
Attack = 4
Attackrange = 5
Revivetime = 6
============================================
=================BossDragon=================
Coordinate = (123,2)
Hp = 3
Attack = 4
Attackrange = 5
Revivetime = 6
============================================
=================BossDragon=================
Coordinate = (123,456)
Hp = 3
Attack = 4
Attackrange = 5
Revivetime = 6
============================================
Destructor of BossDragon
Destructor of Monster
Destructor of Dragon
Destructor of GameObject
Constructor of GameObject
Constructor of Dragon
Constructor of Monster
Constructor of BossDragon
=================BossDragon=================
Coordinate = (100,100)
Hp = 100
Attack = 100
Attackrange = 100
Revivetime = 100
============================================
=================BossDragon=================
Coordinate = (100,100)
Hp = 200
Attack = 100
Attackrange = 100
Revivetime = 100
============================================
=================BossDragon=================
Coordinate = (100,100)
Hp = 200
Attack = 300
Attackrange = 100
Revivetime = 100
============================================
=================BossDragon=================
Coordinate = (100,100)
Hp = 200
Attack = 300
Attackrange = 400
Revivetime = 100
============================================
=================BossDragon=================
Coordinate = (100,100)
Hp = 200
Attack = 300
Attackrange = 400
Revivetime = 500
============================================
Destructor of BossDragon
Destructor of Monster
Destructor of Dragon
Destructor of GameObject

Hint :


  

Author :

(管理員:oop10105)

  Solve it!   Status Forum (0)

C++
C
JAVA
42482. oop100703005 (6 ms , 362KB)
42474. oop100703052 (6 ms , 370KB)
42433. oop100703045 (6 ms , 362KB)
42425. oop100703014 (6 ms , 368KB)
42400. oop100703006 (6 ms , 364KB)
沒有解題記錄 55145. nwgs524513cja (120 ms , 188KB)

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