C++ 在.h和.cpp文件中继承构造函数
我有两个类:Object和Player。 我希望玩家从对象继承,因为对象有我需要的基本函数。 玩家将其添加到从对象可以执行的功能扩展而来的函数上。 我有四个文件: Object.cpp、Object.h、Player.cpp和Player.h 为了说明我的情况,我在我的Player类中添加了一个变量: 游戏变量。我的对象构造函数参数不包含此项,但我的播放器构造函数包含此项,所以您可以看到播放器扩展了对象 无论如何,这是我的代码: 目标h:C++ 在.h和.cpp文件中继承构造函数,c++,inheritance,constructor,C++,Inheritance,Constructor,我有两个类:Object和Player。 我希望玩家从对象继承,因为对象有我需要的基本函数。 玩家将其添加到从对象可以执行的功能扩展而来的函数上。 我有四个文件: Object.cpp、Object.h、Player.cpp和Player.h 为了说明我的情况,我在我的Player类中添加了一个变量: 游戏变量。我的对象构造函数参数不包含此项,但我的播放器构造函数包含此项,所以您可以看到播放器扩展了对象 无论如何,这是我的代码: 目标h: #include <hge.h> #incl
#include <hge.h>
#include <hgesprite.h>
#include <hgeanim.h>
#include <math.h>
class Object{
int x, y;
HTEXTURE tex;
hgeAnimation *anim;
float angle, FPS, velocity;
public:
Object(int x, int y, HTEXTURE tex, float FPS);
//Setters
void SetX(int x);
void SetY(int y);
void SetSpeed(int FPS); //Image Speed
void SetAngle(float angle); //Image Angle
void SetVelocity(float velocity); //Object Speed/Velocity
//Getters
};
#包括
#包括
#包括
#包括
类对象{
int x,y;
HTEXTURE-tex;
hgeAnimation*动画;
浮球角度,FPS,速度;
公众:
对象(intx、inty、HTEXTURE-tex、float-FPS);
//二传手
无效集x(int x);
void SetY(inty);
void SetSpeed(int FPS);//图像速度
无效设置角度(浮动角度);//图像角度
void SetVelocity(float velocity);//对象速度/速度
//吸气剂
};
Object.cpp:
/*
** Object
**
*/
#include <hge.h>
#include <hgesprite.h>
#include <hgeanim.h>
#include <math.h>
#include "Object.h"
Object::Object(int x, int y, HTEXTURE tex, float FPS){
this->x = x;
this->y = y;
this->tex = tex;
this->FPS = FPS;
}
//Setters
void Object::SetX(int x){
this->x = x;
}
void Object::SetY(int y){
this->x = x;
}
void Object::SetSpeed(int FPS){
this->FPS = FPS;
anim->SetSpeed(FPS);
}
void Object::SetAngle(float angle){
this->angle = angle;
}
void Object::SetVelocity(float velocity){
this->velocity = velocity;
}
//Getters
/*
**反对
**
*/
#包括
#包括
#包括
#包括
#包括“Object.h”
对象::对象(intx、inty、HTEXTURE-tex、float-FPS){
这个->x=x;
这->y=y;
这->tex=tex;
此->FPS=FPS;
}
//二传手
void对象::SetX(int x){
这个->x=x;
}
void对象::SetY(int y){
这个->x=x;
}
void对象::设置速度(整数FPS){
此->FPS=FPS;
动画->设定速度(FPS);
}
void对象::设置角度(浮动角度){
这个->角度=角度;
}
void对象::设置速度(浮动速度){
这个->速度=速度;
}
//吸气剂
Player.h:
#include <hge.h>
#include <hgesprite.h>
#include <hgeanim.h>
#include <math.h>
#include "Object.h"
class Player : public Object{
int playerVariable;
public:
Player(int x, int y, HTEXTURE tex, float FPS, int playerVariable);
//Setters
void SetX(int x);
void SetY(int y);
void SetSpeed(int FPS); //Image Speed
void SetAngle(float angle); //Image Angle
void SetVelocity(float velocity); //Object Speed/Velocity
//Getters
};
#包括
#包括
#包括
#包括
#包括“Object.h”
类播放器:公共对象{
int可播放变量;
公众:
播放器(整数x,整数y,HTEXTURE tex,float FPS,整数playerVariable);
//二传手
无效集x(int x);
void SetY(inty);
void SetSpeed(int FPS);//图像速度
无效设置角度(浮动角度);//图像角度
void SetVelocity(float velocity);//对象速度/速度
//吸气剂
};
Player.cpp:
/*
** Object
**
*/
#include <hge.h>
#include <hgesprite.h>
#include <hgeanim.h>
#include <math.h>
#include "Object.h"
#include "Player.h"
Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable){
this->x = x;
this->y = y;
this->tex = tex;
this->FPS = FPS;
}
//Setters
void Object::SetX(int x){
this->x = x;
}
void Object::SetY(int y){
this->x = x;
}
void Object::SetSpeed(int FPS){
this->FPS = FPS;
anim->SetSpeed(FPS);
}
void Object::SetAngle(float angle){
this->angle = angle;
}
void Object::SetVelocity(float velocity){
this->velocity = velocity;
}
//Getters
/*
**反对
**
*/
#包括
#包括
#包括
#包括
#包括“Object.h”
#包括“Player.h”
Player::Player(整数x,整数y,HTEXTURE-tex,float-FPS,playerVariable){
这个->x=x;
这->y=y;
这->tex=tex;
此->FPS=FPS;
}
//二传手
void对象::SetX(int x){
这个->x=x;
}
void对象::SetY(int y){
这个->x=x;
}
void对象::设置速度(整数FPS){
此->FPS=FPS;
动画->设定速度(FPS);
}
void对象::设置角度(浮动角度){
这个->角度=角度;
}
void对象::设置速度(浮动速度){
这个->速度=速度;
}
//吸气剂
我的问题是,我不确定我的设置是否正确。
我已经看过了关于继承的教程,但我不知道如何使用这两种方法进行设置
类的.h文件和.cpp文件。有人能帮忙吗?您可以这样调用基类构造函数,指定每个参数:
Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable): Object(x, y, tex, FPS) {
可以通过这种方式调用基类构造函数,指定每个参数:
Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable): Object(x, y, tex, FPS) {
您将两次定义
对象
功能。您不需要定义它,它将从对象
继承,并在播放器
上自动可用
您的播放器
构造函数需要调用基本对象
构造函数。这是通过构造函数初始化列表完成的:
Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable)
: Object(x, y, tex, FPS) // forward arguments to base constructor
{}
您将两次定义
对象
功能。您不需要定义它,它将从对象
继承,并在播放器
上自动可用
您的播放器
构造函数需要调用基本对象
构造函数。这是通过构造函数初始化列表完成的:
Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable)
: Object(x, y, tex, FPS) // forward arguments to base constructor
{}
看起来您实际上不需要class
Player
来覆盖对象的SetX(int)
、SetY(int)
、SetSpeed(int)
、SetAngle(float)
和SetVelocity(float)
。如果您这样做了,那么您将使这些对象
成员函数虚拟
其他一些注意事项:
您不需要在Player.cpp
中重新定义对象
成员函数<代码>播放器
是一个对象
,因此它继承了类对象
的成员函数。此外,如果Object.cpp
中的实现与Player.cpp
中的实现之间存在任何差异,那么您将由于违反一个定义规则而获得链接器错误
对象
析构函数应该声明为virtual
Player
构造函数中设置Object
的成员不同,您可以使用初始化列表:
Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable)
: Object(x, y, tex, FPS), playerVariable(playerVariable)
{
}
使用初始化列表更有效。在这种情况下,它甚至是必要的,因为Object::x
,Object::y
,Object::tex
,以及Object::FPS
都是Object
和朋友的私人物品Object
中,您应该提供一个自定义的复制构造函数和复制赋值操作符重载(Object::operator=(const Object&)
)。C++编译器为您提供了这些成员函数的默认实现,但是因为您有指针成员和FPS
的类型不匹配。Object::FPS
成员声明为float
,但是Object::SetSpeed(int)
的参数是int
看起来您实际上不需要class
Player
来覆盖对象的SetX(int)
、SetY(int)
、SetSpeed(int)
、SetAngle(float)
和SetVelocity(float)
。如果你做了,我不会