一、概述
函数重载是指在同一作用域内,可以有一组具有相同函数名,不同参数列表的函数,这组函数被称为重载函数。重载函数通常用来命名一组功能相似的函数,这样做减少了函数名的数量,避免了名字空间的污染,对于程序的可读性有很大的好处。
二、如何使用函数重载
如上所说,重载函数的函数名是相同的,所以为了举例,我先定义了三个结构体,这三个结构体分别代表了三棱锥,长方体和球:
/* 三棱锥 */
struct Triangular_Pyramid
{
float S; /* 底面积 */
float height;
};
/* 长方体 */
struct Cuboid
{
float length;
float width;
float height;
};
/* 球体 */
struct Sphere
{
float radius; /* 半径 */
};
- 函数重载就在于,参数不同,所以,下面定义我用到了三种不同类型的引用:
float volume_culc(Triangular_Pyramid&);
float volume_culc(Cuboid&);
float volume_culc(Sphere&);
c++程序在执行函数时,会根据参数的不同来选择相应的函数;
下面是函数的定义:
/* 三棱锥体积计算函数 */
float volume_culc(Triangular_Pyramid& triangular_pyramid)
{
cout << "You choose triangular pyramid,Please input the floor space and the height:" << endl;
cout << "floor space: ";
cin >> triangular_pyramid.S;
cout << "height: ";
cin >> triangular_pyramid.height;
cout << "The volume of the triangular pyramid is " << triangular_pyramid.S * triangular_pyramid.height * 1/3 << "!\n" << endl;
}
/* 长方体体积计算函数 */
float volume_culc(Cuboid& cuboid)
{
cout << "You choose cuboid,Please input the length ,the width and the height:" << endl;
cout << "length: ";
cin >> cuboid.length;
cout << "width: ";
cin >> cuboid.width;
cout << "height: ";
cin >> cuboid.height;
cout << "The volume of the cuboid is " << cuboid.length * cuboid.width * cuboid.height << "!\n" << endl;
}
float volume_culc(Sphere& sphere)
{
cout << "You choose sphere,Please input the radius: " << endl;
cout << "radius: ";
cin >> sphere.radius;
cout << "The volume of the sphere is " << 4/3 * 3.14 * sphere.radius * sphere.radius * sphere.radius << "!\n" <<endl;
}
这里可以看到,三个函数名是相同的,但是函数内部的实现确完全不同,而函数的参数是三种不同类型的引用,所以当传入参数不同时,函数会根据传入的参数来选择应该执行那个函数;
下面是程序含main的源码 Overloading.cpp :
/*********************************************************************************
* Copyright: (C) 2020 Xiao yang IoT System Studio
* All rights reserved.
*
* Filename: Overloading.cpp
* Description: This file is calculate the volume of three shape
*
* Version: 1.0.0(06/03/2020)
* Author: Lu Xiaoyang <[email protected]>
* ChangeLog: 1, Release initial version on "06/03/2020 09:31:42 AM"
*
********************************************************************************/
#include <iostream>
using namespace std;
struct Triangular_Pyramid
{
float S; /* 底面积 */
float height;
};
struct Cuboid
{
float length;
float width;
float height;
};
struct Sphere
{
float radius; /* 半径 */
};
float volume_culc(Triangular_Pyramid&);
float volume_culc(Cuboid&);
float volume_culc(Sphere&);
int main()
{
struct Triangular_Pyramid triangular_pyramid;
struct Cuboid cuboid;
struct Sphere sphere;
int ch;
while(1)
{
cout << "Which volume of shape do you want to calculate?" << "\n";
cout << "1 for triangular pyramid, 2 for cuboid, 3 for sphere, other to quit..." << "\n";
cout << "You choose: ";
cin >> ch;
if(ch == 1)
volume_culc(triangular_pyramid);
else if(ch == 2)
volume_culc(cuboid);
else if(ch == 3)
volume_culc(sphere);
else
break;
}
return 0;
}
float volume_culc(Triangular_Pyramid& triangular_pyramid)
{
cout << "You choose triangular pyramid,Please input the floor space and the height:" << endl;
cout << "floor space: ";
cin >> triangular_pyramid.S;
cout << "height: ";
cin >> triangular_pyramid.height;
cout << "The volume of the triangular pyramid is " << triangular_pyramid.S * triangular_pyramid.height * 1/3 << "!\n" << endl;
}
float volume_culc(Cuboid& cuboid)
{
cout << "You choose cuboid,Please input the length ,the width and the height:" << endl;
cout << "length: ";
cin >> cuboid.length;
cout << "width: ";
cin >> cuboid.width;
cout << "height: ";
cin >> cuboid.height;
cout << "The volume of the cuboid is " << cuboid.length * cuboid.width * cuboid.height << "!\n" << endl;
}
float volume_culc(Sphere& sphere)
{
cout << "You choose sphere,Please input the radius: " << endl;
cout << "radius: ";
cin >> sphere.radius;
cout << "The volume of the sphere is " << 4/3 * 3.14 * sphere.radius * sphere.radius * sphere.radius << "!\n" <<endl;
}
下面来测试一下程序吧!
- 编译运行
编译通过,程序提示要计算那种形状的体积,我们一一选择:
敲下1时,通过程序源码可知将定义的三棱锥结构体的引用传给了函数,函数通过参数类型判断到了是要计算三棱锥的体积,于是提示我们输入底面积和高,于是,1/3 * 底面积 * 高 得到了三棱锥的体积,继续测试:
得到了长方体的体积;go on ~
输入其他任何数据程序退出:
根据这个程序应该技能大致了解c++特有的函数重载了!