继承.h和.cpp文件中的构造函数

Inheriting a constructor in a .h and .cpp file

本文关键字:构造函数 文件 cpp 继承      更新时间:2023-10-16

我有两个类: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类来覆盖ObjectSetX(int), SetY(int), SetSpeed(int), SetAngle(float)SetVelocity(float)。如果您这样做,那么您将使这些Object成员函数virtual

其他注意事项:

  1. 不需要在Player.cpp中重新定义Object成员函数。PlayerObject,所以它继承了Object类的成员函数。此外,如果Object.cppPlayer.cpp的实现之间存在任何差异,那么由于违反一个定义规则,您将获得链接器错误。
  2. Object的析构函数应该声明为virtual
  3. 与其在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::texObject::FPSObject和朋友都是私有的。

  4. Object中,应该提供自定义复制构造函数和复制赋值操作符重载(Object::operator=(const Object&))。c++编译器为您提供了这些成员函数的默认实现,但由于您有指针成员和HTEXTURE成员,因此可能需要提供显式实现来处理深度复制。复制构造函数和复制赋值重载必须总是生成深度拷贝。
  5. FPS类型不匹配。Object::FPS成员被声明为float,但是Object::SetSpeed(int)的形参是int