Review of C + + summary of the eight--inherit one

Source: Internet
Author: User
Tags access properties
A different inheritance method changes the access properties of an inherited member

Member variables, methods that are modified by public are available both inside and outside the class.

Protected modified member variables, methods are used inside the class, used in inherited subclasses, and cannot be used outside the class. (To be used in the family, in order to get the inheritance!) )

Private decorated member variables, methods can only be used inside the class, cannot be used outside of the class

1) inheritance in C + + affects sub-class's external access properties

Public Inheritance: Parent class members maintain the original access level in subclasses

Private Inheritance: Parent class member becomes private member in child class

Protected inheritance: Public members in the parent class become protected

The protected member in the parent class is still protected

Private member in parent class is still private

2) The private member still exists in the subclass, but it cannot be accessed. Regardless of the way the base class is inherited, derived classes cannot directly use the private members of the base class.

3) C + + Neutron class external access attribute table

Parent class member Access level

How to Inherit

Public (inheriting mode)    proteced (inheritance mode)    private (inheriting way) public public   proteced  private    proteced   proteced   proteced   private   private   private private private

Three look at the principle:

The inheritance in C + + (public, private, protected) affects the sub-class's external access properties

Judge a sentence, can be accessed

1) Look at the call statement, which is written in the inner and outer of subclasses

2) See how subclasses Inherit from parent class (public, private, protected)

3) Look at the access level in the parent class (public, private, protected)

Principle of access level setting for class member of derived class

Thinking: How do I properly use public,protected and private to declare access levels for members?

1. Members who need to be accessed by outsiders are set directly to public

2. Members that can only be accessed in the current class are set to private

3. The access rights of members that can only be accessed in the current class and subclass are set to protected,protected members, between public and private.

Two-type compatibility principles

A type-compatible rule is any place where a base class object is needed, and can be overridden with objects of a public derived class. With public inheritance, derived classes get all the members in the base class except constructors, destructors. In this way, the public derived class actually has all the functions of the base class, all the problems that the base class can solve, the public derived class can be solved. The overrides referred to in the Type compatibility rule include the following:

A subclass object can be used as a parent class object

Subclass objects can be assigned directly to the parent class object

Subclass objects can initialize parent class objects directly

The parent pointer can point directly to the child class object

The parent class reference can refer directly to the Child class object

After the override, the derived class object can be used as an object of the base class, but only members that inherit from the base class are used.

Type compatibility rules are one of the important foundations of polymorphism.

First layer Meaning:
1-1 base class pointer (reference) to child class object

Parent *p = Null;p = &C1;P->PRINTP (); 1-2 pointers do function parameters Howtoprint (&P1); Howtoprint (&C1); 1-3 references do function parameters HowToprint2 (p1); HowToprint2 (C1);

Second level meaning

You can have the parent class object, initialize the child class object

Parent p3 = C1;

Object model in three-inheritance

Question: How do I initialize a parent class member? What does the parent class have to do with the constructor of the child class?

When a subclass object is constructed, it is necessary to call the parent class constructor to initialize its inherited members.

When a subclass object is refactored, it is necessary to call the parent class destructor to clean up the members it inherits from

The construction destructor invocation principle in inheritance (when initializing from top to bottom, father and son first)

1. When the subclass object is created, the constructor of the parent class is called first

2. After the parent class constructor finishes executing, the constructor of the subclass is executed

3. When the constructor of the parent class has parameters, the call must be displayed in the initialization list of the subclass

4. The sequence of destructor calls is the opposite of the constructor function

The above is the C + + review key summary of eight--inherit one content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.