What are the differences between overload, override (also called "override") and redefine (also called "hidden") in C++?

In C++, multiple definitions are allowed for a function and operator in the same scope, which are called function overloading and operator overloading respectively.

An overloaded declaration is a declaration with the same name as a function or method that has been previously declared in the scope, but their parameter list and definition (implementation) are different.

When an overloaded function or operator is called, the compiler determines the most appropriate definition by comparing the parameter types used with the parameter types in the definition. The process of selecting the most appropriate overloaded function or operator is called overload decision-making.

The importance of overloading to C++ programming is self-evident. However, interviewers often ask not only about overloading, but also about rewriting and redefinition. This leaves many interviewers feeling confused.

So today we are going to talk about the difference between overloading, rewriting and redefinition.

1. Concept review

(1) Overloading

Overloading is divided into two types, one is function overloading and the other is operator overloading.

Generally refers to function overloading, that is, several functions with the same name (that is, multiple functions have the same name) with different parameter lists (different parameter types, numbers, and orders) declared in the same scope, but the number of parameters is the same. The types are different (the number and type are the same, but a different order is OK), and then the parameter list determines which function to call.
Features:
①The main consideration here is object-oriented. In object-oriented, overloading only exists inside the class, that is, in the same class. If you abandon object-oriented, just be in the same scope.
②The function name is the same but the parameter list is different.
③Overloading does not care about the function return type and cannot be judged by the return type.

class A
{
    
    
	public:
  		void test(int i);
  		void test(double i);			//overload
  		void test(int i, double j);		//overload
  		void test(double i, int j);		//overload
};

(2) Rewriting (also called "overwriting")

Indicates that there is a redefined base class function in the derived class.
Features:
① Its function name, parameter list, and return value type must all be consistent with the overridden function in the base class, only the function body is different (inside the curly braces). ​②When
a derived class calls, the overridden function of the derived class will be called, and the overridden function will not be called.
③The overridden function in the overridden base class must be modified by virtual.

#include<iostream>
using namespace std;

//基类
class Base
{
    
    
public:
    virtual void fun(int i){
    
     
        cout << "Base::fun(int) : " << i << endl;
    }
};

//派生类
class Derived : public Base
{
    
    
public:
    virtual void fun(int i){
    
     
        cout << "Derived::fun(int) : " << i << endl;
    }
};

int main()
{
    
    
    Base b;
    Base * pb = new Derived();
    pb->fun(3);				//Derived::fun(int)

    system("pause");
    return 0;

}

(3) Redefinition (also called "hiding")

Indicates that a derived class function blocks a base class function with the same name.
Features:
①The function names are the same, but the return values ​​can be different.
②The scope is located in the base class and the derived class respectively;
Note:
①If the function of the derived class and the function of the base class have the same name, but the parameters are different, at this time, regardless of whether there is a virtual or not, the function of the base class is hidden.
② If the function of the derived class has the same name as the function of the base class, and the parameters are also the same, but the base class function does not have the visual keyword, then the function of the base class is hidden.
Therefore, here only the function with the same name is required. Regardless of whether the parameter list is the same, the base class function will be hidden.

class A
{
    
    
public:
	void fun()
	{
    
    
		cout<<"A"<<endl;
	}
    
    virtual void fun1(int i,int j)
 	{
    
    
 		cout << "A::fun() : " <<i<<" "<<j<<endl;
 	}
};

class B:public A
{
    
    
public:
    //两个函数参数相同,但基类函数不是虚函数。
	void fun() //隐藏父类的 fun 函数
	{
    
    
		cout<<"B"<<endl;
	}
    
    //两个函数参数不同,无论基类函数是否是虚函数,基类函数都会被屏蔽。
    virtual void fun1(double i) //重写基类A中的fun1()
	{
    
    
		cout << "B::fun(): " << i << endl;
	}
}

int main()
{
    
    
 B b;
 b.fun3(5);
 //b.fun3(1, 2);隐藏了基类的同名函数,所以不能运行
 return 0;
}

2. Difference comparison

(1) The difference between overloading and rewriting:

​①The difference in scope: one is in the same class, the other is in a different class (object-oriented).

That is, overloading means that the function name is the same but the parameter list is different. Overloading only exists inside the class; overriding means that the subclass redefines the virtual function with the same name and parameters in the parent class. The function characteristics are the same, but the specific implementation is different. The main reason is that the function name is the same and the parameter list is different. It appears in the inheritance relationship. Therefore, the overloaded and overloaded functions are in the same class; the overridden and overridden functions are in different classes, that is, in the base class and the derived class respectively.

​②Difference in parameters: One parameter list must be different, and the other parameter list must be the same.

That is, the overloaded and overloaded function parameter lists must be different, and the overriding and rewritten function parameter lists must be the same.

​③The difference between virtual: One can have virtual modification or not; the other must have virtual modification.

That is, the overloaded function and the overloaded function can be modified by virtual or not. The overridden base class must be modified by virtual.
(2) The difference between overloading, hidden rewriting and:

​① Scope difference: The scope of overloading is in the same class; the overriding function and the overridden function, the hidden function and the hidden function are all in different classes. ​ ② The difference in parameters: The parameter list of the hidden function and the hidden function can be the same or different, but the function name must be the same; when the parameters are different, the base class function will be hidden regardless of whether the function in the base class is virtual modified or not. , rather than being rewritten.

Summarize:

Therefore, if you want to distinguish these three concepts, you can pay attention to the essential differences between their concepts, and then conduct a detailed comparison of the differences in terms of scope, parameters, etc., to complete a summary of the differences.

Guess you like

Origin blog.csdn.net/qq_52302919/article/details/132307340