函数重载:实质就是用同样的名字再定义一个有着不同参数类型及个数来实现不同操作的函数。
实例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