首页 > 代码库 > C++ 默认值函数参数 重载
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;
}
/*
* 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;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。