Tag: Inheritance of the Color class inherits Enum www. CPP Char get access
Reference: http://www.weixueyuan.net/view/6359.html
Summarize:
The subclass inherits the parent class, which restricts access to the parent class's member property in the subclass, the subclass accesses the member of the parent class, and the access rights of its members, such as the private decorated member can only be used in the current class, and the subclass cannot access it.
At the same time, the members of the parent class are also members of the subclass, and their access rights in the subclass are determined by the access and inheritance methods in the parent class.
3) Private Inheritance method
- All public members in the base class are private properties in the derived class;
- All protected members in the base class are private properties in the derived class;
- All private members in the base class are inaccessible in the derived class.
Derived classes derive from the base class, and the derived class inherits from three ways of inheriting the base class: public, protected, and private. In unspecified cases, the compiler inherits the default way protected or private.
1) Public Inheritance method
- All public members in the base class are public in the derived class;
- All protected members in the base class are protected properties in the derived class;
- All private members in the base class are not accessible in the derived class.
For example, you can refer to Example 1 in the previous section, where you are no longer re-example.
2) Protected Succession mode
- All public members in the base class are protected properties in the derived class;
- All protected members in the base class are protected properties in the derived class;
- All private members in the base class are still inaccessible in the derived class.
Example 1:
enumLanguage{cpp, Java, Python,javascript, PHP, Ruby};classbook{ Public: voidSetprice (Doublea); DoubleGetPrice ()Const; voidSettitle (Char*a); Char* GetTitle ()Const; voiddisplay ();Private: DoublePrice ; Char*title;};classCodingbook:protectedbook{ Public : voidSetlang (language Lang); Language Getlang () {returnLang;}Private: Language lang;};
This class also uses the example in the previous section, when the inheritance mode is changed to protected inheritance, we will analyze the class members in the Codingbook attribute problem, see the following table.
Codingbook Class Members |
member Properties |
Source |
Price |
Not accessible |
Inherit from book Class |
Title |
Not accessible |
Inherit from book Class |
Lang |
Private |
Codingbook New |
Setprice |
Protected |
Inherit from book Class |
Settitle |
Protected |
Inherit from book Class |
GetPrice |
Protected |
Inherit from book Class |
GetTitle |
Protected |
Inherit from book Class |
Dispaly |
Protected |
Inherit from book Class |
Setlang |
Public |
Codingbook New |
Getlang |
Public |
Codingbook New |
3) Private Inheritance method
- All public members in the base class are private properties in the derived class;
- All protected members in the base class are private properties in the derived class;
- All private members in the base class are inaccessible in the derived class.
Example 2:
enumLanguage{cpp, Java, Python,javascript, PHP, Ruby};classbook{ Public: voidSetprice (Doublea); DoubleGetPrice ()Const; voidSettitle (Char*a); Char* GetTitle ()Const; voiddisplay ();Private: DoublePrice ; Char*title;};classCodingbook:Privatebook{ Public : voidSetlang (language Lang); Language Getlang () {returnLang;}Private: Language lang;};
Again, let's analyze the member properties of all members in the Codingbook class in this example, as shown in the following table.
Codingbook Class Members |
member Properties |
Source |
Price |
Not accessible |
Inherit from book Class |
Title |
Not accessible |
Inherit from book Class |
Lang |
Private |
Codingbook New |
Setprice |
Private |
Inherit from book Class |
Settitle |
Private |
Inherit from book Class |
GetPrice |
Private |
Inherit from book Class |
GetTitle |
Private |
Inherit from book Class |
Dispaly |
Private |
Inherit from book Class |
Setlang |
Public |
Codingbook New |
Getlang |
Public |
Codingbook New
|
3.2 C + + inheritance mode