C++抽象类练习题——games
来源:互联网 发布:干软件二次开发怎么样 编辑:程序博客网 时间:2024/06/06 03:30
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的,保护了成员变量。
- C++抽象类练习题——games
- java抽象类练习题
- java抽象类练习题
- java抽象类练习题
- java抽象类练习题
- 抽象类知识学习-练习题
- C#—抽象方法
- C语言——单链表排序练习题
- C语言——单链表创建练习题
- C语言——归并排序练习题
- C语言基础——经典练习题
- c语言练习题——基础1
- c语言练习题——基础2
- c语言练习题——基础3
- c语言练习题——基础4
- c语言练习题——字符串训练
- C语言练习题——动态数组
- 纯虚函数与抽象类练习题
- EventBus笔记(一)
- Android的Handler,Looper源码剖析
- poj2186 Popular Cows
- MATLAB数据拟合工具在数学建模中的运用
- mysql获取按日期排序获取最新的记录
- C++抽象类练习题——games
- 互联网技术学习中的一些资源总结
- Web开发基础准备——Flask版
- 算法导论_第十六章_动态规划
- 物理扇区和逻辑扇区(相对扇区)的关系
- 2016年6月5日 19:07:13
- 利用Handler在主线程与子线程之间互发消息,handler详解
- 网络安全编程——IP地址字节顺序转换——inet_addr()、inet_ntoa()
- ssh工作原理