Advertisement

Question on Sub Classes!

Started by March 17, 2000 12:38 PM
2 comments, last by Peddler 24 years, 7 months ago
Ok here is my problem. I have a sprite class mySprite, which handles the sprite objects and has a few set variables. Now I want to add more variables to that class without changing any of the MySprite class code. So am I correct in assuming I can just create a sub-class, like so: class mySubClass : public mySprite { //variables I want to add to the mySprite Class int Strength; int Dexterity; int Wisdom; }; Ok, now here is where I am confused....I create an object from the mySprite Class called Block. But how do I go about having the Block object access and change the new variables that I added to the mySprite Class through use of mySubClass? Block->mySubClass::Strength = 0;???? or what? Sorry if this is confusing, but I am a newbie to this stuff. Any help would be really appreciated as this has be stuck in the mud until I get it resolved. Thanks, -Tim Yarosh
Block needs to be a MySubClass. ;-)
Advertisement
Anonymous is right.

Here's an example:

class Super
{
public:
void Set(int x, int y);
protected:
int x;
int y;
};


class Sub : public Super
{
private:
int z;
};

Sub P;



Now the variable P is of type Sub which is a more specific type of Super .

P will have the variables that are in Super as well as all the functions. So P.Set(5,6) is perfectly valid. Soon you can start playing with virtual functions.



----
Andrew


Edited by - acraig on 3/17/00 1:40:11 PM
Thanks!

I implemented my new vars and everything is working fine, on to my next stumbling block

-Tim

This topic is closed to new replies.

Advertisement