0

My question is simple: ~Given two classes, I want to one of them to extend the other one, but turning some methods to be private:

Class B
public Method a();
public Method b();
public Method c();

Class A extends B
private Method a();
private Method b();
public Method c();

Is this possible and how? Thanks!

Samer
  • 1,923
  • 3
  • 34
  • 54
Ricardo Alves
  • 1,071
  • 18
  • 36

3 Answers3

1

Use private inheritance, all the function in base class B will become private.

  class A:   private B
  {
  }

Difference between private, public, and protected inheritance in C++ are explained here.

Community
  • 1
  • 1
Matt
  • 6,010
  • 25
  • 36
1

This is what Private Inheritance is for.

class A:   private B
{
    // All methods of class B are now private.
    // To make some "public" again:
    public:
        Method c()  { return B::c(); } // Call the private c-method from class B.
};
abelenky
  • 63,815
  • 23
  • 109
  • 159
0

You could change the inheritance type from public to private, when you declare class B.

class B : public A {
   private:
      baseMethod();
};
or
class B : private A {
   public:
      baseMethod();
};

The use appropriate override that you want for each method.

Looks like you don't want all of the methods to turn private. Choose the type of inheritance based on the fraction of methods changing their visibility.

KalyanS
  • 527
  • 3
  • 8