• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

Using Public, Private,a nd Protected to Disallow Access to Certain Member Functions

Hello,

I have a BaseClass with 5 member functions: func1, func2, func3, func4, func5.

I want to create derived classes from BaseClass:
- DerivedClass1 must be able to access all member functions in BaseClass except func5.
- DerivedClass2 must be able to access all member functions in BaseClass except func1.

Is there a way that I can use access specifiers public, protected and private to structure my code in such a way that it restrict access BaseClass functions as indicated?

If anyone knows how I can achieve this result I would appreciate some assistance.  Thank you.
0
Invariant
Asked:
Invariant
1 Solution
 
skyDaemonCommented:
You could say:
BaseClass1
Protected:
func2;
func3;
func4;

BaseClass2 : public BaseClass1
Protected:
func1;

BaseClass3: public BaseClass1
Protected:
func5;

DerivedClass1 : public BaseClass2

DerivedClass2  public BaseClass3

0
 
GaryFxCommented:
Try using friend.

Gary
0
 
Mayank SAssociate Director - Product EngineeringCommented:
There is probably no way that you can have all the 5 functions in the same base-class and impose those restrictions on the derived classes at the same time. I guess what skyDaemon suggests is a way out.

Mayank.
0
 
InvariantAuthor Commented:
Thank you for your assistance and for confirming what I thought to be true, but what I was not certain of until hearing from you all.  As I will use SkyDaemon's approach I have offered SkyDaemon the points...but thanks again to all who offered their comments here.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now