For example, class Base has two public methods: foo() and bar(). Class Derived is inherited from class Base. In class Derived, I want to make foo() public but bar() private. Is the following code the correct and natural way to do this?
class Base {
public:
void foo();
void bar();
};
class Derived : public Base {
private:
void bar();
};
Section 11.3 of the C++ ’03 standard describes this ability:
So there are 2 ways you can do it.
1) You can use public inheritance and then make bar private:
2) You can use private inheritance and then make foo public:
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.