C++抽象类练习题——games

Description:

In this problem, you should complete some classes.

You can see more details in sample input / output and main.cpp

Each gameobject will have position,speed, and 10HP.

When you select a gameobject, we will hear it’s voice.You should print this voice.

When you select a worker, you will hear “Scv good to go, sir!”
When you select a zealot, you will hear “My life for Aiur!”
When you select a tower, you will hear nothing, and you can replace it by a “~”

We describe the speed as a vector (dx, dy)
For the three gameobjects, they are (1, 1) (2, 2) and (0, 0). (Tower cannot move.)
When a object moves, you should print where he moves to.

Zealot and tower can attack other objects and their damages are 1.

Worker can repair an object.

When worker build a building, you should print a triangle by ‘*’ with different height.

For example, if the worker build a building with 3 you should print
+
+++
+++++

sample input

1 1
2 2
3 3
4

sample output

test for select:
Scv good to go, sir!
My life for Aiur
~

test for move:
Worker moves to (2, 2)
Zealot moves to (4, 4)
Tower cannot move

test for attack:
Zealot’s hp is 9
Tower’s hp is 9

test for repair:
Zealot’s hp is 10

test for build:
+
+++
+++++
+++++++

题目说明:
题目描述中三角形可能看上去可能会有些问题。高度为3的三角形,第一层为2个空格和一个+,第二层为1个空格和三个+,第三层为5个+

关于move函数,对于原本在(x,y)的对象,移动到(x+dx, y+dy)

为避免奇怪错误,repair 和 attack传参数请传gameobject*

解答:

#ifndef HEADER_H
#define HEADER_H
#include<iostream>
using namespace std;
class gameobject {
    protected:
        double x, y;
        double dx, dy;
        int hp;
    public:
        gameobject(double x = 0, double y = 0, double dx = 0, double dy = 0)
        : x(x), y(y), dx(dx), dy(dy), hp(10) {}
        virtual void select() = 0;
        virtual void move() = 0;
        void under_attack(int x) {
            hp -= x;
        }
        void get_repaired() {
            hp = 10;
        }
        int get_hp() {
            return hp;
        }
};
class worker : public gameobject {
    public:
        worker(double x = 0, double y = 0) : gameobject(x, y, 1, 1) {}
        virtual void select() {
            cout << "Scv good to go, sir!\n";
        }
        virtual void move() {
            x += dx;
            y += dy;
            cout << "Worker moves to (" << x << ", "<< y << ")\n";
        }
        void repair(gameobject* g) {
            g->get_repaired();
        }
        void build(int x) {
            for (int i = 1; i <= x; i++) {
                for (int j = 1; j <= x-i; j++) cout << " ";
                for (int j = 1; j <= 2*i-1; j++) cout << "*";
                cout << endl;
            }
        }
};
class zealot : public gameobject {
    private:
        int _attack;
    public:
        zealot(double x = 0, double y = 0)
        : gameobject(x, y, 2, 2), _attack(1) {}
        virtual void select() {
            cout << "My life for Aiur!\n";
        }
        virtual void move() {
            x += dx;
            y += dy;
            cout << "Zealot moves to (" << x << ", "<< y << ")\n";
        }
        void attack(gameobject* g) {
            g->under_attack(_attack);
        }
};
class tower : public gameobject {
    private:
        int _attack;
    public:
        tower(double x = 0, double y = 0)
        : gameobject(x, y, 0, 0), _attack(1) {}
        virtual void select() {
            cout << "~\n";
        }
        virtual void move() {
            cout << "Tower cannot move.\n";
        }
        void attack(gameobject* g) {
            g->under_attack(_attack);
        }
};
#endif

测试函数:

#include"header.h"
int main() {
    gameobject* g;  // gameobject是一个抽象类
    worker* w;  // worker zealot tower是派生类
    zealot* z;
    tower* t;
    double x, y;  // (x, y)表示坐标
    cin >> x >> y;
    w = new worker(x, y);
    cin >> x >> y;
    z = new zealot(x, y);
    cin >> x >> y;
    t = new tower(x, y);
    // test for select
    cout << "test for select:\n";
    // 当一个对象被选中时,输出他们的语音。
    g = w;
    g->select();
    g = z;
    g->select();
    g = t;
    g->select();
    // test for move
    cout << "\ntest for move:\n";
    // 当一个对象移动时,输出他们移动到的位置。
    g = w;
    g->move();
    g = z;
    g->move();
    g = t;
    g->move();
    // test for others
    cout << "\ntest for attack:\n";
    z->attack(t);
    t->attack(z);
    cout << "Zealot's hp is " << z->get_hp() << endl;
    cout << "Tower's hp is " << t->get_hp() << endl;
    cout << "\ntest for repair:\n";
    w->repair(z);  // worker将zealot的HP修复至10
    cout << "Zealot's hp is " << z->get_hp() << endl;
    int h;
    cin >> h;
    cout << "\ntest for build:\n";
    w->build(h);
    delete w;
    delete z;
    delete t;
    return 0;
}

心得体会:
games是一道抽象类的基础题
我自己写的父类定义如下:

class gameobject {
 public:
    virtual void select() = 0;
    virtual void move() = 0;
    virtual void attack(gameobject* t) = 0;
    virtual int get_hp() = 0;
    int hp;
    gameobject() {hp = 10;}
};

要注意的一个基本的知识点是,抽象类跟继承是紧密联系在一起的父类是抽象类,其中定义一些virtual的函数,在父类本身不能实例化需要在继承父类的子类中实例化,而且,父类中的virtul函数,在子类中,【都要实例化】,不管你需不需要。
父类中也可以定义一些非virtual的函数,这些函数可以在父类中实例化。

BTW,这是TA定义的抽象父类:

class gameobject {
    protected:
        double x, y;
        double dx, dy;
        int hp;
    public:
        gameobject(double x = 0, double y = 0,
double dx = 0, double dy = 0)
        : x(x), y(y), dx(dx), dy(dy), hp(10) {}
        virtual void select() = 0;
        virtual void move() = 0;
        void under_attack(int x) {
            hp -= x;
        }
        void get_repaired() {
            hp = 10;
        }
        int get_hp() {
            return hp;
        }
};

把hp等定义为protected,这样对子类来说是public的,对main来说是private的,保护了成员变量。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 请创建一个抽象类DataStructure,该类包括下面的成员变量和成员函数: 1) 一个成员变量len,表示里面的元素个数最大值 2) 构造函数DataStructure(int l),将len初始化为0 3) 虚析构函数~DataStructure() 4) 纯虚函数Output(),输出DataStructure中的数据 5) 纯虚函数Size(),返回DataStructure中的元素个数 2. 请创建DataStructure的一个派生类MyString,该类包括下面的成员变量和成员函数: 1) 一个成员变量char* data,表示里面的数据 2) 构造函数MyString(int max_size),将MyString初始化为空串,最大元素个数为max_size 3) 析构函数~MyString(),释放相应的数据 4) Input()函数,往MyString输入数据 5) 重载operator+=()函数,实现两个字符串的连接 6) 重定义Output()和Size()函数 3. 请创建DataStructure的一个派生类MyStack,该类包括下面的成员变量和成员函数: 1) 一个成员变量int* data,用于里面的数据 2) 一个成员变量int top,表示最上面的元素下标 3) 构造函数MyStack(int max_size),将MyStack初始化为空栈,最大元素个数为max_size 4) 析构函数~MyStack(),释放相应的数据 5) Push_back(int e)函数,往栈里面压入一个数据e 6) 重定义Output()和Size()函数 4. 请编写main函数,测试上面程序的正确性 1) 创建两个MyString的对象str1和str2,分别调用Input函数输入str1和str2,然后分别调用operator+=函数将str2连接到str1的末尾 2) 创建一个MyStack的对象stack,调用Push_back函数输入往stack中输入m(m < max_size)个数据 3) 创建一个长度为3的DataStructure*类型的数组,将其3个元素分别指向str1, str2, stack,然后编写for循环调用Size()和Output()函数输出每个元素的大小和内容。 5. 输入输出样例: 1) 输入样例 A promising techni que for checking reachability 4 12 23 34 45 2) 输出样例 47 A promising technique for checking reachability 29 que for checking reachability 4 12 23 34 45

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值