我是C ++的初学者,我在过去的4个月里一直在学习。我们有一个为学校做的任务;使用我们迄今为止从C ++学到的知识创建基于文本的冒险游戏。今天我们去了课程,继承和指针,所以我认为我可以使用类/继承进行一些练习。
到目前为止我的代码是:
Character.h (标题文件)
#ifndef CHARACTER_H
#define CHARACTER_H
#include <cstdlib>
class Character
{
private:
int Health;
public:
Character();
~Character();
int GetHealth() {return Health;}
void SetHealth(int newHealth) {Health = newHealth;}
};
class Monster:public Character
{
int GetHealth() {return Health;}
void SetHealth(int newHealth) {Health = rand()% 50+100;}
}
#endif
Character.cpp
#include "Character.h"
Character::Character()
{
Health = 100;
}
Character::~Character()
{
}
Battle.cpp (一切都在哪里发生)
#include <iostream>
#include <cstdlib>
#include "Character.h"
using namespace std;
int main()
{
Character* Player = new Character();
Monster* Monster1 = new Monster();
cout << "Player's Health is: " << Player->SetHealth << endl << endl;
cout << "Monster's Health is: " << Monster1->SetHealth << endl << endl;
}
让我解释一下我要做的事情......
我只是想让程序显示玩家和怪物的健康状况。
在字符头文件中,类&#39;字符&#39;是游戏中每个角色的基础。然后我获取并设置课程的健康状况。然后,我试图创建一个名为“怪物”的儿童课程。来源于“角色”#39;类。与标准角色类相比,这将具有不同的健康状况,但我不确定如何做到这一点。我想让健康随机生成50到100之间。然后在&#39; Battle.cpp&#39;文件我正在尝试创建一个名为&#39; Player&#39;其中包括默认的生命值(100)和一个叫做怪物1的新怪物。然后我想显示两个角色的健康状况。我还打算创造出具有不同健康状况的多个怪物。
当我尝试运行该程序时,我似乎得到了大量的错误,并且说出了#City of Character :: Health&#39;。
我不太确定我做错了什么,因为我还是比较新的C ++并且仍然试图了解指针和继承的概念。
答案 0 :(得分:1)
让我回答一下代码:
#ifndef CHARACTER_H
#define CHARACTER_H
#include <cstdlib>
class Character
{
// this property will be used in the derived classes, we need protected instead of private
protected:
int Health;
public:
Character();
~Character();
// This method is to be repeated in the derived class, and publicly accessible
// (not only inside the class, but also outside (like you do in main())
public:
int GetHealth() {return Health;}
// This method needs to be redefined in Monster, that's a candidate for a virtual method
virtual void SetHealth(int newHealth) {Health = newHealth;}
};
class Monster:public Character
{
// You don't need this anymore - the GetHealth from class Character is accessible
// int GetHealth() {return Health;}
// you override a method - write a different version. Use virtual to note that
virtual void SetHealth(int newHealth) {Health = rand()% 50+100;}
}; // lacked a semi-colon here :)
#endif
Battle.cpp的代码
#include <iostream>
#include <cstdlib>
#include "Character.h"
using namespace std;
int main()
{
Character* Player = new Character();
Monster* Monster1 = new Monster();
// The moment you cout, you need to provide a value (a method that returns something).
// You want to "GetHealth()" in order to show it :)
cout << "Player's Health is: " << Player->GetHealth() << endl << endl;
cout << "Monster's Health is: " << Monster1->GetHealth() << endl << endl;
}
我也不喜欢你在课堂上对Health
所做的事情。首先你声明它private
- 所以没有人可以以未经授权的方式改变它。然后你声明一个普通的SetHelth()
,它允许你对值做任何事情。
相反,您可以在构造函数中创建初始值:
#ifndef CHARACTER_H
#define CHARACTER_H
#include <cstdlib>
class Character
{
// this property will be used in the derived classes, we need protected instead of private
protected:
int Health;
public:
Character();
~Character();
// This method is to be repeated in the derived class, and publicly accessible
// (not only inside the class, but also outside (like you do in main())
public:
int GetHealth() {return Health;}
};
class Monster:public Character
{
// You don't need this anymore - the GetHealth from class Character is accessible
// int GetHealth() {return Health;}
public:
Monster();
}; // lacked a semi-colon here :)
#endif
和Character.cpp
#include "Character.h"
Character::Character()
{
Health = 100;
}
Character::~Character()
{
}
Monster::Monster()
{
Health = rand()% 50+100;
}
然后您可以使用DrinkHealthPotion();
或GetDamage();
答案 1 :(得分:0)
我认为你的问题可能是一个错字,你写的是:
cout << "Player's Health is: " << Player->SetHealth << endl << endl;
cout << "Monster's Health is: " << Monster1->SetHealth << endl << endl;
我认为您的意思是GetHealth
而不是SetHealth
,GetHealth
也是一个函数,因此您必须使用括号GetHealth()
来调用它。
所以我相信你想要它说:
cout << "Player's Health is: " << Player->GetHealth() << endl << endl;
cout << "Monster's Health is: " << Monster1->GetHealth() << endl << endl;
希望能帮助您,并祝您在未来使用C ++付出更多努力