/*Copyright (c)2016,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:main.cpp
*作 者:舒文超
*完成日期:2016年3月24日
*版 本 号:v1.0
*
*问题描述:分析以下程序运行机制
*/
#include <iostream>
using namespace std;
const double pi = 3.1415926;
float area(float r = 6.5);
float volume(float h,float r = 6.5);
int main()
{
cout << area() << endl;
cout << area(7.5) << endl;
cout << volume(45.6) << endl;
cout << volume(34.2,10.4) << endl;
return 0;
}
float area(float r)
{
return pi*r*r;
}
float volume(float h,float r)
{
return pi*r*r*h;
}
分析:
(1):去掉第四行的“=6.5”则在第一次调用area()函数时会因为参数没有确定的值而错误。
(2):将第十四行改为“float area(float r = 6.5)”会出错,原因是在函数声明中已经指定了默认实参。
(3):将第五行“float h,float r = 6.5”改为“float h,float r”会出错,原因是在调用volume(45.6)时其中少一个参数值。
(4):将第五行改为“volume(float h = 0,float r = 6.5)”无错误,此时调用volume()时可以不传参数值。