Hi,
I have done some reading and searching without finding how subclasses relate to friend classes.
Here is a little scenario:

class Dog;
class Cat{
   public:
        int getAge(){return age};

   protected:
        friend class Dog;
        int age;

};

class Dog{
   public: 
        Boolean foo(){return True};

    protected: 
         friend class Cat;
};

class Puppy : public Dog{
};

class Kitty : public Cat{
};

How do the subclasses Puppy and Kitty relate to each other, and also to the parent classes Dog and Cat? What is inherited, and what limitations do they have (what objects will have access to what?). What effect does making the friend class protected have (and how would making it public make it different?)

I hope someone can help me reveal a bit more of how C++ inheritance works in general, and how it applies to friends as well.

Thanks in advance for any help!

Recommended Answers

All 9 Replies

Just remember two basic rules and you'll be fine:

1) Being my friend doesn't make you a friend of my children.
2) Being my friend doesn't make you a friend of my other friends.

>What effect does making the friend class protected have
Zero. It doesn't matter if the friend declaration is private, protected, or public, the effect is the same across the board.

Dog and Cat don't relate to each other, and should probably not be friends. (No joke intended, though it is funny anyway...)

"Friends" in the C++ sense mean that some foreign object has access to your object's internals. This is useful, and quite often, but should not be used indiscriminately.

As for inheritance, both Dog and Cat could derive from a common ancester, say "Mammal" or some such. Then they could have methods in common, like Speak() and Eat().

It would probably be worth your while to spend some time over at the C++FAQ-Lite.

Hope this helps.

[edit] Hmm, too slow again... :P

basically a friend class would be a class that is allowed to access private variables of the class it is friends with

as in dog can access cat's age by calling getAge(); from within the dog class...

as far as how kitty and puppy work?
cat has a friend class called dog as a protected variable

dog has a friend class called cat as a protected variable

you should read about how protected variables are inherited, I think your confusion comes from there.

the way protected variables are inherited isn't exactly intuitive, It'd do you some good to read up on what it means for something to be 'protected'

narue's solution was much more elegant... easier to understand... great analogy!

Thanks for all the responses! I am still a bit in the wilderness though...

Dog and Cat don't relate to each other, and should probably not be friends. (No joke intended, though it is funny anyway...)

Uhm, the naming was not meant as a realistic examples. I guess I should stick to foo and bar in the future :)
Make it a TV and a RemoteControl instead...
Say I then have different types of TV that I create subclasses for inheriting TV, and then different kinds of remote controls inheriting RemoteControl to fit the different TVs. I am wondering if then a FancyRemote class inheriting RemoteControl will have access to the TV superclass.

Based on Narure's response I gather that I need to make the subclasses of TV and the corresponding subclasses of RemoteControl friends of each other - and simply make sure that a subclass has access to any parent function the friend class might need (define them protected in the parent class).

My understanding of protected means that the subclass will have access to the protected parent variable as a private variable. In that sense, it would seem to me that the friends class also should be inherited, which Narure indicates is not the case. So yeah, I will go do some more reading on protected variables. I guess just because a subclass inherits access to the variables of the parent, the friend is not a variable like types and objects (?).

>Based on Narure's response I gather that I need to make the subclasses of TV and the
>corresponding subclasses of RemoteControl friends of each other - and simply make sure that
>a subclass has access to any parent function the friend class might need (define them
>protected in the parent class).
Why don't you just make sure that the public interfaces are sufficient for communication between the TV and remote classes? You probably don't even need friends.

Seconded. The remote control shouldn't have any clue what is going on inside the TV.

I am using a code library that uses friends - and I am extending the library functions by adding some subclasses. It has not been my design choice, and I am not in a place where I can change the original structure. Like I wrote previously - the example is an example for the sake of simplifying the question (Cat, Dog, TV, Remote, Foo, Bar - whatever...).

I thought I asked a question about friends and how inheritance works with classes that have friends, not whether cats and dogs can be friends, or what access a remote control should have to a TV. Btw, the latter is the example used by the 'C++ Primer Plus" book by Stephen Prata when explaining friends.

>I thought I asked a question about friends and how
>inheritance works with classes that have friends
Yes, that was indeed your question.

>not whether cats and dogs can be friends, or what
>access a remote control should have to a TV
There's no need to get defensive. Besides, my answer is independent of your design. A good bridge between two classes can take the place of friends, increase flexibility, and reduce coupling.

>Btw, the latter is the example used by the 'C++ Primer Plus"
>book by Stephen Prata when explaining friends.
Beginner books often choose examples that are more accessible to the target readership and are designed to teach a programming concept rather than a good design concept. They're typically nonsensical (the remote control example), broken (the shape example), or woefully incomplete (any animal example).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.