Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Slaru

Classes and Inheritance

This topic is 5211 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

Hello, Say i was to declare a class as so. class Foo { public: Foo(); ~Foo(); void DoSomething(); private: int a; }; ... and derive a class like so. class BetterFoo : public Foo { public: BetterFoo(); ~BetterFoo(); void DoSomethingBetter(); }; 1. When I derive using ''public Foo'', what does the keyword ''public'' mean, and can you use other keywords? (eg. protected, private?) 2. If I wanted the derived class ''BetterFoo'' to have access to the Foo''s variable ''a'', but other classes that I derive to not, do I manipulate the keyword ''public'' as stated in question 1 to control the kind of access? If not, how can I control the access to Foo''s variable ''a''? I hope you understand my questions. Thanks. Slaru

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Slaru
Hello,
Say i was to declare a class as so.

class Foo
{
public:
Foo();
~Foo();
void DoSomething();
private:
int a;
};

... and derive a class like so.

class BetterFoo : public Foo
{
public:
BetterFoo();
~BetterFoo();
void DoSomethingBetter();
};

1. When I derive using ''public Foo'', what does the keyword ''public'' mean, and can you use other keywords? (eg. protected, private?)



You can. Using keywords protected or private there will result in protected or private inheritance, which are rarely things that you want.

quote:

2. If I wanted the derived class ''BetterFoo'' to have access to the Foo''s variable ''a'', but other classes that I derive to not, do I manipulate the keyword ''public'' as stated in question 1 to control the kind of access? If not, how can I control the access to Foo''s variable ''a''?


Sorry. Try that again?
You want BetterFoo to have access to Foo::a.
By "other classes that I derive to not", do you mean other subclasses of Foo should not have access to it, or do you mean that subclasses of BetterFoo should not? Or something else perhaps?

Share this post


Link to post
Share on other sites
You are referring to the type of inheritance. Its significance is defining the access level between the derived class and its base classes members.

Kuphryn

[edited by - kuphryn on May 9, 2004 11:23:00 AM]

Share this post


Link to post
Share on other sites
Thanks. I now learned just a tid more of the deep C++ stuff. I solved my problem with a much different approach so that BetterFoo doesnt even derive from Foo, but Foo''s ''a'' is a friend of BetterFoo''s DoSomethingBetter() function.

Share this post


Link to post
Share on other sites

  • 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!