What is the difference between a publicaly derived class and a friend class?

Posted on 2007-07-30
Medium Priority
Last Modified: 2008-01-09
What is the difference between a publicaly derived class and a friend class?
Question by:DHamilton01
LVL 11

Accepted Solution

Jase-Coder earned 75 total points
ID: 19596853
A friend class can access the private elements of the other class, whereas a publicly derived class cannot directly access the private members of the base class.

Author Comment

ID: 19597271
I think I mis-represent the issue. What I meant to ask was the difference when the derived class is declared with 'private' priv...

LVL 29

Assisted Solution

pepr earned 75 total points
ID: 19597889
There are three levels of "a secret". Public (no secret), protected (secret for unrelated), private (secret for anyone but members of the class). When you derive one class from the other then you say whether you will change the levels of visibility of the base class members when using the derived class.

When you say public in front of the base class, then what is said inside the base class will hold also when working with the derived class. This means that public members of the base class will be available also as public when you use the derived class, private will  not be accessible, and protected will behave as if they were born protected in the derived class.

When you say private in front of the base class, then the base class method will not be available when using the derived class (i.e. when working with the instance of the derived class outside of the derived class).
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Assisted Solution

aatrish2001 earned 75 total points
ID: 19598030
class                        public                     private            protected

public   derivd          public                          private                 protected

private  dervd               no                             no                    no

protected  dervd          protected                private             protected

Assisted Solution

jasonclarke earned 75 total points
ID: 19598829
Maybe to add a bit more - the idea of public inheritance is that a classes clients know all about it - when they have an instance of it.  A class that inherits publicly from another class has an 'is-a' relationship with the parent class - so if B inherits publicly from A then B is an A.

The idea of private inheritance is that only the classes themselves know about it, i.e. it is an implementation detail of the class.  A class that inherits privately from another class has an 'implemented in terms of' relationship with the parent class - so if B inherits publicly from A then B is implemented in terms of A.

Maybe some code would help:

class A
      void f() {};

class B : public A

class C : private A

      B* b = new B;
      b->f();  // OK - public inheritance can see A's public and protected methods

      C* c = new C;
      c->f(); // Won't compile - private inheritance can't see A's methods

      A* a1 = b;  // OK - can cast a B to an A
      A* a2 = c;  // Won't compile can't cast to a A because of private inheritance
LVL 17

Assisted Solution

rstaveley earned 75 total points
ID: 19598943
Following on from Jason's illustration, a nice way to look at the difference is that B "is an A" and that C is "implemented in terms of A".

Everything in A's interface is available in an instance of B. If you want to work with a reference or a pointer to an A, you can pass a reference or pointer to B in its place, because B "is an A".

C isn't an A. The class happened to be implemented in terms of A, but you can't use it as an A, because it isn't an A. Jason illustrates this point.

The following class, D, is not all that different from C, but instead of being privately derived from A, it "has an A", which is private. There is different syntax required in the D implementation from C's implementation for working with A's public members, but D is more similar to C than B is.

   class D
    /* .... */
       A a;
    /* .... */

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question