setting up inheritance

This is a discussion on setting up inheritance within the C++ Programming forums, part of the General Programming Boards category; Hi again, I'm trying to set up some inheritance between some classes that I've already written. There are two classes, ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    27

    setting up inheritance

    Hi again,

    I'm trying to set up some inheritance between some classes that I've already written.

    There are two classes, Snake (parent class) and AISnake (derived class). here are their current headers

    Code:
    class AISnake {
    
        //Attributes
    
        protected:
    
            char direction_;
            char nd_;
            std::list<Position> AIsegments;
            UINT    nodesize_;
            Image nodeimage2;
            //Snake playerSnake_;
    
        //Operations
    
        public:
            AISnake(int x, int y);
    
            //updates the position of the snake
            void AIupdate();
    
            //renders the snake to the canvas
            void AIdraw(Canvas& canvas);
    
            //sets the direction the snake will move
            void AIset_direction(char direction);
    
            //gets the direction the snake will move
            int AIget_direction();
    };
    Code:
    class Snake {
    
        //Attributes
    
        protected:
    
            char direction_;
            char nd_;
            std::list<Position> segments;
            UINT    nodesize_;
            Image nodeimage;
            //Snake playerSnake_;
    
        //Operations
    
        public:
            Snake(int x, int y);
    
            //updates the position of the snake
            void update();
    
            //renders the snake to the canvas
            void draw(Canvas& canvas);
    
            //sets the direction the snake will move
            void set_direction(char direction);
    
            //gets the direction the snake will move
            int get_direction();
    };
    at the moment, they are completely separate classes, but I want AISnake to inherit from Snake.

    The AI snake will use all the same attributes and operations as Snake apart from the constructor and the void update( ) operation.

    I know I need to put ' : public Snake' next to the class declaration in tell the compiler it's a derived class but what's the syntax to overload the constructor and update( ) operations? and what do I do to tell it to use the other operations as they are written in snake.cpp?

    thanks very much in advance for the help. And sorry for the inevitable amount of questions that will come from me in the next few days, I'm almost finished this project, so it won't last long, I promise

    ES

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    27
    Try to read your book, on inheritance, overloading and overriding.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't overload a constructor in a derived class. Your derived class needs its own constructor. You can just write a new update function in the new class.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    Yes, you'll use ": public Snake" in your AISnake declaration.

    Then, the next step is removing all the identical members from the derived class, since they will already exist in the base class.

    Then you write a completely separate constructor, because those are not inherited from the base class. This is true even if its parameters will be identical. More on that below...

    Finally, since your update() method is different from that of the base class, you'll need to declare the base class method as "virtual". Otherwise, the polymorphism may not work as you expect when iterating through a loop of generic "Snake" object pointers, for example.

    So first, your slightly-updated base class will be:

    Code:
    class Snake {
    
        //Attributes
    
        protected:
    
            char direction_;
            char nd_;
            std::list<Position> segments;
            UINT    nodesize_;
            Image nodeimage;
            //Snake playerSnake_;
    
        //Operations
    
        public:
            Snake(int x, int y);
    
            //updates the position of the snake
            virtual void update();
    
            //renders the snake to the canvas
            void draw(Canvas& canvas);
    
            //sets the direction the snake will move
            void set_direction(char direction);
    
            //gets the direction the snake will move
            int get_direction();
    };
    ... and then your derived class definition will look like this:

    Code:
    class AISnake : public Snake {
    
        //Additional attributes
        //(None currently)
    
        //Operations
    
        public:
            AISnake(int x, int y);
    
            //updates the position of the snake
            virtual void update();
    };
    Your constructor for AISnake will look like this:

    Code:
    AISnake::AISnake(int x, int y) : 
    Snake(x,y)
    {
    }
    You'll see how, even though you have to write a new constructor, you can still simply pass the original parameters to the base class constructor using the initialization list. This assumes you're doing the same thing with x and y in both constructors. If not, obviously you'll write different code based on any different AISnake behavior.

    The reason I also declared the AISnake::update() method virtual is twofold: it reminds you that it is an overridden method, but more importantly, if you want to further derive another class from AISnake in the future, AISnake()::update() will need to be virtual as well. You don't have to leave that in if you don't want to, but it's a commonly-used convention.
    Last edited by jrohde; 05-20-2010 at 12:17 PM. Reason: grammatical errors

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. conditional inheritance?
    By arcaine01 in forum C++ Programming
    Replies: 17
    Last Post: 09-04-2009, 05:12 PM
  2. Replies: 16
    Last Post: 06-08-2009, 04:03 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 01:46 PM
  5. Your favourite fantasy game setting?
    By fry in forum Game Programming
    Replies: 4
    Last Post: 10-16-2002, 07:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21