-1

I have a base class with several public methods and want to derive a child class which inherits only certain functions, so I derived the child class using private inheritance. Also I vave come across several times (including C++ primer plus) that private inheritance is almost exactly like containment. Now I have a problem where I want to use a function defined in Base to be available in the derived class.

The derived class doesn't have any private members in addition to those required by the base class. I'm not sure how to define public method "func" for derived class as shown in the code.

class Base
{
private:
    double *p;
public:
    Base(int m, int n);
    void fun(const Base & obj1, const Base & obj2)
};

class Derived : private Base
{
public:
    Derived(int n) : Base(1,n) {}
    void fun(const Derived & obj1,const Base & obj2)
    {
    /* The function fun in derived class works almost exactly 
       like fun in Base class but I don't know how to call 
       Base.Fun(...). Also all the data needed to perform 
       operations in this function is a part of the private 
       member of the base class which the Derived member can't 
       access.
    */
    }
}

If I were to use containment I could have just defined as follows:

class Derived
{
private:
    Base P;
public:
    void fun(const Derived & obj1,const Base & obj2)
    {
        Base :: P.func(obj1.P,obj2);
    }
 };

This make me wonder if containment is more appropriate here than private inheritance. On the other hand I'm not sure if either of the implementations are correct. So I'm looking for possible ways to do this.

Please note that I have not shown copy constructors,assignment and operators and some other methods in the codes above but I'm aware of their requirement. The codes are just to serve the basic purpose of showing private inheritance and containment.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
itachi
  • 192
  • 2
  • 10
  • 1
    Possible duplicate of [When to use C++ private inheritance over composition?](https://stackoverflow.com/questions/6297331/when-to-use-c-private-inheritance-over-composition) – Raedwald Nov 25 '17 at 21:09

1 Answers1

1

If the fun method of Derived can only be implemented by directly accessing the private members of Base, neither inheritance or containment will allow Derived to access those methods. Using inheritance the member would either need to be protected (or public) or getter/setter methods that are protected (or public) would need to be added. Using composition the public getter/setter methods would be required to avoid making those data members public.

In the fun method of Derived this:

Base::fun(obj1, obj2) 

will call the fun method of class Base.

Josh Heitzman
  • 1,816
  • 1
  • 14
  • 26
  • Josh, the methods don't need to directly access private members of the Base class. I tried to avoid getter and setter because it would require copying each time. Can you tell me why the code snippet that I have shown for containment would not work? – itachi Nov 19 '12 at 22:15
  • The call in the fun method Derived needs to be `P.Base::fun(obj1.P,obj2);` to compile correctly. – Josh Heitzman Nov 19 '12 at 22:26
  • By the way in in your comment you "Also all the data needed to perform operations in this function is a part of the private member of the base class which the Derived member can't access." which is where my statement about directly accessing the primate members of Base came from. – Josh Heitzman Nov 19 '12 at 22:28