Inherit _ Derivation

Source: Internet
Author: User

Inherited:
1. Subclasses have all member variables (attributes) and member functions (methods) of the parent class
2, subclass is a special kind of parent class
3. Subclass objects can be used when the parent object uses the
4. Subclasses can have methods and properties that the parent class does not have

The way of inheritance is generally inherited according to public;

Access (properties and methods) control for a single class :
Public: Within class, available outside of class
Private: In-class
Protected: Inside Class, inherited subclass

Access control for derived classes:
Inheritance mode: Public inheritance, subclass access properties of the parent class are not changed
Protected: The parent class public becomes a protected inside the subclass
Private: All converted to the private property within the subclass

Assignment Compatibility principle:
1. Parent class pointer or reference to parent class or subclass (Polymorphic)
2, you can initialize the parent class with a subclass (subclass is a special kind of parent class)

Call rules for constructor destructors in inheritance:
Construction: After the late father class sub-class
Destructor: first subclass stepfather Class
Initializes the list with a constructor in the subclass, initializes the members of the parent class, and initializes its own members. The inverse of the destructor.
Child (int A, int b, int c):p arent (A, B)
{
This->c = C;
}

The blending of inheritance and composition (the object in which the parent class is defined in the subclass), constructing the destructor call method
The subclass constructor requires not only initializing its own member variables but also initializing the member variables of the parent class.
At this point, you need to initialize the list with the constructor to invoke the constructor of the parent class to initialize the member variables of the parent class

Constructor initialization list: combining, using in inheritance
Composition: An object in a subclass that has a parent class
Inheritance: Initializing a parent class member in a subclass constructor

Call the parent class constructor first, and if the parent class has a parent class (ancestor),
First, start with the ancestors, and then call down, until the child class, first call the subclass of the composite object constructor and then call the subclass itself constructor.
The order of destruction is reversed.

Methods for handling member variables with the same name in inheritance:
1, child class member variable with the same name as the parent class member variable, the subclass still inherits the member variable with the same name from the parent class
2. Distinguish a member variable with the same name from a subclass and parent class by using a domain action (the domain action of the parent class is used in the subclass)
3. Members of the same name are stored in different locations in memory.
child.b = 1; By default, subclasses use their own b
CHILD.PARENT::B = 2; Subclasses call the parent Class B (using the parent class's domain-action character)

The static keyword in a derived class
Static members in the parent class that satisfy the access control of the derived class
Static member variable out-of-class definition initialization, reminding the compiler to allocate memory

class is not declared and is private by default. constructors are declared as General public (except for singleton)(public is default in structs)

inheritance is ambiguity: A derives B and C two classes, and D inherits from B and C,d when the object in call A is called by B or by C.
To solve this problem, virtual inheritance is required.
Class B:vitual class A
{};
Class C:vitual class A
{};
Class D:public B, public C
{};
However, when both B and C have member variable K, the object of D cannot directly access K, and virtual inheritance cannot be resolved, but it can be accessed using the parent class scope.

Inherit _ Derivation

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.