1 protection for C + + class members
2 the nature of C + + classes
1 protection for C + + class members
If the class function returns a pointer to a member variable, the function returns a constant pointer in order to avoid modification of the external member variable.
1 classMans2 {3 Private:4 Charname[ -];5 intAge ;6 Public:7 voidSet_name (Const Char*s)8 {9memset (Name,0,sizeof(name));//InitializeTen if(strcmp (s),"Tom") ==0)//You can't name Tom . One { A return; - } - strcpy (name, s); the } - voidSet_age (inti) - { -Age =i; + } - Const Char*get_name ()//returns const, security. If you do not add a const, you can force the modification by address + { A returnname; at } - intget_age () - { - returnAge ; - } -};
If a class member variable has the same name as a global variable extern, the member variable of the class is accessed by default in the class member function.
Inside the class, access the global variable extern, using the identifier double colon::
1 classMans2 {3 Private:4 Charname[ -];5 intAge ;6 Public:7 voidSet_name (Const Char*s)8 {9memset (Name,0,sizeof(name));//InitializeTen if(strcmp (s),"Tom") ==0)//You can't name Tom . One { A return; - } - strcpy (name, s); the } - voidSet_age (inti) - { -:: Age = i;//Double colon:: Represents the global variable extern + } - Const Char*get_name ()//returns const, security. If you do not add a const, you can force the modification by address + { A returnname; at } - intget_age () - { - returnAge ; - } -};
2 the nature of C + + classes
The class is actually the data member of the structure plus executable code, while providing encapsulation, inheritance, polymorphism.
Inside the class, there is no permission qualifier, and the default is private
But inside the struct, there is no permission qualifier, and the default is public
123
2 Classes and objects