C++函数重载完全攻略(无师自通)
例如,有一个使用 double 形参的 square 函数。但是我们还需要一个 square 函数,该函数专门用于整数,并接收一个 int 作为其实参。这两个函数都会做同样的事情:返回其实参的平方值,唯一的区别是参与操作的数据类型。如果在同一程序中使用这两个函数,则可以为每个函数分配唯一的名称。例如,对于使用 int 的函数可以命名为 squarelnt,对于使用 double 的函数可以命名为 squareDouble。
但是,C++ 允许函数重载,这意味着可以为多个函数分配相同的名称,只要它们的形参列表不同即可。下面的程序演示了该功能:
#include iostream #include iomanip using namespace std; // Function prototypes int square(int); double square(double); int main() int userInt; double userReal; // Get an int and a double cout Enter an integer and a floating-point value: cin userInt userReal; // Display their squares cout Here are their squares: cout fixed showpoint setprecision(2); cout square(userInt) and square(userReal) endl; return 0; int square (int number) return number * number; double square(double number) return number * number; }
程序输出结果:
Enter an integer and a floating-point value: 12 4.2
Here are their squares: 144 and 17.64
以下是程序中使用的 square 函数的函数头:
int square(int number)
double square(double number)
在 C++ 中,每个函数都有一个签名。函数签名是函数的名称和函数形参的数据类型顺序。因此,上面程序中的 square 函数具有以下签名:
square(int)
square(double)
当调用重载函数时,C++ 将使用函数签名将其与具有相同名称的其他函数区分开来。在上面程序中,当 int 实参被传递给 square 时,调用具有 int 形参的函数版本。同样,当 double 实参被传递给 square 时,将调用带有 double 形参的版本。
请注意,函数的返回值不是签名的一部分。以下函数无法在同一程序中使用,因为它们的形参列表并无差异。
int square(int number) return number * number; double square (int number) //错误!形参列表必须不同 return number * number; }
当有类似函数使用不同数量的形参时,重载也很方便。例如,假设有一个程序包含的函数可以返回整数和。其中一个函数返回 2 个整数的和,另一个返回 3 个整数的和,还有一个返回 4 个整数的和。以下是它们的函数头:
int sum(int num1, int num2)
int sum(int num1, int num2, int num3)
int sum(int num1, int num2, int num3, int num4)
由于每个函数的形参数量不同,所以它们可以在同一个程序中使用。下面程序使用了 2 个函数,每一个都被命名为 CalcWeeklyPay,以确定员工的每周总收入。一个版本的函数使用一个 int 和一个 double 形参,而另一个版本则仅使用一个 double 形参。
#include iostream #include iomanip using namespace std; //Function prototypes char getChoice (); double calcWeeklyPay(int, double); double calcWeeklyPay(double); int main() char selection; // Menu selection int worked; // Weekly hours worked double rate, // Hourly pay rate yearly; // Annual salary // Set numeric output formatting cout fixed showpoint setprecision(2); //Display the menu and get a selection cout Do you want to calculate the weekly pay of/n cout (H) an hourly-wage employee, or /n cout (S) a salaried employee? selection = getChoice (); // Process the menu selection switch (selection) //Hourly employee case H : case h : cout How many hours were worked? cin worked; cout What is the hourly pay rate? cin rate; cout The gross weekly pay is $ cout calcWeeklyPay(worked, rate) endl; break; //Salaried employee case S : case s : cout What is the annual salary? cin yearly; cout The gross weekly pay is $ cout calcWeeklyPay(yearly) endl; return 0; char getChoice() char letter; // Holds user1s letter choice // Get the user s choice cin letter; // Validate the choice while (letter != H letter != h letter != S letter != s ) cout Enter H or S: cin letter; // Return the choice return letter; double calcWeeklyPay(int hours, double payRate) return hours * payRate; double calcWeeklyPay(double annSalary) return annSalary / 52.0; }
程序输出结果:
Do you want to calculate the weekly pay of
(H) an hourly-wage employee, or
(S) a salaried employee? H
How many hours were worked? 40
What is the hourly pay rate? 18.50
The gross weekly pay is $740.00
22061.html
chtml相关文章
- C++多态纯虚函数和抽象类与多态案例(二)-制作饮品
- EasyC++08,C++算术运算符与类型转换
- C++类中静态变量和静态方法使用介绍
- c++ findwindow函数_matlab中怎么查找函数的用法
- 【c++】【基础】【primer_plus】【第八章】 引用与模板函数
- Cu002FC++ 中的 atol()、atoll() 和 atof() 函数
- C++ Boost 文件系统相关函数
- c++标准库
- C/C++生态工具链——内存泄露检测工具Valgrind
- C++ Primer Plus习题及答案-第二章
- C++11新的类功能(特殊成员函数、override和final)
- 两行代码让 Python 性能超越 C++ 3 个数量级
- 【C++ 语言】面向对象 ( 类定义 | 限制头文件引用次数 | 构造方法 | 析构方法 )
- 【C++修炼之路】3. 类和对象(中)
- 【C++】类和对象 (上篇)
- 【C 语言】文件操作 ( 配置文件读写 | 框架搭建 | 头文件定义 | 头文件导入限制 | 兼容 C++ 语言 | 函数形参输入输出属性注释)
- c++基础篇之C++ 模板
- C++静态成员变量和静态成员函数详解
- C++类模板中的非类型参数
- C++函数默认参数(详解版)
- C++静态成员函数(无师自通)
- C++学生信息管理系统(文件版)源码下载、源码解析和设计思路
- 从汇编看c++中函数里面的static关键字的使用说明
- 一道超经典的C++结构体的题目
- 关于C++静态成员函数访问非静态成员变量的问题
- C++中单链表的建立与基本操作
- C++实现迷宫算法实例解析
- C++虚函数的实现机制分析
- C++中回调函数(CallBack)的用法分析