Common concepts such as C ++ inheritance, virtual functions, pure virtual functions, polymorphism, overwriting, overloading, hiding, and referencing

Source: Internet
Author: User

Common concepts include inheritance, virtual functions, pure virtual functions, polymorphism, overwriting, overloading, and hiding.

1. Inheritance:

Inheritance is easy to understand. Pay attention to the calling sequence of constructor in inheritance. When constructing a subclass (derived class), you must first call the constructor of the parent class (base class.

2. virtual functions:

Virtual keywords

3. Pure virtual functions

Virtual void breath () = 0;

The pure virtual function allows the base class to have an operation name without operation content, so that the derived class can be defined when it is inherited. A class with pure virtual functions is called an abstract class. Objects cannot be declared for this type. It serves as a base class. In a derived class, pure virtual functions of the base class must be fully implemented. Otherwise, the derived class becomes an abstract class and the object cannot be instantiated.

4. Polymorphism:

Polymorphism is implemented using virtual function classes, rather than pure virtual functions.

Polymorphism is summarized in one sentence: add the virtual keyword before the function of the base class and re-write the function in the derived class. during runtime, the corresponding function will be called Based on the century type of the object. (Late binding or dynamic binding)

5. Coverage:

The condition for overwriting a function is:

1) basic functions must be virtual functions)

2) the two functions that are overwritten must be located in the derived class and the base class respectively;

3) The function name and parameter list must be identical.

6. Heavy Load:

Function parameters can be overloaded only when their types and numbers are different. The overload occurs in the same class.

7. Hide:

A derived class has a function with the same name as the base class (regardless of whether the parameter list is the same), so that the function with the same name of the base class is hidden in the derived class.

 

Hiding, overloading, and overwriting are easy to confuse. The simple difference is:

1) The overload occurs in the same class, and both hide and overwrite occur in the derived class and the base class;

2) In a derived class and a base class, if the two function names are identical to the parameter list, if the base class function uses virtual, It is overwritten. If the base class function does not use virtual, is hidden.

3) In the derived class and the base class, if the two function names are the same, the parameter list is different, no matter whether the base class function uses virtual or not, it is hidden.

 

8. Reference:

A reference is the alias of a variable and must be initialized during declaration. The referenced variables and variables used to initialize the referenced variables share the same memory.

 

Differences between reference and pointer variables:

1) The reference is an alias, which does not occupy the memory address space. It occupies the same memory as the variable for initialization;

2) the pointer is the address, which occupies storage space.

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.