classes and inheritance

I'm still new at using C++ and I'm having a problem with understanding classes and inheritance. Specifically, how a derived class can access the members of a private base class. For example, "class pet: private animal". How can class "pet" access members of class "animal" if "animal" is private. Any examples or explanations would be helpful. Thanks.
Specifically, how a derived class can access the members of a private base class.
I tale it you mean "private members of a base class". It can't.

Class data should never be public or protected, it should be private. Only the owning class should be able to access the class' data. External objects should only be able to interact with an object by sending it messages; in C++ speak, calling methods on a class.
I understand that when a class is private only the class is allowed to access the members.

"External objects should only be able to interact with an object by sending it messages; in C++ speak, calling methods on a class."

This means I would need to create an object such as "pet dog", and then using the object "dog" to call a function member in "pet" to access a member in class "animal". Am I understanding this correctly? Thanks.
kbw wrote:
class data should never be public or protected, it should be private.


That is entirely dependent on the problem being solved.
Class data should never be public or protected, it should be private.


What do you term as class data?

I hate devs who make everything private and cause deriving to be a pain...
kbw wrote:
Class data should never be public or protected, it should be private.


Let's say I have a car, which has a red chassis. I'm not going to go and ask the car what color it is, I'll just look at it, and see for myself. Similarly, unless Lexus came up with a new feature for their higher-end cars, I'm not going to ask the car to paint itself, I'll take it to a shop and ask them to do it for me, where they will paint the car (and not ask it to paint itself, hopefully).

My point is, there are definitely some cases in which even public data is acceptable. ;)

-Albatross
warning: you are mis-using private inheritance because pet isA animal, pet hasA animal

class pet: private animal

http://www.parashift.com/c++-faq-lite/private-inheritance.html#faq-24.2

private inheritance is special and different from public/protected/private members

if you want pet to have access to animal's members, you make those members protected - in the example below, the member in question is weight:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

class Animal {
 public:
    Animal() { } 
    unsigned getWeight() const { return weight; }
 protected:
    unsigned weight;
};

class Pet : public Animal {
  public:
    Pet() { weight = 99; }
};

main()
{
  Pet myPet;
  cout << myPet.getWeight() << endl;
}
Last edited on
I understand that when a class is private only the class is allowed to access the members.
Back to private classes.

Public inheritace implements "has a" relationship. Private inheritance implements "implemented in terms of" relationship.

The effect of private inheritance is to allow the derived class to overide virtual methods, but the details (methods and data) of the base class are invisible to the user of the derived class.

There are a number of features that follow specific implementation patterns in C++, this is one of them.


Concerning my comment: Class data should never be public or protected, it should be private.
Hands up all those who think that MFC's CWnd class should have a public data member m_hWnd.
How more costly would it be if MFC's CWnd class had an inlined public member method GetHandle() that returned the window handle?
How much more flexible would it be if access to an MFC window handle were accessed through a method? Maybe a debug version could assert the validity of the handle value or a checked build assert the validity of the handle itself? You can't do any of that because someone thought it would be more efficient to make the data public 16 years ago.


That is entirely dependent on the problem being solved.
On noddy little apps it doesn't matter what you do.

I hate devs who make everything private and cause deriving to be a pain...
I hate shortcuts, they always come back to haunt you.

I'm not going to go and ask the car what color it is, I'll just look at it, and see for myself...there are definitely some cases in which even public data is acceptable.
You can only talk to an object by sending it a message--those are the rules in OO. C++ makes OO efficient by checking senders/receivers at runtime so it can perform the sending the message as efficiently as in C. Of course, not all C++ programs are object oriented, my statement doesn't apply to other domains.
Last edited on
Topic archived. No new replies allowed.