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

Posted on 2007-07-30
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

    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

    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 28

    Assisted Solution

    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).
    LVL 3

    Assisted Solution

    class                        public                     private            protected

    public   derivd          public                          private                 protected

    private  dervd               no                             no                    no

    protected  dervd          protected                private             protected
    LVL 9

    Assisted Solution

    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

    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;
        /* .... */

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    Title # Comments Views Activity
    MSVCR80.dll crash 2 121
    The dreaded error 1935 with VC++ 2008 Redistributable 12 61
    Auto build a project upon execution 20 60
    C++ question 3 25
    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now