Differences between protect, public, and private

Source: Internet
Author: User

Briefly introduce the differences between protect, public, and private, which will not be detailed later. Welcome to correction ~


 
Take C ++ as an example:
 
Public: Data and function members can be directly used in member functions, friends, and inheritance classes. It can also be used as an interface for users of the class (that is, A,
A. change ()).
 
Private: Data and function members, which can only be accessed by class members and friends. Cannot be accessed by an inherited class (no matter what type of inheritance), although
Private member of the class, but the parameter is passed by calling the constructors of the base class. The inherited class cannot access the Private Members of the base class.
 
Protect: the usage of the base class is the same as that of the private class. The base class object cannot be accessed. In a derived class, a member of the same base class is public and can be accessed and used by its inherited class object.
 
[Cpp]
Class Base
{
Private:
Int age;
Protect:
Int num;
}
Class Devi: public Base
{
}
Base B; Devi d;
B. num = 3; // error
D. num = 3; // OK

Class Base
{
Private:
Int age;
Protect:
Int num;
}
Class Devi: public Base
{
}
Base B; Devi d;
B. num = 3; // error
D. num = 3; // OK

When a user uses a member inherited from a base class, it depends not only on the access label (private, public, etc.) of the base class during definition, but also on
The method of inheriting the base class. At the same time, the inheritance class can only make the access permission smaller.
 
At the same time, pay attention to the differences between the inheritance of the partition classification interface and the implementation inheritance.
Interface inheritance, that is, the class user can call function members directly through "." or "->.
Implement inheritance: inherits the access permissions of data members during the definition of member functions of the class.
 
Note the differences between class members and class users.
Class Members include data members and function members. They are about class implementation.
A class user refers to a class object, which refers to the use of interfaces. Yes. The object uses "." to point to the object through "-> ".
[Cpp]


Public:
A (string & Name = "lihao", int Age = 21)
{}
Virtual void print () const;
Private:
String name;
Int age;
}
 
Void A: print () const
{
Cout <name <endl
<Age <endl;
}
 
Class B: public
{
Public:
B (string & Name, int Age, int Grade): A (Name, Age), grade (Grade)
{}
Private:
Int grade;
}
 
Void B: print () const
{
A: print ();
Cout <grade <endl;
}

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.