7. C ++ class and encapsulation concept, 7. c encapsulation Concept

Source: Internet
Author: User

7. C ++ class and encapsulation concept, 7. c encapsulation Concept

Classes are generally divided into the following two parts:

  • -ClassOfInternalImplementation
  • -ClassOfExternalUsage

For example:

UserTo use a mobile phone, you only need to knowHow to Use.

WhileMobile phone developers, You need to consider the mobile phoneInternal implementationDetails.

 

 

Class Encapsulation

Not every member variable and member function of the class must be made public.

For example:

Girls do not want outsiders to know their weight and age

Boys don't want outsiders to know their height and salary

Some attributes are made public to the outside world.

For example:Name, education, nationality, etc.

Therefore, you can define the access level for the member variables and member functions in the class:

  • -Public Public member,Allow member variables and member functions to be used inside and outside the class (public by default)
  • -Private Private member, Can only be used inside the class

Scope of Class Members

The scope of all class members isOnly inside the class,External access failure

Member FunctionsYou can directlyAccess member variablesAndCall member functions

Class can be externalAccess public members through class variables

Class member'sScopeAndAccess LevelNo relationship

 

Refer to the following code:

# Include <stdio. h> int I = 1; // defines the global variable struct Test {private: int I; // defines the private member variable ipublic: int j; int getI () {I = 3; return I ;}; int main () {int I = 2; // defines the local variable I Test; test. j = 4; printf ("I = % d \ n", I); // I = 2; use the local variable printf (": I = % d \ n ",: I); //: I = 1; use the global variable // printf ("test. I = % d \ n ", test. i); // Error accessing the private member, Error printf ("test. j = % d \ n ", test. j); // test. j = 4 printf ("test. getI () = % d \ n ", test. getI ());
// Test. getI () = 3 access private member return 0 through public member ;}

 

Summary

Classes are generally dividedUsageAndInternal detailsTwo parts

Class encapsulation mechanism (Public/privateMake the usage and internal detailsPhase Separation

 

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.