0

I'm trying to inherit some private members of a class. I'm not going to put any of the other cpp files or .h files because this should only concern BoxClass.h, Bullet.h, and Bullet.cpp. In bullet.cpp in "Bullet::Bullet_Draw_Collision()" the programs not recognizing "ySize" from "BoxClass.h". I inherited "BoxClass" class to "Bullet" class. Why isn't the program recognizing this variable. Thanks!

edit: To simplify my question, why can't I inherit the ySize variable.

BoxClass.h:

#ifndef BOXCLASS_H
#define BOXCLASS_H

class BoxClass {
    //prv variables
    unsigned short int width, retrieveX;
    int height, i, xSize, ySize, rightWall;
    float space_Value, height_Count;
    bool error;

    int width_Var, height_Var, position_Var;
    int speed_Var = 1;
    unsigned short int horizontalCount = 0, verticleCount = 0;


public:

    int Retrieve_X();

    void Print_Rectangle_Moving(int x, int y, int horizontalSpaces, int verticleSpaces);

    void Print_Solid_Rectangle();

    void Rectangle_Movement(int speed);

    //function shows area of individual spaces
    float Rectangle_Area();

    // constructor
    BoxClass(int x, int y);
};

#endif

Bullet.h:

#ifndef BULLET_H
#define BULLET_H

class Bullet: private BoxClass{

public:
    void Bullet_Draw_Collision();

    //constructor
    Bullet();
};

#endif

Bullet.cpp:

#include "BoxClass.h"

void Bullet::Bullet_Draw_Collision() {
ySize;
}

Bullet::Bullet() {

};
Zack Oliver
  • 107
  • 6

2 Answers2

2

You must set the members of BoxClass protected or public in order to access them in Bullet

BoxClass.h

class BoxClass 
{
protected: // or public, consider var access when designing the class
    int ySize;
};

Bullet.h

class Bullet: private BoxClass // or protected or public
{
public:
    void Bullet_Draw_Collision();
};

Bullet.cpp

void Bullet::Bullet_Draw_Collision() 
{
   // do whatever you need with inherited member vars
   ySize;
}

Bullet::Bullet() 
{
};
1

You can use either of these options.

Option 1:

 class BoxClass {

  protected:
     int ySize;
};

Option 2:

class BoxClass {

  private:
     int ySize;

  protected:
     //properties
     void set_ysize(int y);
     int get_ysize() const;
};

void Bullet::Bullet_Draw_Collision()
{
   set_ysize(10);
}
seccpur
  • 4,996
  • 2
  • 13
  • 21