c++ problems and solutions related to oops
1->How to publicly inherit from a base class but make some of public methods from the base class private in the derived class?
ans-
1) You can use public inheritance and then make bar private:
class Base {
public:
void foo(){}
void bar(){}
};
class Derived : public Base {
private:
using Base::bar;
};
2) You can use private inheritance and then make foo public:
class Base {
public:
void foo(){}
void bar(){}
};
class Derived : private Base {
public:
using Base::foo;
};
Note: If you have a pointer or reference of type Base which contains an object of type Derived then the user will still be able to call the member.
Comments
Post a Comment