继承 .h 和 .cpp 文件中的构造函数
所以我有两个类:Object 和 Player。 我希望Player继承自Object,因为Object具有我需要的基本功能。 Player 具有扩展 Object 功能的附加功能。 我有四个文件: Object.cpp、Object.h、Player.cpp 和 Player.h。
为了根据我的情况举例,我在 Player 类中添加了一个变量: 玩家变量。我的 Object 构造函数参数不包含此参数,但我的 Player 构造函数包含此参数,因此您可以看到 Player 扩展了 Object。
无论如何,这是我的代码:
Object.h:
#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
};
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
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
};
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
我的问题是,我不确定我的设置是否正确。 我看过有关继承的教程,但我不知道如何使用两者进行设置 类的 .h 文件和 .cpp 文件。有人可以帮忙吗?
So I've got two classes: Object and Player.
I want Player to inherit from Object because Object has basic functions that I need.
Player has it's added-on functions that expand from what Object can do.
I have four files:
Object.cpp, Object.h, Player.cpp, and Player.h.
To make an example out of my situation, I have added a variable to my Player class:
playerVariable. My Object constructor parameters does not contain this, however my Player constructor does, so you can see that Player expands Object.
Anyways, Here is my code:
Object.h:
#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
};
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
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
};
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
My problem is, I'm not exactly sure if I'm setting this up right.
I've looked at tutorials on inheritance but I have no idea how to set it up with both
a .h file and a .cpp file for the classes. Can someone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在两次定义
Object
功能。您不需要定义它,它将继承自Object
并自动在Player
上可用。您的
Player
构造函数需要调用基本Object
构造函数。这是通过构造函数初始化列表完成的:You are defining the
Object
functionality twice. You don't need to define that, it will be inherited fromObject
and automatically be available onPlayer
.Your
Player
constructor needs to invoke the baseObject
constructor. This is done with constructor initialization lists:您可以通过这种方式调用基类构造函数,并指定每个参数:
You can call the base class constructor this way, specifying every parameter :
看起来您实际上并不需要类
Player
来覆盖SetX(int)
、SetY(int)
、SetSpeed(int )
、Object
的SetAngle(float)
和SetVelocity(float)
。如果这样做,那么您将把这些Object
成员函数设为virtual
。其他一些注意事项:
Player.cpp
中的Object
成员函数。Player
是一个Object
,因此它继承了类Object
的成员函数。另外,如果Object.cpp
和Player.cpp
中的实现之间存在任何差异,那么您将因违反单一定义规则而收到链接器错误。Object
析构函数应声明为virtual
。您可以使用初始化列表,而不是直接在
Player
构造函数中设置Object
的成员:使用初始化列表效率更高。在这种情况下甚至是必要的,因为
Object::x
、Object::y
、Object::tex
和Object: :FPS
都是Object
和朋友私有的。Object
中,您应该提供自定义复制构造函数和复制赋值运算符重载 (Object::operator=(const Object&)
)。 C++ 编译器为您提供这些成员函数的默认实现,但由于您有指针成员和 HTEXTURE 成员,因此您可能需要提供显式实现来处理深层复制。复制构造函数和复制分配重载必须始终进行深复制。FPS
类型不匹配。Object::FPS
成员声明为float
,但Object::SetSpeed(int)
的参数是int
。It looks like you don't actually need class
Player
to overrideSetX(int)
,SetY(int)
,SetSpeed(int)
,SetAngle(float)
, andSetVelocity(float)
ofObject
. If you did, then you would make theseObject
member functionsvirtual
.A few other notes:
Object
member functions inPlayer.cpp
.Player
is anObject
, so it inherits the member functions of classObject
. Also, if there are any differences between the implementation inObject.cpp
andPlayer.cpp
then you will get linker errors due to a violation of the One Definition Rule.Object
destructor should be declaredvirtual
.Rather than setting the members of
Object
directly in thePlayer
constructor, you could use an initialization list:Using initialization lists is much more efficient. It is even necessary in this case because
Object::x
,Object::y
,Object::tex
, andObject::FPS
are all private toObject
and friends.Object
, you should provide a custom copy constructor and copy-assign operator overload (Object::operator=(const Object&)
). The C++ compiler provides default implementations of these member functions for you, but because you have pointer members andHTEXTURE
members, you likely need to provide explicit implementations to handle the deep copy. The copy constructor and copy-assign overload must always make deep copies.FPS
. TheObject::FPS
member is declared as afloat
, but the parameter toObject::SetSpeed(int)
is anint
.