-1

So my goal is simple, I want to have an x and y in the base class, and then have my other class own those values as well

class Skeleton
{
private:
    float x, y;

public:
    Skeleton(float x, float y);
};

class Object : Skeleton
{
    float getX() const
    {
        return x;
    }
};

By the way, I'm sort of new to C++ I come from Java and want to learn C++!

Matie
  • 67
  • 6
  • 1
    Yes, that's called **inheritance**. So, what is your problem exactly? – Ardent Coder May 23 '20 at 21:26
  • @AsteroidsWithWings I think OP might be looking for best practices as well. Let him clarify. – Ardent Coder May 23 '20 at 21:27
  • 2
    Use `protected` instead of `private` in the `Skeleton` class. `protected: float x, y;` – WBuck May 23 '20 at 21:28
  • 2
    One of the better ways to learn C++ is from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Java and C++ are very different languages (both very good languages), but what you've learned in Java may not be applicable in C++. – Eljay May 23 '20 at 21:37
  • 1
    Perhaps the answer you've gotten is an answer to whatever it is you're asking, but it's very hard to tell. – Ted Lyngmo May 23 '20 at 21:46

1 Answers1

0

You can use the protected access specifier instead of private to allow classes which derive from your base class to access certain member functions and variables.

Also you'll need your derived classes constructor to call the base classes constructor.

class Skeleton
{
protected:
    float x, y;

public:
    Skeleton(float x, float y);
};

class Object : Skeleton
{
public:
    Object( float x, float y ) : Skeleton{ x, y } { }

    float getX() const
    { 
        return x;
    }
};
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
WBuck
  • 5,162
  • 2
  • 25
  • 36