簡體   English   中英

在.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。

為了舉例說明,我在Player類中添加了一個變量: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覆蓋SetX(int) SetY(int) SetSpeed(int) SetAngle(float) ,並SetVelocity(float)Object 如果這樣做,則可以將這些Object成員函數virtual

其他一些注意事項:

  1. 您無需在Player.cpp重新定義Object成員函數。 Player是一個Object ,因此它繼承了Object類的成員函數。 另外,如果Object.cppPlayer.cpp的實現之間存在任何差異,則由於違反“一個定義規則”,您將收到鏈接器錯誤。
  2. 應該將Object析構函數聲明為virtual
  3. 您可以使用初始化列表來Object直接在Player構造函數中設置Object的成員:

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

    使用初始化列表效率更高。 在這種情況下甚至有必要,因為Object::xObject::yObject::texObject::FPS都是Object和其朋友專用的。

  4. Object ,您應該提供一個自定義的復制構造函數和copy-assign運算符重載( Object::operator=(const Object&) )。 C ++編譯器為您提供了這些成員函數的默認實現,但是由於您具有指針成員和HTEXTURE成員,因此可能需要提供顯式實現來處理深層副本。 復制構造函數和復制分配重載必須始終進行深復制。
  5. FPS的類型不匹配。 Object::FPS成員被聲明為float ,但是Object::SetSpeed(int)的參數是int

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM