Derived Classes

What is the difference between the following two definitions? Like how does the public affect the behavior of the derived class?

 
  class ForRent : public Property


class ForRent : Property
second one if equal to class ForRent : private Property
http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/
Ok thanks. Why would anyone use private?

Its when you want to protect the variables of the inherited class from modification.
Last edited on
Ramzi89 wrote:
Why would anyone use private?

It is almost never used. Almost in every case where private inheritance can be used is better to use aggregation.
> Why would anyone use private?

FAQ: http://www.parashift.com/c%2B%2B-faq-lite/priv-inherit-vs-compos.html
"when you want to build a class Fred that uses code in a class Wilma, and the code from class Wilma needs to invoke member functions from your new class, Fred" - for instance, customized reuse of a class using the template method pattern (without voilating the Liskov substitution principle).

The second answer by fizzer here: http://stackoverflow.com/questions/656224/when-should-i-use-c-private-inheritance
In addition to the adapter pattern that is mentioned, also the Bridge pattern. In both cases, where we have an IS-LIKE-A rather than an IS-A relationship, and we want to avoid redundant boiler plate code of a massive scale.

Inheriting publicly from an interface, and privately from an implementation (adapter, bridge) is canonical C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct our_window_impl : public our_window_interface,
                         private their_window_implementation
{
       our_window_impl( /* .... */ ) 
       {
              // ....
              their_window_manager::instance().register(this) ;
       }

       // ....
       
       protected :
              virtual void their_template_method() override
              {
                    // ...
              }

       // ....
};

Following on from what JLBorges and MiiNiPaa said...

In short,

- public inheritance is for implementing the "is a" relationship

In other terms, it's inheritence of interface, with or without implementation. The interface defines the behavior of the class to the outside world. By inheritenting the base classes interface, you inherit its behaviour. Hence it acts like it is an instance of the base class.

(to inherit interface without implemetation you need to override all inherited methods; or use a base class with no implementaton -- a vtable only class.)

- private inheritence is for implementing the "is-implemented-in-terms-of" relationship

This is inheritence of implementation only. The methods of the base class are hidden so cannot be used by the outside world. So the derived class can make use of the base class functionality, but that's all.

Scott Meyers (Effective C++) remarks that there are some circumstances where private inheritence is preferable to composition, either because it simplifies the implementation or because it allows the compiler to use empty base class optimization.

I think he talks about composition rather than than aggregation as the object may be treated as a whole in the case of composition, whereas an aggregate is really a collection of cooperating objects (with distinct lifetimes.)

Andy

PS Scott Meyers : Effective C++
http://www.amazon.co.uk/Effective-Specific-Addison-Wesley-Professional-Computing/dp/0201924889
Last edited on
OK thanks Andy.

Would anyone be able to explain the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;

class ClassA
{
public:
    void print(){ cout << "Hello\n\n"; }
};

class ClassB
{
public:
    void print(){ cout << "World\n\n"; }
};

class ClassC : public ClassA, public ClassB
{
public:
   void print(){ cout << "Nice to meet you\n\n"; }
}c;

int main()
{
    ClassA * pc1 = new ClassC; //Pointer to base class ClassA that 
//references an object of classC. So *pc1 inherits the members from ClassA.
    ClassB * pc2 = new ClassC; //Pointer to base class ClassB that 
//references an object of classC. So *pc2 inherits the members from ClassB.
    c.print(); //Accessing the implementation of display() in ClassC directly without using pointers.
    pc1->print();
    pc2->print();
    return 0;
}


What I do not understand is how can class C inherit both class A & B? Fair enough if one is specified i.e with pointers in the above example but what happens when one is not specified i.e with the object c in the above example (I don't understand the creepy instantiation of c as well)?

Also with the addition of virtual??? My understanding of virtual is that it allows for dynamic binding (i.e choice of function called is at run time) and when you place it in front of the base classes method it will allow outside functions to access the correct method when the parameter is passed as reference. But what effect does it have when it is used within a class?
Last edited on
Virtual just means, forget what type the pointer is and just follow it to the reference???

So it has no effect when an object is called in a member function because the object is actually calling the function so theres no confusion about type as the actual objects reference is available.

Its only when your calling using base classes/pointers that virtual takes an effect. And essentially it makes that the function of the actually object of the referenced address is called at runtime???

Inspector Ramz.
Last edited on
Topic archived. No new replies allowed.