Use of C + + class access control--public,protected,private (not finished)

Source: Internet
Author: User

Reference Code

#include <iostream> #include <cstdio>using namespace std;class father {   int id;protected:int tt;public: Father (): ID (3) {}void print () {printf ("This was Father public function%d\n", id);} void Changeid () {id=10;printf ("Change the value of ID to 10\n");} friend void Change (father& f);  A friend function allows a function outside of the class to access members in the class. };class child:protected Father {Public:void play () {father::p rint ();//father::id=3;//base class private members whose derived classes do not have direct access to father::tt=2;   the//base class protects members whose derived classes have direct access to}void print () {printf ("This is a child function\n");}}; void Change (father& f) {f.id=10;f.tt=3;} int main () {child c;//c.id=3;//Private member only functions and friend functions in this class can access C.print (); C.play ();//c.tt=2;//protected members and private are similar, only a little different, A protected member in a base class has visibility into its derived class. Father F;change (f); F.print ();}


Summary:


When Private,public,protected is simply set as a member permission in a class:


Private: can only be accessed by a function in the class, its friend function, and cannot be accessed by any other object of the class.

Protected: can be accessed by functions in the class, functions of subclasses, and their friend functions, but cannot be accessed by objects of that class

Public: can be accessed by functions in the class, by functions of subclasses, by their friend functions, or by objects of that class


When Private,public,protected is inherited as a method:


For the public inheritance method:


(1) The public member of the parent class becomes the public member of the subclass, allowing code outside the class to access those members;
(2) The private member of the parent class is still the private member of the parent class, and the members of the subclass cannot access the member;
(3) The protected member of the parent class becomes the protected member of the subclass, and only the members of the subclass are allowed access;


(1) The visibility of the base class members to their objects:


Public members are visible, others are not visible. Here the protection members are identical to the private members.


(2) The visibility of a base class member on a derived class:


Public and protected members are visible, and private members are not visible. The protection of Members here is the same as public members.


(3) The visibility of a base class member on a derived class object:


Public members are visible and other members are not visible.


Therefore, when the public inherits, the objects of the derived class can access the public members in the base class, and the member functions of the derived class can access the public and protected members in the base class. Here, it is important to distinguish between objects of derived classes and member functions in derived classes that have different access to the base class.


for private inheritance methods:


(1) The public member of the parent class becomes the private member of the subclass, and only the child class members are allowed access;
(2) The private member of the parent class is still the private member of the parent class, and the members of the subclass cannot access the member;
(3) The protected member of the parent class becomes the private member of the subclass, and only the child class members are allowed access;


(1) The visibility of the base class members to their objects:


Public members are visible and other members are not visible.


(2) The visibility of a base class member on a derived class:


Public and protected members are visible, and private members are invisible.


(3) The visibility of a base class member on a derived class object:


All the members are invisible.


Therefore, in private inheritance, members of the base class can only be accessed by directly derived classes, and cannot be inherited further down.


For the protection inheritance method:


(1) The public member of the parent class becomes the protected member of the subclass, and only the child class members are allowed access;
(2) The private member of the parent class is still the private member of the parent class, and the members of the subclass cannot access the member;
(3) The public member of the parent class becomes the protected member of the subclass, and only the child class members are allowed access;


This inherits in the same way as private inheritance. The difference between the two is that the members of the derived class have different visibility to the base class members.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use of C + + class access control--public,protected,private (not finished)

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.