-
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
-
Try to read your book, on inheritance, overloading and overriding.
-
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.
-
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.