Jump to content
  • Advertisement
Sign in to follow this  
Kenny77

(Base Object) Inheritance Problems

This topic is 3753 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to create a little base object class that the rest of my objects (intro screens, menu buttons/controls, gameplay entities, etc) can inherit from, but I'm having a little trouble deciding what I should include in this class. The things I have now are: public: Constructor/Destructor GetPosition() Method private: Draw() Method SDL_Surface*, which represents the object's bitmap SDL_Rect, stores the objects position ...and my IntroScreen object inherits from this and adds public: Constructor(accepts file path to bitmap) Destructor private: Draw(gets passed the screen surface) UpdateScreen(), used for updating the fade in/out effect int, for controlling the ammount of time the screen is displayed This appears to me to be a good way to organize this, but I get a few errors in the IntroScreen constructor saying that "base object variable is private within this context". Are the private fields not inherited too?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Are the private fields not inherited too?


No, they're not. Private means "cannot be seen or accessed outside of this class". If you want "cannot be seen or accessed outside of this heirarchy, then use 'protected' members/attributes.

Share this post


Link to post
Share on other sites
If you want to use things declared private in the base class in the derived classes you have to use protected: instead of private: because private base class data cannot be accessed from a derived class.

Also if you want pointers on what you should include in the base class for your derived classes you should be specific in what is inheriting from this base class. You list intro screens, menu buttons, gameplay entities, which don't have that much in common, so I don't know if having them all inherit from a base class would be a good idea.

Share this post


Link to post
Share on other sites
Quote:
Original post by Timkin
Quote:
Are the private fields not inherited too?


No, they're not. Private means "cannot be seen or accessed outside of this class". If you want "cannot be seen or accessed outside of this heirarchy, then use 'protected' members/attributes.


Whoops, thought it was the other way around.

Share this post


Link to post
Share on other sites
Quote:
Original post by Timkin
Quote:
Are the private fields not inherited too?


No, they're not. Private means "cannot be seen or accessed outside of this class". If you want "cannot be seen or accessed outside of this heirarchy, then use 'protected' members/attributes.


The private fields are inherited by subclasses but the subclasses don't know about the fields. In order for the subclasses to have access to those fields, they can use functions provided by their parent class. For example,


class A {
private:
int variable;
protected:
void setVariable(int v){ variable = v; }
public:
int getVariable(){ return variable; }
};

class B : public A {
B(){
variable = 12; // Illegal - variable is private and subclasses of A cannot access the variable because it is unknown to them
setVariable(12); // Legal because subclass is using function provided by parent
}
};

A a;
a.setVariable(1);
a.getVariable() // returns '1'

B b;
b.setVariable(2);
b.getVariable() // returns '2'


Note: This is true for Java at least, haven't actually tried in C++ but I'm pretty sure this works in C++ as well.

Share this post


Link to post
Share on other sites
Or you can make the function a friend function inside the class
ie
class a
{
int a;
friend void fun();
};
a obj;
void fun()
{
obj.a;//int a is accessdible here
}

Although its much better if you just make the members protected or public if you want to use them outside the class' scope

You can also make a class a friend in the same manner

Share this post


Link to post
Share on other sites
Try to avoid friend as much as you can. Friends mean that you have into take into account those friend objects whenever you look at a class that has friends. That further means extra processing for your brain whenever you look at that class.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!