An analysis of access control examples in C + + inheritance _c language

Source: Internet
Author: User
Tags inheritance

In this paper, the access control in C + + inheritance is discussed in depth, which is very necessary to master C + + object-oriented programming. The specific contents are as follows:

Generally speaking, we think that a class has two different users: the ordinary user and the implementation of the class. In which, the ordinary user writes the code to use the class object, this part of code can only access the class's public (interface) member, the implementation person is responsible for writing class's member and the friend's code, the member and the friend can access both the class's public part, also can access the class private part. If you consider inheritance further, a third user, the derived class, appears. Derived classes can access both public and protected (protected) members of the base class, but they cannot access the private (private) members of the base class.

Inheritance related points are as follows:

①. Most classes inherit from only one class, and this form of inheritance is called "Single inheritance." This article is mainly about the single inheritance.
②. In an object of a derived class, contains parts that are inherited from the base class and that are customized by derived classes. Because a derived class contains a base class part, you can make a type conversion from a derived class to a base class, which is implicit.
③. There is no implicit type conversion from the base class to the derived class.
④. The automatic type conversion of a derived class to a base class is valid only for pointers or references, and no type conversions exist between objects.
⑤. If the base class defines a static member, there is only one instance of each static member, regardless of how many derived classes are derived.
⑥ prevents a class from being inherited and can use the keyword final, which is provided in the c++11 new standard.

In addition, readers need to understand the inheritance of virtual functions and pure virtual functions described in the previous article.

I. Public, private and protected members

1. Access descriptor

Control members are accessible to ordinary users or derived classes in C + + by using the Access descriptor public, protected, and private to control the members of the class:

Public: Members that are defined as public are accessible to ordinary users, to the implementation of classes, and to derived classes. Public is typically used to define the external interface of a class.

protected: The purpose of defining protected members is to make derived classes accessible and to prevent other users from accessing them. So the implementation of the class and derived classes can be accessed, while ordinary users cannot access them.

Private: a member defined as private can only be accessed by the implementing person (members and friends) of the class. The private part is typically used to encapsulate (that is, hide) the implementation details of a class.

Class people{ 
protected: 
  string name; 
 
Class Student:public people{public 
: 
  friend void Print (Student &s); 
  friend void Print (People &p); 
 
Correct, the protected member 
void Print (Student &s) {s.name= "Songlee" of the base class can be accessed through derived class objects; cout<< s.name << Endl;} 
///error, the protected member 
void Print (People &p) {p.name= "Songlee" of the base class cannot be accessed through the base class object; cout<< p.name << Endl } 

It is to be noted that a member or friend of a derived class can only access a protected member of a base class through a derived class object. A derived class does not have any access privileges to a protected member in a base class object.

2. Changing the accessibility of a member

Sometimes we need to change the access level of a name inherited by a derived class by using a use declaration:

Class people{ 
protected: 
  string name; 
 
Class Student:public people{public 
: 
  using People::name;//Change the access rights of the inherited name member to public 
}; 
 
int main () 
{ 
  Student me; 
  Me.Name = "Songlee";   You can access the name 
  cout << me.name << Endl;  
  return 0; 
} 

By using a using declaration statement inside a class, we can mark any accessible member (non-private member) in the direct or indirect base class of the class, changing its access rights.

Ii. public, private and protected inheritance

We note that the Access descriptor public, protected, and private are used in the derived list of classes to represent different inheritance modes:

Class A:public B {/* */};   Public inheritance 
class A:private B {/* * */};  Private inheritance 
class a:protected B {/*/};//protected inheritance 

access specifiers in derived lists of derived classes have little effect on whether members of derived classes (and friends) can access members of their direct base classes . The members (and friends) of a derived class have access to the base class members only in relation to the access descriptor in the base class.

So what does an access specifier in a derived list do?

The access specifier in a derived list controls the access of the derived class user to the base class member, and the user of the derived class. The following gives a change in the access rights caused by different inheritance methods:

Public Inheritance : If the inheritance is publicly owned, the member follows its original access descriptor. The public, protected, and private properties in the parent class do not change in subclasses.

protected inheritance : access rights that are higher than protected levels become protected. That is, the public property in the parent class becomes protected in the subclass, and the protected and private properties in the parent class are unchanged in the subclass.

Private Inheritance : The access rights that are higher than the private level become private. That is, three access attributes in the parent class become private in subclasses.

Class A {  //base class public 
: 
  string a_public;   Public member 
protected: 
  string a_protected;  protected member 
}; 
 
Class B:private A {  //private inheritance Public 
: 
  B () {a_public= ' public '; a_protected= "protected"; }; 
}; 
 
int main () 
{ 
  b b;  Access 
  cout << b.a_public << "<< b.a_protected << Endl via object B;  Error because it is private inheritance return 
  0; 
}

If we do not use the access descriptor in the derived list, the struct keyword defaults to public inheritance, and the class keyword defaults to private inheritance. It is advisable, however, to explicitly write an access descriptor when inheriting.

In addition, different inheritance methods affect the conversion of derived classes to the base class , assuming that derive inherits from base:

1. User code can use a derived class to convert to a base class only if the derive is publicly inherited from base, and the conversion cannot be used by user code if the way derive inherits base is protected or private.

2. Regardless of how derive inherits Base,derive's member functions and friends, you can use derived classes to convert to a base class, and the type conversions of a derived class to its direct base class are always accessible to members and friends of derived classes.

3. If the way derive inherits base is public or protected, the members and friends of the derived class of derive can convert to the type of base by derive, and conversely, if derive inherits base in a private way, it cannot be used.

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.