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

发布于 2024-12-11 00:27:21 字数 2826 浏览 0 评论 0原文

所以我有两个类: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

や莫失莫忘 2024-12-18 00:27:21

您正在两次定义 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
{}

You are defining the Object functionality twice. You don't need to define that, it will be inherited from Object and automatically be available on Player.

Your Player constructor needs to invoke the base Object constructor. This is done with constructor initialization lists:

Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable)
   : Object(x, y, tex, FPS) // forward arguments to base constructor
{}
梦里°也失望 2024-12-18 00:27:21

您可以通过这种方式调用基类构造函数,并指定每个参数:

Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable): Object(x, y, tex, FPS) {

You can call the base class constructor this way, specifying every parameter :

Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable): Object(x, y, tex, FPS) {
够运 2024-12-18 00:27:21

看起来您实际上并不需要类 Player 来覆盖 SetX(int)SetY(int)SetSpeed(int )ObjectSetAngle(float)SetVelocity(float)。如果这样做,那么您将把这些Object成员函数设为virtual

其他一些注意事项:

  1. 您不需要重新定义 Player.cpp 中的 Object 成员函数。 Player是一个Object,因此它继承了类Object的成员函数。另外,如果 Object.cppPlayer.cpp 中的实现之间存在任何差异,那么您将因违反单一定义规则而收到链接器错误。
  2. Object 析构函数应声明为virtual
  3. 您可以使用初始化列表,而不是直接在 Player 构造函数中设置 Object 的成员:

    Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable)
        :对象(x,y,tex,FPS),playerVariable(playerVariable)
    {
    }
    

    使用初始化列表效率更高。在这种情况下甚至是必要的,因为 Object::xObject::yObject::texObject: :FPS 都是 Object 和朋友私有的。

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

It looks like you don't actually need class Player to override SetX(int), SetY(int), SetSpeed(int), SetAngle(float), and SetVelocity(float) of Object. If you did, then you would make these Object member functions virtual.

A few other notes:

  1. You don't need to redefine the Object member functions in Player.cpp. Player is an Object, so it inherits the member functions of class Object. Also, if there are any differences between the implementation in Object.cpp and Player.cpp then you will get linker errors due to a violation of the One Definition Rule.
  2. The Object destructor should be declared virtual.
  3. Rather than setting the members of Object directly in the Player constructor, you could use an initialization list:

    Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable)
        : Object(x, y, tex, FPS), playerVariable(playerVariable)
    {
    }
    

    Using initialization lists is much more efficient. It is even necessary in this case because Object::x, Object::y, Object::tex, and Object::FPS are all private to Object and friends.

  4. In 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 and HTEXTURE 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.
  5. There is a mismatch on the type of FPS. The Object::FPS member is declared as a float, but the parameter to Object::SetSpeed(int) is an int.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文