2 Classes and objects

Source: Internet
Author: User

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

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.