Author Topic: [C++] Good model for using derived classes?  (Read 4622 times)

0 Members and 1 Guest are viewing this topic.

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
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?
« Last Edit: June 30, 2009 by Pixel_Outlaw »
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [C++] Good model for using derived classes?
« Reply #1 on: June 30, 2009 »
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
Code: [Select]
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
« Last Edit: June 30, 2009 by Jim »
Challenge Trophies Won:

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: [C++] Good model for using derived classes?
« Reply #2 on: June 30, 2009 »
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?
« Last Edit: June 30, 2009 by Pixel_Outlaw »
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: [C++] Good model for using derived classes?
« Reply #3 on: June 30, 2009 »
@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.
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: [C++] Good model for using derived classes?
« Reply #4 on: June 30, 2009 »


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.
Challenge Trophies Won:

Offline Japaneseninja

  • ZX 81
  • *
  • Posts: 16
  • Karma: 1
    • View Profile
Re: [C++] Good model for using derived classes?
« Reply #5 on: June 30, 2009 »
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.

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1292
  • Karma: 466
    • View Profile
    • my stuff
Re: [C++] Good model for using derived classes?
« Reply #6 on: June 30, 2009 »
Quote
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:
Code: [Select]
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:
Code: [Select]
...
  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.
« Last Edit: June 30, 2009 by hellfire »
Challenge Trophies Won:

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: [C++] Good model for using derived classes?
« Reply #7 on: June 30, 2009 »
Thanks so much hellfire.
Challenge Trophies Won: