当前位置: 首页 > 网络知识

C++快速入门 第六讲:函数的重载

时间:2026-01-29 09:25:33

函数重载:实质就是用同样的名字再定义一个有着不同参数类型及个数来实现不同操作的函数。

实例1:改变同一函数的输入参数类型

1 #include <iostream> 2 3 void convertTemperature(double tempIn, char typeIn); 4 void convertTemperature(int tempIn, char typeIn); 5 6 int main() 7 26 27 void convertTemperature(double tempIn, char typeIn) 28 55 56 if( typeOut != 'E' ) 57 60 else 61 64 65 std::cout << "请输入任意字符结束!" << "\n"; 66 std::cin.get(); 67 } 68 69 void convertTemperature(int tempIn, char typeIn) 70 97 98 if( typeOut != 'E' ) 99 102 else 103 106 107 std::cout << "请输入任意字符结束!" << "\n"; 108 std::cin.get(); 109 }

作业:calc()传入不同数目的参数时,不同运算的运用

1 #include <iostream> 2 3 double calc(double tempIn);//计算该参数平方值 4 double calc(double tempIn, double tempIn2);//计算两个参数的积 5 double calc(double tempIn, double tempIn2, double tempIn3);//计算三个参数的和 6 7 int main() 8 35 std::cout << "计算结果为:" << tempOut << "\n"; 36 37 std::cin.ignore(100, '\n'); 38 std::cout << "\n"; 39 40 return 0; 41 } 42 43 double calc(double tempIn) 44 47 48 double calc(double tempIn, double tempIn2) 49 52 53 double calc(double tempIn, double tempIn2, double tempIn3) 54


上一篇:C++快速入门 第十五讲:构造器和析构器
下一篇:OpenCV3.3深度神经网络DNN模块 实例5:FCN模型实现图像分割
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器
  • 英特尔第五代 Xeon CPU 来了:详细信息和行业反应
  • 由于云计算放缓引发扩张担忧,甲骨文股价暴跌
  • Web开发状况报告详细介绍可组合架构的优点
  • 如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳
  • 美光在数据中心需求增长后给出了强有力的预测
  • 2027服务器市场价值将接近1960亿美元
  • 生成式人工智能的下一步是什么?
  • 分享在外部存储上安装Ubuntu的5种方法技巧
  • 全球数据中心发展的关键考虑因素
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器

    英特尔第五代 Xeon CPU 来了:详细信息和行业反应

    由于云计算放缓引发扩张担忧,甲骨文股价暴跌

    Web开发状况报告详细介绍可组合架构的优点

    如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳

    美光在数据中心需求增长后给出了强有力的预测

    2027服务器市场价值将接近1960亿美元

    生成式人工智能的下一步是什么?

    分享在外部存储上安装Ubuntu的5种方法技巧

    全球数据中心发展的关键考虑因素