C ++ object-oriented programming notes (2)

Source: Internet
Author: User

C ++ Object-Oriented Programming (2)

I. Heavy Load and coverage

Features of member functions being overloaded:

(1) the same range (in the same class );

(2) The function name is the same;

(3) parameters are different;

(4) virtual
Keyword is optional.

Override refers to the function of a derived class that overwrites the base class function. The features are as follows:

(1) different scopes (located in the derived class and the base class respectively );

(2) The function name is the same;

(3) The parameters are the same;

(4) basic functions must have virtual
Keyword.

Ii. Hide rules

(1) If the function of the derived class has the same name as the function of the base class, but the parameter is different. In this case, functions of the base class will be hidden regardless of whether there is any virtual keyword (Be sure not to confuse them with overload ).

 

(2) If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual

Keyword. In this case, the function of the base class is hidden (do not confuse with overwrite ).

Iii. Scope of classes under Inheritance

1. As described above, a derived class member with the same name as a base class member will be blocked from direct access to the base class member. However, the scope operator can access the blocked base class members, for example, return base: men;

2. As described above, the functions declared in the local scope do not reload the functions defined in the global scope. Similarly, the functions defined in the derived class do not reload the Members defined in the base class, the base class function is considered only when the derived class does not define this function.

3. Determine the four steps for function calling at the inheritance level

(1) first determine the static type of the object, reference, or pointer for function call;

(2) Search for functions in the class. If the function cannot be found, search for the function in the base class directly. In this way, search for the function based on the inheritance chain of the class, wait until you find the function or find the last class. If the name cannot be found in the class or its related base class, an error occurs;

(3) If this parameter is found, the regular type check is performed to check whether the function call is valid if the defined parameter is found.

(4) assuming that the function call is valid, the compiler generates the code. If a function is a virtual function and is called through a pointer, the compiler generates code to determine which function version to run based on the object's dynamic type. If no yeast is used, the compiler generates code to directly call the function.

4. You cannot save the base class object by saving the derived class object in the definition window. There is no standard conversion from the base class type to the derived type. The only feasible option may be to use a container to save the object pointer.

5. More Strict inheritance rules should be: If logically B
Is a "one" of a, and
All functions

If both the ability and attribute make sense to B, B is allowed.
Inherits the functions and attributes of.

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.