Deep analysis of protected members in C + + derived classes inherit _c language

Source: Internet
Author: User
Tags access properties inheritance

Protected, like public and private, is used to declare the access rights of a member. The members declared by protected are called "protected members", or "protected members" for the short term. From the user's point of view of the class, the protected member is equivalent to a private member. However, unlike private members, a protected member can be referenced by a member function of a derived class.

If the base class declares private members, then any derived class cannot access them, and if you want to access them in a derived class, declare them as protected members. If a protected member is declared in a class, it means that the class might be used as a base class and that the members are accessed in its derived classes.

When defining a derived class, the inheritance of the base class is specified as protected, which is called protection inheritance, and a derived class established with protection inheritance is called a protected derived class (protected derived class), and its base class is called the protected base class (protected base Class), simply to protect the base class.

Protection inheritance is characterized by the protection of public and protected members of the base class from being protected in a derived class, and whose private members are still private to the base class. That is, the base class of the original public members are also protected, do not let the class outside the arbitrary access.

All members of the base class are protected in a derived class and cannot be accessed outside of the class, and their public and protected members can be accessed by the member functions of their derived classes.

All members of the base class are protected in a derived class and cannot be accessed outside of the class, and their public and protected members can be accessed by the member functions of their derived classes.

Comparing private inheritance and protection inheritance (that is, comparing access properties in private derived classes and in protecting derived classes), you can find that in a direct derived class, the two inheritance methods are actually the same: you cannot access any members outside of a class. In a derived class, you can access public members and protected members in the base class through member functions. However, if you continue to derive, in a new derived class, the two methods of inheritance will work differently.

For example, if a new derived class is derived from a public inheritance, the members in the original private base class become inaccessible members in the new derived class. Cannot be accessed either inside or outside of a derived class, and the public and protected members in the original protected base class are protected in the new derived class and can be accessed by the member functions of the new derived class.

It is to be remembered that the private members of the base class are inherited by derived classes (whether private, public, or protected) and become inaccessible members that are inaccessible to all members of the derived class. If you need to reference some members of a base class in a derived class, you should declare these members of the base class as protected rather than private.

If you are good at using protection members, you can find a combination of data sharing and member concealment in the hierarchy of classes. It can realize the concealment of some members, and can easily inherit, and can realize code reuse and expansion.

Through the above introduction, you can know the following points.

1 in a derived class, Members have 4 different access properties:
Common, and can be accessed from within derived classes and outside derived classes.
Protected, derived classes are accessible, and derived classes outside the derived class can be accessed.
Private, can be accessed within a derived class, and cannot be accessed outside of a derived class.
Inaccessible, not accessible within a derived class or outside of a derived class.

What needs to be stated is:
The access properties of the members listed here refer to the access properties obtained in the derived class.
Outside of a derived class, it refers to a module that establishes a derived class object, outside the scope of a derived class.
If this derived class continues to derive, the access properties obtained by the member are different under different inheritance methods, and in this table only the situation in the next layer of the common derived class is listed, and if it is a private inheritance or protection inheritance, you can find the answer from table 11.3.

2 The members of the class have different access attributes in different scopes, this is very clear. The access attribute of a member is conditional, depends on which scope it is in. Some readers ask: "A common member of a base class that becomes protected in a derived class is itself public or protected?" "It should be said that this is the different characteristics of the same member in different scopes." For example, the school Personnel department has the information of the school staff and students, the university's leadership can consult anyone's material, the school's department can only get the information from the department's faculty and students, and can not access the materials of any other department. If you want to ask: can access to a certain material, cannot generalize, must identify your identity, to determine whether the person's material can be You "access."

Before a derived class is introduced, the members of the class belong to only the class to which they belong, and no other classes are involved, which does not cause ambiguity. After you introduce a derived class, there is a problem: in which scope the characteristics of the members are discussed, and the same member has different characteristics in different inheritance levels. In order to illustrate this concept, you can make an analogy, the car driver's license is issued by the region, Beijing's driving license in the Beijing area unimpeded, if to the field, may be subject to certain restrictions, to foreign countries will be invalid. The same driver's rights in different areas are different. For example, visiting a patient in a hospital, if allowing you to visit the patient in close proximity and talk with the patient, you can understand the patients more deeply; If you only allow visitors outside the glass windows and doors and see the patient at a certain distance, you can only have a rough impression of the patient's condition. If only the footage of the patient's activity is allowed to be seen on television in the corridor of Ward, it is more indirect. People in different situations to the same patient, get different information, or that the patient in different situations of "visibility" different.

Normally, people often get used to saying how a certain type of public member is, which is not usually misleading. But never mistakenly assume that the member's Access attribute can only be public and cannot be changed. When discussing the access properties of a member, be sure to describe the scope, such as member a of the base class, where the Access property in the base class is public, and the Access property in the private derived class is private.

The following example shows how to access a protected member.

[Example] references a protected member in a derived class.

#include <iostream> #include <string> using namespace std;
  Class student//declares base class {public://base class common member void display (); protected://base class protects member int num;
  String name;
char sex;
};
  Defines a base class member function void Student::d isplay () {cout<< "num:" <<num<<endl;
  cout<< "Name:" <<name<<endl;
cout<< "Sex:" <<sex<<endl;} Class student1:protected Student//protected declaration of derived classes Student1 {public:void display1 ();//derived class public member function Private:int age;
Derived class private data member string addr;//derived class private data member}; void Student1::d isplay1 ()//defines a derived class public member function {cout<< "num:" <<num<<endl;//reference to the protection member of the base class, legal cout<< " Name: "<<name<<endl;//refers to the protection member of the base class, legal cout<<" sex: "<<sex<<endl;//reference to the protection member of the base class, legal cout< < age: <<age<<endl;//refers to a private member of a derived class, legally cout<< "address:" <<addr<<endl;
  Reference to private members of derived classes, legal} int main () {Student1 stud1;//stud1 is the object Stud1.display1 () of the derived class Student1 class; Display1 is a public member function in a derived class Stud1.num=10023;
Error, outside can not access Protection member return 0;
 }

It is legitimate to reference the protection members of a base class in a member function of a derived class. The protection members of the base class are inaccessible to the outside of the derived class (for example, NUM is a protected member in the base class student, and because the derived class is protected inheritance, it is still protected in the derived class. The outside world cannot use stud1.num to refer to it, but within a derived class it is the equivalent of a private member, which can be accessed by a member function of a derived class. As you can see, the difference between protecting a member and a private member is to extend the access scope of the protected member to a derived class.

Note: In your program, you can access the Protection member NUM, name, and sex of the base class by using the Student1 object of the derived class, stud1 the public member function display1, and do not mistake the protection member of the base class for the derived class object name (for example, the stud1.num is incorrect).

Private inheritance and protection inheritance methods need to be very careful when used, easy to make mistakes, generally not commonly used, the following examples of this tutorial mainly describes the common way of inheritance.

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.