码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 默认值函数参数 重载

时间:2014-06-10 06:43:40      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:c++   重载   默认值函数参数   

//默认值函数参数


/*
 * Test.cpp
 *
 *  Created on: 2014年6月9日
 *      Author: John
 */








#include<iostream>
#include<string.h>
#define Pi 3.14


void FunTest(double Radius, double Height=0);




int main()
{
using std::cin;
using std::cout;
using std::endl;
using std::string;


double Radius = 0;
double Height =0;
cout<<"Input Radius:";
cin>>Radius;
cout<<"Do you wanna Input Height?(y/n)";
string line;
cin>>line;
cout<<"str is : "<<line<<endl;


char c = line[0];
std::cout<<"your answer is : "<<c<<endl;;


if(c!=‘y‘){
FunTest(Radius,Height);
}else{
cout<<"Please Input Height : ";
std::cin>>Height;
FunTest(Radius,Height);
}


return 0;
}




//void FucTest(double Radius, double Height = 0)
void FunTest(double Radius, double Height)
{
using std::cout;
using std::endl;
if(Height == 0.0)
{
cout<<"This is a Sphere."<<endl;
cout<<"The Volume of the Sphere is : "<<4*Pi*Radius*Radius*Radius/3.0;
}else{
cout<<"This is a Cylinder."<<endl;
cout<<"The Volume of the Cylinder is : "<<Pi*Radius*Radius*Height;
}
}




//重载


/*
 * OverLoading.cpp
 *
 *  Created on: 2014年6月9日
 *      Author: John
 */








#include<iostream>
#include<string.h>
const int  Pi = 3.14;


double CalcVolume(double Radius);


double CalcVolume(double Radius, double Height);


int main()
{
using std::cin;
using std::cout;
using std::endl;
using std::string;


double Radius = 0;
double Height = 0;


cout<<"Enter ‘s‘ for Sphere, ‘c‘ for Cylinder:"<<endl;
string str;
cin>>str;
cout<<"Enter Radius : ";
cin>>Radius;
if(str == "s" || str == "S")
{
cout<<"The Volume of the Sphere is : "<<CalcVolume(Radius)<<endl;
}
if(str == "c" || str == "C"){
cout<<"Enter Height : ";
cin>>Height;


cout<<"The Volume of the Cylinder is : "<<CalcVolume(Radius,Height)<<endl;
}
return 0;


}




double CalcVolume(double Radius)
{
return 4*Pi*Radius*Radius*Radius / 3.0;
}
double CalcVolume(double Radius, double Height)
{
return Pi*Radius*Radius*Height;
}













C++ 默认值函数参数 重载,布布扣,bubuko.com

C++ 默认值函数参数 重载

标签:c++   重载   默认值函数参数   

原文地址:http://blog.csdn.net/haimian520/article/details/29599129

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!