Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 20 Jan 2013
Offline Last Active Apr 03 2013 07:15 PM

Topics I've Started

Pure virtual function use case question

24 March 2013 - 09:26 AM

Hi, I'm writing a Pong clone and sort of learning C++ at the same time. In the game I have one paddle controlled by the computer and one by the user, but for now I have just one Paddle class.


class Paddle
    Paddle(Position p);
    void setPosition(Position p);
    void moveUp();
    void moveDown();
    void moveTowardGoal(int goal);
    Position getPosition() const;
    int xpos;
    int ypos;
    int vel;


Now I want the moveUp/moveDown methods to move the paddle different distances depending if it's the AI paddle or the user paddle, so I was going to create two child classes, AIPaddle and UserPaddle. However, I am not going to be using polymorphism, as I need to be able to call the moveTowardGoal method, which would only be in the AIPaddle child class. My question is, since the moveUp/moveDown functions are in both child classes (with different implementations), is it "best practice" to declare them in the base class as pure virtual even if polymorphism isn't happening? Or should I just define separate moveUp/moveDown functions in each child class? I doubt it makes any difference performance- or conceptual-wise in this simple case, but as I said I'm trying to learn the C++ way of thinking about things so I'll have a blueprint if something more complex happens in the future.