The interview encounters the difference between rewriting, overloading, and redefining C++

During the interview, I was asked:

1. Overload

Refers to the same function name, but its parameter list number or order, different types. But it can't be judged by the return type.
(1) The same scope (in the same scope);
(2) The function name is the same;
(3) The parameters are different;
(4) The virtual keyword is optional.

(5) The return value can be different;

Summary: Overloading function parameters

2. Overriding (also known as overriding)
refers to the virtual function that the derived class redefines the base class. The characteristics are:
(1) Not in the same scope (located in the derived class and the base class respectively);
(2) The function names are the same ;
(3) The parameters are the same;
(4) The base class function must have the virtual keyword and cannot have static.
(5) The return value is the same (or covariant), otherwise an error will be reported; <—I only know the concept of covariance for the first time...

(6) The access modifier of the overridden function can be different. Although virtual is private, it is also possible to rewrite it to public and protected in derived classes

Summary: Overriding Virtual Functions

3. Redefinition (also hidden)
(1) Not in the same scope (in the derived class and the base class respectively);
(2) The function name is the same;
(3) The return value can be different;
(4) The parameters are different. At this point, with or without the virtual keyword, the functions of the base class will be hidden (not to be confused with overloading and overriding).

(5) The parameters are the same, but the base class function does not have the virtual keyword. At this point, the functions of the base class are hidden (not to be confused with overriding).

Summary: Redefining Non-Virtual Functions

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324736255&siteId=291194637