Dark Bit Factory & Gravity
PROGRAMMING => C / C++ /C# => Topic started by: Pixel_Outlaw on June 30, 2009
-
Through some more reading I have just found out that derived classes maintain protected member data from the base class. Even though my book says that the private member data exists in the derived classes; isn't it wasteful to have a bunch of extra methods and such that cannot be used from the base class tagged on to the derived class?
-
There can be public methods on the base class (or friend classes) that access the private data. If they end up not being accessable at all, then they're wasted, but then you'd probably want to re-think why you built the classes to have unused data in the first place ;)
Or maybe you mean
class shape
{
...
}
class circle : shape
{
private int radius;
}
Then shape definitely should not be able to see radius.
You need to be really specific with your questions - 'initial class' doesn't mean anything, I assume you mean base class. public, private, protected, derived mean very specific things.
Jim
-
The following sentences confused me a bit:
"A derived class has almost as much access to members as does a base class because the derived class inherits most of the base class's functionality. The only items that a derived class cannot access are the base class's private members."
This is saying that it can't access its own inherited members if they were private in the base class? Does this mean a bunch of dead data? Should I not use private declarations in base classes?
-
@PO:
AFAIK, private methods/variables are not available in derived
classes. So, if you write a class where it doesnt make sense
that derived classes should inherit a certain property use the
private attribute.
If you want that certain values are still available in derived
classes, use protected attribute.
-
Ahh OK thanks, so far I've just seen "private" and "public" data members in classes. I'll have to look into "protected" data members.
-
Yes, this is correct. When you want the data to be inherited (in a *modifiable* form) by the derived class, use the "protected" keyword instead of "private" in your base class.
-
This is saying that it can't access its own inherited members if they were private in the base class?
Generally private members are not susposed to be accessible from "outside".
It's considered good habbit to keep all member-variables private and provide public getter- & setter-methods instead:
class Name
{
private:
String mForename;
String mSurName;
public:
String getForename()
{
return mForename;
}
void setForename(String name)
{
mForename= name;
}
String getSurname()
{
return mSurname;
}
void setSurname(String name)
{
mSurname= name;
}
};This might look long-winded but makes your code much more maintainable.
A drawback of this approach is that any returned object gets copied.
This can noticeably hurt performance when dealing with complex objects.
Instead you can return a reference (which is essentially a pointer just without the related syntax) and additionally make it constant so the caller can't change it:
...
const String& getSurname()
{
return mSurname;
}
(this is unnecessary for "simple" data-types)
Generally it's advisable that each class completely manages its' own members, so it's rarely required to poke inherited data.
-
Thanks so much hellfire.