C++ part of the interview questions

The relationship between overloading and overriding in c++?

answer:

1. Overload

1. Overloading refers to a function with a different parameter list, but a function with the same function name. Overloading requires that the parameter list must be different, such as different types of parameters, different number of parameters, and different order of parameters.

2. If only the return value of the function is different, there is no way to overload, because overloading requires that the parameter list must be different .

 

2. Override (override override)

1. Overriding exists in the class, and the subclass overrides the function inherited from the base class. The function name, return value, and parameter list must be the same as the base class .

2. When the object of the subclass calls the member function, if the member function is overridden, the overridden version in the subclass is called, otherwise the function inherited from the base class is called.

3. If the subclass overrides the virtual function of the base class, it can be used to implement polymorphism.

    When the subclass redefines the virtual function of the base class, the base class pointer can dynamically call the virtual function in the subclass according to the different subclass pointers assigned to it, which can achieve dynamic binding, which is polymorphism.

4. Features of subclass overriding base class function

(1) The function name is the same, the parameters are the same, and the return value is the same

(2) If the base class function is a virtual function, the subclass overrides the virtual function to achieve polymorphism

 

3. The difference between overloading and overriding

1. Overloading requires the same function name, but the parameter list must be different, and the return value can be the same or different.

    Overriding requires that the function name, parameter list, and return value must be the same.

2. Overloading in a class is the relationship between different member functions in the same class

Overriding     in a class is the relationship between the different member functions between the subclass and the base class

3. The call of the overloaded function is based on the parameter list to decide which function to call

   The call of the overriding function is based on the object type to decide which one to call

4. Polymorphism cannot be achieved by overloading member functions in a class

Overriding     base class virtual functions in subclasses can achieve polymorphism

If the base class function is virtual, then subclass redefinition is overriding.

If the base class function is not a virtual function, then the subclass redefinition belongs to the hidden base class function.

 c. "Hidden" means that the function of the derived class shields the function of the base class with the same name. The rules are as follows:
(1) If the function of the derived class has the same name as the function of the base class, but 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).
(2) If the function of the derived class has the same name and the same parameters as the function of the base class, but the function of the base class does not have the virtual keyword. At this point, the functions of the base class are hidden (not to be confused with overriding).

 

Guess you like

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