C++ class access control

Sharky Forums


Results 1 to 6 of 6

Thread: C++ class access control

  1. #1
    Stormtrooper Mod Pinky's Avatar
    Join Date
    Oct 2000
    Posts
    2,971

    Post C++ class access control

    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!

    Proofread carefully to see if you any words out.

    [the rules]

  2. #2
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171

    Post

    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).]
    .

  3. #3
    Katana Wielding Moderator Klashe's Avatar
    Join Date
    Sep 2000
    Location
    IL, USA
    Posts
    3,306

    Post

    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.



  4. #4
    Katana Wielding Moderator Klashe's Avatar
    Join Date
    Sep 2000
    Location
    IL, USA
    Posts
    3,306

    Post

    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.


  5. #5
    Stormtrooper Mod Pinky's Avatar
    Join Date
    Oct 2000
    Posts
    2,971

    Post

    thank you both!

    Proofread carefully to see if you any words out.

    [the rules]

  6. #6
    Reef Shark
    Join Date
    Oct 2000
    Posts
    397

    Post

    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •