3.2 C + + inheritance mode

Source: Internet
Author: User

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

Related Article

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.