首页 > 编程语言 >C++快速入门 第六讲:函数的重载

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

时间:2023-08-18 14:25:54浏览次数:40  
标签:std 入门 double cout C++ 重载 calc tempIn2 tempIn

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

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

  1 #include <iostream>
  2 
  3 void convertTemperature(double tempIn, char typeIn);
  4 void convertTemperature(int tempIn, char typeIn);
  5 
  6 int main()
  7 {
  8     double tempIn;
  9     int tempInInt;
 10     char typeIn;
 11 
 12     std::cout << "请以【xx.x C】或【xx.x F】的形式输入温度: ";
 13       std::cin >> tempIn >> typeIn;
 14       std::cin.ignore(100, '\n');
 15       std::cout << "\n";
 16     convertTemperature(tempIn, typeIn);
 17 
 18     std::cout << "请以【xx C】或【xx F】的形式输入温度: ";
 19       std::cin >> tempInInt >> typeIn;
 20       std::cin.ignore(100, '\n');
 21       std::cout << "\n";
 22     convertTemperature(tempInInt, typeIn);
 23 
 24     return 0;
 25 }
 26 
 27 void convertTemperature(double tempIn, char typeIn)
 28 {     
 29       const unsigned short ADD_SUBTRACT = 32;
 30       const double RATIO = 9.0 / 5.0;
 31       
 32       double tempOut;
 33       char typeOut;      
 34       
 35       switch( typeIn )
 36       {
 37       case 'C':
 38       case 'c':
 39             tempOut = (tempIn * RATIO) + ADD_SUBTRACT;
 40             typeOut = 'F';
 41             typeIn = 'C';
 42             break;
 43 
 44       case 'F':
 45       case 'f':
 46             tempOut = (tempIn - ADD_SUBTRACT) / RATIO;
 47             typeOut = 'C';
 48             typeIn = 'F';
 49             break;
 50 
 51       default:
 52             typeOut = 'E';
 53             break;                  
 54       }
 55 
 56       if( typeOut != 'E' )
 57       {
 58             std::cout << tempIn << typeIn << " = " << tempOut << typeOut << "\n\n";
 59       }
 60       else
 61       {
 62             std::cout << "请按照给出格式输入!" << "\n\n";
 63       }
 64 
 65       std::cout << "请输入任意字符结束!" << "\n";
 66       std::cin.get();
 67 }
 68 
 69 void convertTemperature(int tempIn, char typeIn)
 70 {     
 71       const unsigned short ADD_SUBTRACT = 32;
 72       const double RATIO = 9.0 / 5.0;
 73       
 74       int tempOut;
 75       char typeOut;      
 76       
 77       switch( typeIn )
 78       {
 79       case 'C':
 80       case 'c':
 81             tempOut = (tempIn * RATIO) + ADD_SUBTRACT;
 82             typeOut = 'F';
 83             typeIn = 'C';
 84             break;
 85 
 86       case 'F':
 87       case 'f':
 88             tempOut = (tempIn - ADD_SUBTRACT) / RATIO;
 89             typeOut = 'C';
 90             typeIn = 'F';
 91             break;
 92 
 93       default:
 94             typeOut = 'E';
 95             break;                  
 96       }
 97 
 98       if( typeOut != 'E' )
 99       {
100             std::cout << tempIn << typeIn << " = " << tempOut << typeOut << "\n\n";
101       }
102       else
103       {
104             std::cout << "请按照给出格式输入!" << "\n\n";
105       }
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 {
 9     double tempIn,tempIn2,tempIn3;
10     int number; 
11     double tempOut;
12 
13     std::cout << "请输入数据个数: ";
14     std::cin >> number;
15    
16     std::cout << "请以【xx xx】形式输入具体数据: ";  
17     switch(number)
18     {
19         case 1:
20             std::cin >> tempIn;
21             tempOut = calc(tempIn);
22             break;
23         case 2:
24             std::cin >> tempIn >> tempIn2;
25             tempOut = calc(tempIn, tempIn2);
26             break;
27         case 3:
28             std::cin >> tempIn >> tempIn2 >> tempIn3;
29             tempOut = calc(tempIn, tempIn2, tempIn3);
30             break;
31         default:
32             std::cout << "错误!";
33             break; 
34     }
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 {
45     return tempIn*tempIn;
46 }
47 
48 double calc(double tempIn, double tempIn2)
49 {
50     return tempIn*tempIn2;
51 }
52 
53 double calc(double tempIn, double tempIn2, double tempIn3)
54 {
55     return (tempIn + tempIn2 + tempIn3);
56 }

标签:std,入门,double,cout,C++,重载,calc,tempIn2,tempIn
From: https://www.cnblogs.com/ybqjymy/p/17640341.html

相关文章

  • C++快速入门 第七讲:复杂的数据类型
    数组:可以把许多个同类型的值存储在同一变量名下实例1:输入的数据存储到数组中,并计算其和与平均值输出1#include<iostream>23usingnamespacestd;//使用作用域45intmain()6{7intarray[10];8inti;9intsum=0;10doubleaverage;......
  • C++快速入门 第八讲:复杂的数据类型——指针
    小知识:程序以文件的形式存储在硬盘,但它们却是在计算机的内存中运行对于变量可以通过变量名与地址两种方式进行索引,变量的地址在程序执行期间是不会发生变换的地址是计算机内存中的某个位置;指针是专门用来存放地址的特殊类型变量......
  • C++快速入门 第九讲:复杂的数据类型——指针02
    指针的类型必须与由它保存其地址的变量的类型一致,当某个变量的地址给了指针p时,就可以通过*p(即表示该地址的数据)来对该变量数据进行操作一定要牢记一个事实:指针所保存的是内存中的一个地址,它并不保存指向的数据的值本身。因此务必确保指针对应一个已经存在的变量或者一块已经分配......
  • C++快速入门 第十讲:复杂的数据类型——指针和数组
    计算机是把数组以一组连续的内存块保存的。数组的第一个元素的地址为该数组的基地址。实例1:数组元素地址打印1#include<iostream>23usingnamespacestd;45intmain()6{7constunsignedshortITEMS=5;8intintArray[ITEMS]={1,2,3,4,5}......
  • C++快速入门 第十一讲:结构
    结构是一种由程序员自己定义的、由其他变量类型组合而成的数据类型。其所能包含的变量的个数是没有限制的。实例1:简单数据库读写1#include<iostream>2#include<fstream>//文件操作3#include<windows.h>//为了使用Sleep()函数45structFishO......
  • C++快速入门 第十二讲:传值、传址和传引用
    实例1:值传递1#include<iostream>23voidchangeAge(intage,intnewAge);4intmain()5{6intage=24;//定义一个age,占一处地址7std::cout<<"Myageis"<<age<<"\n";89changeAge(age,ag......
  • C++快速入门 第二讲:从一个小程序说起
    cout(cout<<i表示变量i流向屏幕显示)是一个输出流对象,属于basic_ostream类的对象。ostream类在iostream头文件中定义。同理cin(回车后,键盘输入缓冲区内容流向cin流的内部缓冲区,cin>>xx操作便从这个缓冲区提取数据,即键盘输入流向程序)为输入流对象,C++标准库所使用的所有标识符(即类......
  • C++快速入门 第三讲:输入输出方法
    实例1:忽略输入字符串的前面部分字符输出1#include<iostream>//23usingnamespacestd;//名字空间45intmain()6{7charbuf[20];//只能存放19个字符,因为字符串以0结尾89cin.ignore(7);//忽略输入的前七个字符10cin.g......
  • C++快速入门 第四讲:文件操作
    ifream与ofream分别为文件读取类和文件写入类实例1:文件读取(读取同一文件夹的test.txt文件内容)1#include<fstream>//涉及到了文件流操作2#include<iostream>34usingnamespacestd;56intmain()//in输入:读out输出:写7{8ifstreamin;//......
  • C++快速入门 第五讲:输入输出小结
    实例1:根据输入内容输出1#include<iostream>2usingnamespacestd;//名字空间3intmain()4{5charanswer;67cout<<"请问可以格式化您的硬盘吗?!【Y/N】"<<"\n";8cin>>answer;910switch(answer......