C++ function overloading, function redefinition, and function rewriting

In the study of C++, it is certainly inevitable to discriminate some concepts, but often times, they are puzzled. The key to the question is whether you have noticed some details.

One, function overload (overload)

What is function overloading?
In fact, we can think of this as a mechanism of the compiler. It will automatically match the function you call according to the relevant characteristics of the function, and automatically select the matching function, so that this automatic matching selection will call the corresponding function The mechanism, we can call it function overloading.
How can it constitute a function overload?

Function overloading refers to declaring multiple functions with the same name but different parameter lists in a class. These parameters may have different numbers, orders, and types, but they cannot be judged by the return type. The characteristics are:
(1) the same scope (in the same scope);
(2) the same function name;
(3) different parameters;
(4) virtual keyword is optional (note: function overloading and whether virtual modification has nothing to do);
Note: Function overloading has nothing to do with the return value type

Below, I will explain the function overloading in the form of code: (I did not use the class to explain here, just use the definition of simple functions to explain)

void sum(int a, int b)
{
    
    
	cout << "调用了参数类型int,int函数" << endl;
	cout << a + b << endl;
}

void sum(int a, int b, int c)
{
    
    
	cout << "调用了参数类型int,int,int函数" << endl;
	cout << a + b + c << endl;
}

void sum(char a, char b)
{
    
    
	cout << "调用了参数类型char,char函数" << endl;
	cout << (int)a + (int)b << endl;
}

void sum(char a, int b)
{
    
    
	cout << "调用了参数类型char,int函数" << endl;
	cout << (int)a + b << endl;
}
void sum(int a, char b)
{
    
    
	cout << "调用了参数类型int,char函数" << endl;
	cout << a + (int)b << endl;
}
#include<iostream>
using namespace std;
//程序入口
int main()
{
    
    
	sum(3, 4);
	sum(3, 4, 5);
	sum('a', 'b');
	sum('a', 3);
	sum(3, 'a');
	return 0;
}

The results of the operation are shown in the figure:
Insert picture description here
The results of the operation are obvious. You can observe that the function I called obviously has the same name, but because the number, type, and order of the actual parameters passed in are different, the functions it calls are different. It is overloading. The system will automatically match according to the type, number, and order of the actual parameters you pass in, and call the corresponding function. If there is no matching function, an error will be reported.

Second, redefine (also known as hiding)

==Note:== I am not referring to VS compilation, the error content is redefinition, this redefinition is not another redefinition, the redefinition here refers to the rewriting of the function. Here I have cited the concept of classes. If you don’t know enough about the concept of classes, you can transfer to other bloggers to understand the concept of classes first, and then distinguish between redefinition and rewriting.
What is the function redefinition?
(1) Not in the same scope (in the derived class and base class respectively);
(2) The function name is the same;
(3) The return value can be different;
(4) The parameters are different. At this time, regardless of the virtual keyword, the function of the base class will be hidden (be careful not to be confused with overloading and overwriting);
(5) The parameters are the same, but the base class function does not have the virtual keyword. At this time, the function of the base class is hidden (be careful not to confuse it with coverage);

#include<iostream>
using namespace std;
//基类
class Base
{
    
    
public:
	void Show()//无参
	{
    
    
		cout << "调用了基类Base类Show方法" << endl;
	}
private:

};
//派生类CA继承于Base类
class CA:public Base
{
    
    
public:
	void Show()//无参
	{
    
    
		cout << "调用了CA类Show方法" << endl;
	}

private:

};
//派生类CB集成与Base类
class CB:public Base
{
    
    
public:
	void Show(int i)//含参
	{
    
    
		cout << "调用了CB类Show方法" << endl;
	}
private:

};

//程序入口
int main()
{
    
    

	Base base;					//创建一个Base类对象
	base.Show();				//调用Show方法
	CA ca;						//创建一个Base类对象
	ca.Show();					//调用Show方法
	CB cb;						//创建一个Base类对象
	cb.Show(1);					//调用Show方法
	return 0;
}

Operation result: As
Insert picture description here
you can see, the Show() method exists in the original base class Base class.
Assuming that I did not define the Show method in the derived CA class, the class object ca must call the member method of the base class. For specific implementation, you can directly comment out the Show method in the CA class and run it, I will not implement it here. Up.
But here, I redefine the Show method in the derived class CA, so the Show method of the base class is hidden. Then the class object ca will call the Show method that it has rewritten, so the class object ca calls CA. Show method of the class.
Secondly, the CB class also rewrites the Show method, so the base class Show method is also hidden. At this time, the CB class object cb cannot call the Show method without parameters, and cb can only call the Show( int i) method.
This is the function redefinition

Three, rewrite (also known as override)

Function rewriting means that the subclass redefines the virtual function of the base class. Features are:

(1) Not in the same scope (respectively in the derived class and base class);
(2) The function name is the same;
(3) The parameters are the same;
(4) The base class function must have the virtual keyword, not static.
(5) The return value is the same, otherwise an error will be reported;
(6) The access modifier of the rewritten function can be different;

The difference between overload and coverage:

(1) Override is the relationship between the subclass and the parent class, which is a vertical relationship; overloading is the relationship between different methods in the same class, which is a horizontal relationship;

(2) Override requires the same parameter list, and overload requires different parameter lists; override requires the same return type, but overload does not require;

(3) In the coverage relationship, the calling method body is determined according to the type of the object (base class type or derived class type), and the overloading relationship is based on the actual parameter list and formal parameter list at the time of calling to select the method body.

#include<iostream>
using namespace std;
//基类
class Base
{
    
    
public:
	virtual void Show(int a,int b)
	{
    
    
		cout << "调用了基类Base类Show方法并输出了两位数:"<<a<<","<<b << endl;
	}
private:

};
//派生类
class CA:public Base
{
    
    
public:
	void Show(int a, int b)
	{
    
    
		cout << "调用了基类CA类Show方法并输出了两位数:" << a << "," << b << endl;
	}

private:

};

//程序入口
int main()
{
    
    

	Base base;					//创建一个Base类对象
	base.Show(3,4);				//调用Show方法
	CA ca;						//创建一个Base类对象
	ca.Show(5,6);					//调用Show方法
	
	return 0;
}

operation result:
Insert picture description here
Note: In the coverage relationship, the calling method body is determined according to the type of the object (base class type or derived class type), and the overloading relationship is based on the actual parameter list and formal parameter list at the time of calling to select the method body.
In other words: when calling a function, which function is called depends on the type of the class object. If the type of the object of this class is a base class, call the member function of the base class. If the type of the object of this class is a derived class, and the derived class overwrites the member function of the base class, then the class object of the derived class is Will call the member function of the same name after rewriting.

Regarding the learning of C++, welcome everyone to follow me, comment on my articles, like, bookmark, follow me not to get lost. Netizens are also welcome to discuss C++ dry goods with me. The knowledge of C++ is much more than that, we will analyze and explain one by one. If netizens want me to explain, they can comment and leave a message, and I will do my best to explain it to everyone.

: : ProMer_Wang

Link: https://blog.csdn.net/qq_43801020/article/details/106851972

This article is the original article of ProMer_Wang, the copyright belongs to the author, please indicate the source of the original text for reprinting, welcome to reprint!

Guess you like

Origin blog.csdn.net/qq_43801020/article/details/106851972