[RESOLVED] Class inheritance problem.
Hello, I was searching on the internet for my answer but I found nothing. All I want to make is Object that can move sf::Recrtangle or sf::Sprite. For example:
For example:
Code:
class Movable {
private:
std::string name;
protected:
Movable(std::string);
public:
void moveRight(int lenght); //to move rectangleshape from Rectangle class.
};
class Rectangle : Movable {
private:
sf::RectangleShape rectangleshape;
public:
Rectangle(std::string rectangle_name) : Object(rectangle_name) {}
};
int main() {
Rectangle rectangle("name");
rectangle.moveRight(64);
}
What should i add or change in my code to just move rectangle shape from movable class.
Re: Class inheritance problem.
Hi. When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.
Re: Class inheritance problem.
I expected comment like this :). I can't believe, my nervs are exploding and someone come and tell me something totaly not related to my question. Sure sir, in the future I will use tool for posting code, but at least you could give me an answer.
Re: Class inheritance problem.
Rectangle isn't defined as public inheritence from Movable. As not everything declared is defined in the posted code, I've changed it slightly. The code below compiles
Code:
#include <string>
class Movable {
private:
std::string name;
protected:
Movable(std::string nam) : name(nam) {}
public:
void moveRight(int lenght) {} //to move rectangleshape from Rectangle class.
};
class Rectangle : public Movable {
private:
//sf::RectangleShape rectangleshape;
int rectangleshape;
public:
Rectangle(std::string rectangle_name) : Movable(rectangle_name) {}
};
int main() {
Rectangle rectangle("name");
rectangle.moveRight(64);
}
See http://www.learncpp.com/cpp-tutorial...ss-specifiers/ for further info on class inheritance.
Re: Class inheritance problem.
Thank you for your reply, but I thought how can I from Movable class call rectangleshape variable from Rectangle class.
Re: Class inheritance problem.
class Rectangle is inherited from Movable, so rectangle has access to public/protected members of Movable. However, this doesn't work the other way around. Movable isn't inherited from Rectangle so has no access to Rectangle's members.
Re: Class inheritance problem.
Re: Class inheritance problem.
What is the best solution for this?
Re: [RESOLVED] Class inheritance problem.
Change the design so that the base class doesn't require access to members of any derived class. Basically, the base class knows nothing about any derived classes and shouldn't know anything about them - that's why its a base class! Consider it a one-way street with access from derived to base only.
Why do you want Movable to access Rectangle members? Any data that Movable requires needs to be stored in Movable.
Re: [RESOLVED] Class inheritance problem.
So you're basicly saying I should put all move/rotate functions in Rectangle class?
Is this code correct?
Code:
class Object {
private:
std::string name;
protected:
Object(std::string name) : name(name) {}
public:
std::string* getName();
};
class Movable {
private:
sf::RectangleShape* rectangle_rectangleshape;
sf::Sprite* shape_sprite;
protected:
Movable(sf::RectangleShape* rectangle_rectangleshape) : rectangle_rectangleshape(rectangle_rectangleshape), shape_sprite(NULL) {}
Movable(sf::Sprite* shape_sprite) : shape_sprite(shape_sprite), rectangle_rectangleshape(NULL) {}
public:
void setPosition(float, float);
void move(float, float);
void moveRight(float);
void moveDown(float);
void moveLeft(float);
void moveUp(float);
void Movable::slideTo(float, float);
void setAngle(float);
void rotate(float);
void rotateLeft(float);
void rotateRight(float);
Position getPosition();
};
class Rectangle : public Object, public Movable {
private:
sf::RectangleShape rectangleshape;
public:
Rectangle(std::string object_name, int rectangle_width, int rectangle_height) : Object(object_name), Movable(&rectangleshape) {}
};
class Shape : public Object, public Movable {
private:
sf::Sprite sprite;
public:
Shape(std::string object_name, int rectangle_width, int rectangle_height) : Object(object_name), Movable(&sprite) {}
};
Re: [RESOLVED] Class inheritance problem.
Quote:
Object that can move sf::Recrtangle or sf::Sprite
Going back to basics, consider if Object is a base class and Rectangle and Sprite are derived from Object and if Object is a polymorphic class (possibly a pure virtual class?), then the code to do the actual moving would be in the Rectangle and Sprite class but Object would have the required virtual declarations so that at run-time depending upon whether a Rectangle or Sprite instance was used the correct functions for either would be invoked. Thus this would be possible
Code:
myfunc(Object& myobj)
{
myobj.moveRight(100);
}
Rectangle myrect;
Sprite mysprite;
myfunc(myrect);
myfunc(mysprite);
etc.
For further details of virtual/polymorphic classes see
http://www.learncpp.com/cpp-tutorial...ual-functions/