C ++ basics: public, private, and protected

Source: Internet
Author: User

Public MemberProgramThe class that implements Information Hiding can be accessed anywhere and its public members are restricted to member functions. This function defines the private member that can be used by General programs to manipulate objects of this type. the data member can only be declared as private by the class with Information Hiding implemented by the member functions and the class's friends.

Protected member protected member performs private on the derived class just like a public member for other programs.

PRIVATE: The members declared in the Section (data members or member functions) can only be accessed by the member functions and Friends of the class.

In the protected section of the class, the declared members (no matter data members or member functions) can only be accessed by the member functions of the class, the member functions and Friends of the friends and sub-classes.

In the public: Section of the class, the declared members (whether data members or member functions) can be accessed by anyone.

Public, private, or proteced, which controls how variable members and member functions are accessed within and outside the class. In-class access refers to access using class member functions, while in-class access refers to access using objects or pointers to objects.

When a class member is defined as public, it can be accessed outside the class, including its derived class.

When a member is defined as private, it can only be accessed within the class and cannot be accessed by its derived class.

When a member is defined as proteced, it can only be accessed within the class, but can be accessed by its derived class.

When no access specifier is specified for a member, the default value is private.

When defining a derived class, the access specifier can also appear before the base class, which controls the access methods of the variable members and member functions of the base class in the derived class. When the access description is public, it is called public inheritance. Similarly, when the question specifier is protected, it is called protection inheritance, and when the question specifier is private, it is called private inheritance.

In public inheritance, the Public Member of the base class becomes the Public Member of the derived class, and the protected member of the base class becomes the protected member of the derived class.

When the inheritance is protected, the public and protected members of the base class become the protected members of the derived class.

When private inheritance is performed, the public and protected members of the base class are both private members of the derived class.

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.