Why access a private member through a public interface (2)

Source: Internet
Author: User
Generally, Private Members and objects cannot be accessed directly. They can only be accessed through the interface functions set in the class. Why is this setting required? It is very simple. For the sake of security, you can see the following code: # define deiostreamusingnamespacestd; classHuman {public: voidset (intw); intprint (); private: intweigh

Generally, Private Members and objects cannot be accessed directly. They can only be accessed through the interface functions set in the class. Why is this setting required? It is very simple. For the sake of security, you can see the following code: # include iostreamusing namespace std; class Human {public: void set (int w); int print (); private: int weigh

Generally, Private Members and objects cannot be accessed directly. They can only be accessed through the interface functions set in the class. Why is this setting required? It is very simple. For the sake of security, you can see the following code.

# Include
 
  
Using namespace std; class Human {public: void set (int w); int print (); private: int weight ;}; int main () {Human Tom; Tom. set (111); cout <"Tom's weight:" <
  
   
0 & w <100) {weight = w;} else {cout <"the output is out of the range. Set the default value to 0" <
   
    

The code is very clear, so that access through the interface will be safe. For example, when setting data, you can also set the conditions in the print interface, in this way, the program security will be very good.

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.