I want to make all functions in my base class invisible to my client, only accessible through my derived class.
Is there a way to do this in C++?
Thanks!
I want to make all functions in my base class invisible to my client, only accessible through my derived class.
Is there a way to do this in C++?
Thanks!
 
    
    There are two ways one is
Using Protected Keyword
class A{
    protected:
        void f(){
            std::cout << "F" << std::endl;s
        }
};
class B:public A{
    // access f() here...
};
Any derived class can access f() function from A class.
Second way: Making B class friend class of A.
#include <iostream>
class A{
    private:
        void f(){
            std::cout << "F" << std::endl;
        }
    friend class B;
};
class B:public A{
    A obj;
    public:
        void accessF(){
            obj.f();
        }
};
int main(){
    B obj;
    obj.accessF();
    return 0;
}
 
    
    Use access-specifier (if base class is not under your control):
class A
{  public:
    void f() {}
    void h() {}
};
class B: private A  // all members of A are private
{ public:
    void g()
    { f();
    }
    using A::h;     // adjust access
};
int main()
{ A a;
  a.f();
  B b;
  b.g();
  b.h();
  b.f();  // error: 'void A::f()' is inaccessible
}
