简体   繁体   中英

Inheriting a constructor in a .h and .cpp file

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?

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
{}

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

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM