继承.h和.cpp文件中的构造函数
Inheriting a constructor in a .h and .cpp file
我有两个类:Object和Player。我想要Player从Object继承因为Object有我需要的基本功能。Player有它的附加功能,扩展了Object的功能。我有四个文件:Object.cpp, Object.h, Player.cpp和Player.h.
让一个例子,我的情况下,我已经添加了一个变量到我的播放器类:playerVariable。我的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文件。有人能帮忙吗?
您定义了两次Object
功能。您不需要定义它,它将从Object
继承,并自动在Player
上可用。
您的Player
构造函数需要调用基本的Object
构造函数。这是通过构造函数初始化列表完成的:
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) {
看起来您实际上不需要Player
类来覆盖Object
的SetX(int)
, SetY(int)
, SetSpeed(int)
, SetAngle(float)
和SetVelocity(float)
。如果您这样做,那么您将使这些Object
成员函数virtual
。
其他注意事项:
- 不需要在
Player.cpp
中重新定义Object
成员函数。Player
是Object
,所以它继承了Object
类的成员函数。此外,如果Object.cpp
和Player.cpp
的实现之间存在任何差异,那么由于违反一个定义规则,您将获得链接器错误。 与其在
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++编译器为您提供了这些成员函数的默认实现,但由于您有指针成员和HTEXTURE
成员,因此可能需要提供显式实现来处理深度复制。复制构造函数和复制赋值重载必须总是生成深度拷贝。 -
FPS
类型不匹配。Object::FPS
成员被声明为float
,但是Object::SetSpeed(int)
的形参是int
。
Object
的析构函数应该声明为virtual
。- 类中的数组变量C++导致"was not declared in this scope"实现文件的一个函数中错误,但在构造函数中不会导致错误
- 如何从另一个文件调用类构造函数?
- 如何使输入文本文件成为构造函数参数?c++
- C++ 在头文件或构造函数中初始化 const 类成员变量?
- C++头/实现文件中的默认和重载构造函数?
- 文件中定义的 Constexpr 构造函数.cpp会导致链接错误
- 如果类划分为单独的文件,则不调用默认构造函数
- 将单独的头文件和类定义文件链接到主函数文件 - G++ 返回重载"undefined reference to"构造函数
- 从文件实例化:构造函数采用std:ifstream&作为参数
- 如何使用类构造函数从文件中获取值?C++
- 打开不存在的文件时如何使流构造函数失败?
- 如何解析文本文件并使用构造函数中的文件输入来创建对象的容器
- 我想传入文件的构造函数模式
- 找不到构造函数 - 如何将 CPP 文件添加到 Qt 构建过程
- 如何将模板构造函数专用化移动到 cpp 文件
- 具有POSIX文件描述符对象的C 复制构造函数
- 从类构造函数中的txt文件读取
- 如何将显式声明的构造函数分离到 .h 和 .cpp 文件中?
- 将文件传递给构造函数进行读取
- 确认从类构造函数中的文本文件初始化变量