【C++】Function overloading① (Concept of function overloading | Judgment criteria for function overloading - number/type/order of parameters | Return value is not a criterion for judging function overloading)


Blog summary:

  • Overloaded functions: use the same function name to define different function parameter lists;
  • Judgment criteria: Only the difference in the number/type/order of function parameters is the criterion for "function overloading", and the return value of the function is not the criterion for "function overloading";




1. Function overloading




1. The concept of function overloading


The concept of "function overloading" in C++:

  • Use the same function name, define different functions;
  • The function name is the same, but the parameter order or parameter type of the parameter list is different;

Note: Only function parameters are the criteria for judging "function overloading", and the return value of the function is not the criteria for judging "function overloading";


2. Judgment criteria for function overloading


"Function overloading" satisfies the conditions (judgment criteria):

  • The parameter "number" is different
  • Parameters "type" are different
  • The parameter "order" is different

Only the function parameter is the judgment standard of "function overloading", and the return value of the function is not the judgment standard of "function overloading";





2. Code example




1. Code example - function overloading


In the following code, three fun functions are defined, all of which are overloaded functions;

The first fun function receives an int integer parameter;

// 函数1 : 接收一个整数参数  
void fun(int i) {
    
    
	cout << "打印整数: " << i << endl;
}

The second fun function receives 2 int type parameters; this overloaded function has a different number of parameters from the first function;

// 函数2 : 接收两个整数参数  
void fun(int i, int j) {
    
    
	cout << "打印两个整数: " << i << " 和 " << j << endl;
}

The third fun function receives a float floating-point type parameter. This overloaded function is different from the first function in parameter types, and different from the second function in both parameter types and numbers;

// 函数3 : 接收一个浮点数参数  
void fun(float f) {
    
    
	cout << "打印浮点数: " << f <<endl;
}

When calling a function, determine which overloaded function is called according to the parameters passed in;

Pass in the actual parameter 4, the parameter type is an int type, and the first overloaded function is called;

fun(4);				// 调用第一个 fun 函数  

Pass in actual parameters 2, 3, the parameter type is 2 int types, and the second overloaded function is called;

fun(2, 3);			// 调用第二个 fun 函数  

Pass in the actual parameter 1.5f, the parameter type is a float type, and the third overloaded function is called;

fun(1.5f);          // 调用第三个 fun 函数  

Code example:

// 包含 C++ 头文件
#include "iostream"

// 使用 std 标准命名空间
//		该命名空间中 , 定义了很多标准定义
using namespace std;

// 函数重载示例  

// 函数1 : 接收一个整数参数  
void fun(int i) {
    
    
	cout << "打印整数: " << i << endl;
}

// 函数2 : 接收两个整数参数  
void fun(int i, int j) {
    
    
	cout << "打印两个整数: " << i << " 和 " << j << endl;
}

// 函数3 : 接收一个浮点数参数  
void fun(float f) {
    
    
	cout << "打印浮点数: " << f <<endl;
}

int main()
{
    
    
	fun(4);				// 调用第一个 fun 函数  
	fun(2, 3);			// 调用第二个 fun 函数  
	fun(1.5f);          // 调用第三个 fun 函数  


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
    return 0;
}

Results of the :

打印整数: 4
打印两个整数: 23
打印浮点数: 1.5
Press any key to continue . . .

insert image description here


2. Code example - the return value is not a criterion for judging function overloading


Only the function parameter is the judgment standard of "function overloading", and the return value of the function is not the judgment standard of "function overloading";

Therefore, if two functions have the same parameter list but different return values, an error will be reported at compile time, and the program cannot run;


Function 1 is defined, void fun(int i), which receives an integer int type parameter and returns void return value;

// 函数1 : 接收一个整数参数 , 返回 void 空
void fun(int i) {
    
    
	cout << "打印整数: " << i << endl;
}

After that, define function 2, int fun(int i), which receives an integer parameter and returns an int type return value. Function 2 directly conflicts with function 1, and an error will be reported when compiling;

// 函数2 : 接收一个整数参数 , 返回 int 类型返回值 
int fun(int i) {
    
    
	cout << "打印整数: " << i << endl;
	return 0;
}

If you use IDE tools, such as Visual Studio 2019, an error will be reported after the code is written;

insert image description here


Error code example:

// 包含 C++ 头文件
#include "iostream"

// 使用 std 标准命名空间
//		该命名空间中 , 定义了很多标准定义
using namespace std;

// 函数重载示例  

// 函数1 : 接收一个整数参数 , 返回 void 空
void fun(int i) {
    
    
	cout << "打印整数: " << i << endl;
}

// 函数2 : 接收一个整数参数 , 返回 int 类型返回值 
int fun(int i) {
    
    
	cout << "打印整数: " << i << endl;
	return 0;
}

int main()
{
    
    


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
    return 0;
}

Results of the :

已启动生成…
1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1>hello_world.cpp
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(16,16): error C2556:int fun(int): 重载函数与“void fun(int)”只是在返回类型上不同
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(11): message : 参见“fun”的声明
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(16,5): error C2371: “fun”: 重定义;不同的基类型
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(11): message : 参见“fun”的声明
1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0==========

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/132698434