Analysis of C + + access properties and Inheritance properties

Source: Internet
Author: User
Tags access properties

for Beginners C + + Property control and inheritance properties or comparison that master, the author here summarizes their own study of these processes of insight. First we look at:

1, C + + supported access properties have public, protected, private, down I talk about my learning process some of the understanding.

1. For public

1. Functions in the class, functions in subclasses, access to their friend functions, and objects in the class

                     2 and I have always felt that it is a good idea to use code to understand concepts, and below I will give some of my test cases:

class base{public:    //friend function     friend void friend_show (BASE&NBSP;&AMP;BB);p ublic:    base (): X (0), px (0) { }    ~base () {  }public:    //Public Property     int px;    void  showbase ()     {        cout<< "i  Am show base and am public "<<endl;    }     void test ()     {        cout<< "I  am test "<<endl;        //test protection method call public method succeeded          print ();         //test public properties can be modified by the protected method         cout<< "px = " <<px<<endl;         //Test Private method call public method          print_private ();         //Testing Private methods Modify public properties           cout<< "px = " <<px<<endl;             }protected:    void print ()     {         cout<< "i am print " <<endl;         //public method protected member access          showbase ();          //Protection method Modify public properties           px = 10;     }private:    void print_ Private ()     {        cout<< "i am  Pint_private "<<endl;         //public members are accessed by private members          Showbase ();          //Private method Modify public properties           px = 20;    }private:    int x;}; Void friend_show (BASE&NBSP;&AMP;BB) {    cout<< "I am friend_Show" < <endl;    //Friend method Access public property     bb.px= 30;    // The Friend method accesses the public method &NBSP;&NBSP;&NBSP;&NBSP;BB. Showbase ();} Void main () {    base bb;    bb. Test ();     //testing Friend method methods public properties and Methods      friend_show (BB);      cout<< "bb.px = " &LT;&LT;BB.PX&LT;&LT;ENDL;}

Operation Result:


The above test code I give as many comments as possible, verify the above several properties, in addition to the subclass of the function of the Access property does not give the test case, the other gives the test case. Access properties for a function in a subclass I'll summarize it as a dedicated module below.

2, for ptotected

1, can be a function in a class, a function in a subclass, its friend function, the object of the class access

2, here I also give the test code:

class base{public:    //friend function     friend void friend_show (BASE&NBSP;&AMP;BB);p ublic:    base (): X (0), pp (0)   {   }     ~base ()     {    }public:    void  showbase ()     {        cout<< "i  Am show base and am public "<<endl;         //Call Protection Property         print ();         //Modifying protection Properties         pp = 40;     }    void test ()     {         cout<< "I am test" <<endl;        // Test protection method Call protection method &NBSP;&NBSP;&NBSP;&NBsp;    test_proteced ();         //test Public Protection method Modify protection properties         cout<< "pp = " <<pp<<endl;         //Test Private method call protection method           Print_private ();         //test Private method Modify protection properties           cout<< "pp = " <<pp<<endl;          //test public method Call protection method           showbase ();         //Test public methods Modify protection properties            cout<< "pp = " <<pp<<endl;             }    void testfriend ()     {         cout<< "I am testfriend" <<endl;          cout<< "pp = " <<pp<<endl;    }protected:     int pp;    void print ()     {         cout<< "i am print and am protected " <<endl;    }    void test_proteced ()      {        cout<< "i am test_proteced " < <endl;        //Test Protection method call          print ();         pp = 50;    } Private:    void print_private ()     {         cout<< "I&nbSp;am pint_private "<<endl;        //public member accessed by private member          print ();         //Private method Modify protection properties          pp = 60;    }private:     int x;}; Void friend_show (BASE&NBSP;&AMP;BB) {    cout<< "I am friend_Show" < <endl;    //Friend method Access Protection properties     bb.pp= 30;    // Friend method Access Protection method &NBSP;&NBSP;&NBSP;&NBSP;BB. Print ();     }void main () {    base bb;     bb. Test ();     //testing Friend method methods public properties and Methods      friend_show (BB);      bb. Testfriend ();}

Operation Result:


I have also given as much detail in the code as possible, and I will summarize the access rights of the child functions behind them as a special module, except for the access to the child functions.

3. For private modifier

1. Only functions in the class and their friend functions can be accessed, and cannot be accessed by any other function or object.

2, I also give the test code:

class base{public:    //friend function     friend void friend_show (BASE&NBSP;&AMP;BB);p ublic:    base (): X (0)   {  }     ~base ()    {    }public:    void showbase ()     {        cout<< "I am show  base and am public "<<endl;        //Call private property         print_private ();         //Modify private property         x = 200;    }     void test ()     {         cout<< "I am test" <<endl;        // Test public method Call protection method         showbase ();         //test public methods modify private properties          cout<< "x = " <<x<<endl;         //Test Protection method call private Method          print ();         //Test Protection Method Modify private property          cout << "x = " <<x<<endl;         // Test Private method Call private Method          test_private ();     The      //test is a private method that modifies a private property          cout< < "x = " <<x<<endl;        }     void testfriend ()     {        cout< < "I am testfriend" <<endl;         cout<< "x = " <<x<< Endl;    }protected:    void print ()     {         cout<< "i am print and am  protected  "<<endl;        //testing methods for accessing private properties          print_private ();         //the properties of the Test call property         x = 100;    }private:     void print_private ()     {         cout<< "I am pint_private" <<endl;    }     Void  test_private ()     {        cout << "I am test_priavate"<<endl;        print_private ();     }private :    int x;}; Void friend_show (BASE&NBSP;&AMP;BB) {    cout<< "I am friend_Show" < <endl;    //Friend method Access private property     bb.x= 300;    // The Friend method accesses the private method &NBSP;&NBSP;&NBSP;&NBSP;BB. Print_private ();     }void main () {    base bb;     bb. Test ();     //testing Friend method methods public properties and Methods      friend_show (BB);      bb. Testfriend ();}


Finally, it also gives the Privete access control test code and verifies the access control permissions given above me.

Confined to space here only basic access control permissions are given, no access control permission test cases are given for subclasses, and rules, access control permissions in subclasses I will give in the next Article blog post.

Analysis of C + + access properties and Inheritance properties

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.