默认参数的函数

  • 函数的缺省参数必须从右向左依次定义,即在模型形式参数的右边不能有缺省的形式参数
  • 比如:int fun(int a = 0, int b, int c = 0)


#include<iostream>
//using namespace std; 
int distance(int x1,int y1,int x2=3,int y2=4);
int main() {
	int a = distance(3,4);
	int b = distance(4,3);
	int c = distance(3,4,5);
	int d = distance(3,4,5,6);
	std::cout<< a <<std::endl;
	std::cout<< b <<std::endl;
	std::cout<< c <<std::endl;
	std::cout<< d <<std::endl;
//	cout<< c <<endl;
//	cout<< d <<endl;
	return 0;
}
int distance(int x1,int y1,int x2,int y2) {
	int x,y;
	x=x2+x1;
	y=y2+y1;
	return x*x+y*y;
}

猜你喜欢

转载自javaeye-hanlingbo.iteye.com/blog/2408119