public abstract class Stuff { int hp; int atk; int def; int money; int exp; }
时间: 2023-12-01 16:04:55 浏览: 18
这是一个抽象类Stuff,它定义了5个属性:hp、atk、def、money、exp。这些属性可以被它的子类继承和使用,但是这个类本身不能被实例化。
由于这个类是抽象类,可能还包含了一些抽象方法,这些方法只有方法名,没有方法体。它告诉子类必须实现这些方法。抽象方法的定义方式是在方法名前面加上abstract关键字,例如:
```
public abstract void attack();
```
这个抽象方法attack()没有方法体,它只是告诉子类必须实现这个方法。子类必须重写这个方法,并且提供自己的实现。这样可以约束子类的行为,使得子类更加符合预期的功能。
相关问题
#include<iostream> using namespace std; class Iceman { public: int num; int hp; int atk; static int totalNum; Iceman(int n, int h, int a) { num = n; hp = h; atk = a; totalNum++; } }; int Iceman::totalNum = 0; int main() { Iceman* red_iceman[100]; int Iceman_totalNum = 0; int current_hp = 3; red_iceman[Iceman_totalNum] = new Iceman(Iceman::totalNum + 1, current_hp, 0); Iceman_totalNum++; cout << " red iceman " << " born with strength " << red_iceman[Iceman_totalNum]->hp << "," << Iceman_totalNum << " iceman in red headquarter" << endl; }这段代码有什么问题
这段代码存在两个问题:
1. 当访问 `red_iceman[Iceman_totalNum]->hp` 时,`Iceman_totalNum` 的值已经被加 1,导致访问到了一个未初始化的指针,会导致程序崩溃。
2. 在使用 `new` 运算符为 `red_iceman[Iceman_totalNum]` 分配内存时,没有检查是否超出了数组 `red_iceman` 的边界,可能会导致程序崩溃或者出现未定义的行为。
下面是修改后的代码:
```
#include<iostream>
using namespace std;
class Iceman {
public:
int num;
int hp;
int atk;
static int totalNum;
Iceman(int n, int h, int a) {
num = n;
hp = h;
atk = a;
totalNum++;
}
};
int Iceman::totalNum = 0;
int main() {
Iceman* red_iceman[100];
int Iceman_totalNum = 0;
int current_hp = 3;
red_iceman[Iceman_totalNum] = new Iceman(Iceman::totalNum + 1, current_hp, 0);
Iceman_totalNum++;
cout << " red iceman " << " born with strength " << red_iceman[Iceman_totalNum - 1]->hp << "," << Iceman_totalNum << " iceman in red headquarter" << endl;
delete red_iceman[Iceman_totalNum - 1]; // 释放内存
return 0;
}
```
在这个修改后的代码中,我们使用 `Iceman_totalNum - 1` 来访问刚刚新建的 `Iceman` 对象的 `hp` 属性,避免了上面提到的第一个问题。同时,在分配内存前,我们需要先检查 `Iceman_totalNum` 是否超出了数组 `red_iceman` 的边界,避免了第二个问题。最后,为了避免内存泄漏,我们使用 `delete` 运算符释放了分配的内存。
#include<iostream> using namespace std; class Iceman { public: int num; int hp; int atk; int sword = 0; int bomb = 0; int arrow = 0; static int totalNum; Iceman(int n, int h, int a) { num = n; hp = h; atk = a; totalNum++; } }; int Iceman::totalNum = 0; int main() { Iceman* red_iceman[100]; int Iceman_totalNum = 0; int current_hp = 3; red_iceman[Iceman_totalNum] = new Iceman(Iceman::totalNum + 1, current_hp, 0); Iceman_totalNum++; cout << " red iceman " << " born with strength " << red_iceman[Iceman_totalNum]->hp << "," << Iceman_totalNum << " iceman in red headquarter" << endl; }这段代码有什么问题
这段代码有两个问题:
1. 在输出语句中,`red_iceman[Iceman_totalNum]`已经是指向新创建的Iceman对象的指针,因此应该使用`->`而不是`.`来访问`hp`成员变量。正确的输出语句应该是:
```
cout << " red iceman " << " born with strength " << red_iceman[Iceman_totalNum]->hp << "," << Iceman_totalNum << " iceman in red headquarter" << endl;
```
2. 在创建Iceman对象时,构造函数中的`totalNum++`语句应该在其他语句之前执行,否则创建的第一个Iceman对象的编号会是0而不是1。正确的构造函数应该是:
```
Iceman(int n, int h, int a) {
totalNum++;
num = n;
hp = h;
atk = a;
}
```