Click to See Complete Forum and Search --> : C++ class access control
Pinky
01-01-2001, 12:01 AM
Can somebody please offer some insight on the differences between private and protected access controls? I have really never dealt with protected, and every definition I see seems to sound just like the definition for private, which I do use often (of course).
thanks!
dighn
01-01-2001, 05:02 AM
protected is like private with the exception that protected variables/methods can be accessed by the subclass(s) of the class
[This message has been edited by dighn (edited January 01, 2001).]
Klashe
01-02-2001, 12:12 PM
Originally posted by Pinky:
Can somebody please offer some insight on the differences between private and protected access controls? I have really never dealt with protected, and every definition I see seems to sound just like the definition for private, which I do use often (of course).
thanks!
A private member cannot participate in inheritance. Only protected members can.
Example:
Class X:{
Protected:
Int I;
Int J;
Private:
Int L;
Public:
Void get_ij();
Void put_ij();
};
Class Y : public X {
Int k;
Public:
Int get_k;
Void make K;
};
Here class Y has access to I and J in class X because they are protected. But Y does not have access to L, because it is private to class X.
Klashe
01-02-2001, 12:17 PM
Originally posted by Klashe:
A private member cannot participate in inheritance. Only protected members can.
Example:
Class X:{
Protected:
Int I;
Int J;
Private:
Int L;
Public:
Void get_ij();
Void put_ij();
};
Class Y : public X {
Int k;
Public:
Int get_k;
Void make K;
};
Here class Y has access to I and J in class X because they are protected. But Y does not have access to L, because it is private to class X.
Oh yeah, and ignore the caps in that last post. http://www.sharkyforums.com/ubb/smile.gif
Pinky
01-02-2001, 07:04 PM
thank you both! http://www.sharkyforums.com/ubb/smile.gif
Conrad Song
01-02-2001, 11:18 PM
Originally posted by Pinky:
Can somebody please offer some insight on the differences between private and protected access controls? I have really never dealt with protected, and every definition I see seems to sound just like the definition for private, which I do use often (of course).
thanks!
Member Inheritance
Before | After Inherited as
| Pub Pro Pri
-------+--------------------
Pub | Pub Pro Pri
Pro | Pro Pro Pri
Pri | Not Not Not