CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2016
    Posts
    22

    [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.
    Last edited by 2kaud; October 9th, 2016 at 10:37 AM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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 '#'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Oct 2016
    Posts
    22

    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.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Oct 2016
    Posts
    22

    Re: Class inheritance problem.

    Thank you for your reply, but I thought how can I from Movable class call rectangleshape variable from Rectangle class.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Oct 2016
    Posts
    22

    Re: Class inheritance problem.

    Thank you!

  8. #8
    Join Date
    Oct 2016
    Posts
    22

    Re: Class inheritance problem.

    What is the best solution for this?

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Oct 2016
    Posts
    22

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

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: [RESOLVED] Class inheritance problem.

    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/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured