C/C ++ notes (2)

Source: Internet
Author: User

Inheritance and derivation:

(1) The Child class does not need to be modified to extend the features of the parent class. We call it inheritance.

(2) the process of creating a new class and adding a new levy on the basis of the original is called "class Derivation ".

(3) Call the original Class "base class", "parent class", and the new class "derived class", or subclass.

Example: class son: Public father

(4) The public-derived members are still public members, the public-derived protected members are still protected members, and the public-derived Private Members cannot be derived members.

(5) private inheritance: class son: Private father

A subclass derived from the private method. The public and protected members of the parent class are private in the subclass, while private members are not accessible. Because private derivation is not conducive to continuing to derive, therefore, it is rarely used. Whether inherited in the form of only or private, the Private Members of the base class are not accessible in the derived class.

(6) single inheritance: Only one base class

Multi-inheritance: has multiple base classes (C # does not have multiple inheritance)

Example: class son: Public father, public mother

(7) subclass call sequence: Constructing parent class objects → constructing subclass objects → destructor parent class objects

When a class contains sub-objects, the calling sequence of the constructor is to call the sub-object constructor first (according to the sequence in which they appear when the class is defined, instead of following the order in the constructor list), and then call the constructor of the current object.CodeThe order in which the Destructor is called is exactly the same as that in the constructor.

 

Virtual functions:

(1) Before virtual is used, C ++ uses static concatenation for overloaded functions. After virtual is used, C ++ dynamically associates the function.

(2) Static Association editing refers to the compilation and connection phase. This kind of association is also called early Association editing, which solvesProgramThe relationship between calling and executing the operation code in. This kind of association during compilation is also called static bundle. during compilation, the relationship between operation calls in the program and code execution is solved. For static concatenation. The relationship between the call and the called has been determined. For static concatenation, because the relationship between the call and the call has been determined, the effects of the code during compilation and execution are the same.

(3) When the program is executed, it is called dynamic association, or dynamic bundle, or later Association.

(4) If pointer or reference is not used in virtual functions, dynamic association cannot be implemented.

(5) use the member name limitation (for example, a: Get) in the virtual function to forcibly remove Dynamic Association.

 

Virtual destructor:

(1) When a derived class object is created, it first calls the constructor of the base class and then calls the constructor of the class. Generally, when using a virtual function, we will pass the pointer of the derived object to the pointer to the base class, what happens if the pointer to the object of the derived class is deleted? If the Destructor is a virtual function, the correct operation is performed. It first calls the destructor of the derived class, generally, the destructor of any class can be declared as a virtual destructor. When the pointer is deleted, the system obtains the type of the object and calls the correct destructor.

(2) because the Destructor do not allow parameters, it is impossible to implement overload. Therefore, a class can only have one virtual destructor.

(3) As long as the destructor of the base class is described as a virtual destructor, The destructor of the derived class naturally become virtual destructor, regardless of whether they are described or not.

(4) In C ++, the virtual constructor does not exist and therefore cannot be declared.

(5) If a virtual function is defined in the base class, the Destructor should also be described as a virtual destructor, which makes the memory collection more accurate.

 

Function template (STL basics)

(1) Let's look at the following example: An Example of absolute value

IntABS (IntX)
{
ReturnX <0? -X: X;
}

DoubleABS (DoubleX)
{
ReturnX <0;-X: X;
}

In addition to different parameter types, these two functions have the same functions. In this case, functions with the same functions and different parameter types can be abstracted into a template. This is the function template.

The function template is defined in the form of template <typename identifier>. The keyword typename can also be replaced by the keyword class.

Therefore, the absolute values can be written as follows:

 
Template <typename T>
T ABS (t x)
{
ReturnX <0? -X: X;
}

Note: The compiler will export the parameter types of the function template when calling the function. Remember that the code of the parameter type has been pushed out during compilation, rather than being deduced at runtime.

The following is an example of a complete function template application:

Template <ClassT>
T ABS (t x)
{
ReturnX <0? -X: X;
}

Template <ClassT> T max (t tl, t T2)
{
Return(T1> T2 )? T1: T2;
}

IntMain ()
{
IntA =-1;
IntB =-2;
IntC

ABS ();
ABS (B)
C = max (A, B );

Return 0;
}

 

RelatedArticle:

• C/C ++ notes (1)

• C/C ++ notes (2)

 

 

 

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.