首页 > 其他 > 详细

缺省函数

时间:2016-09-25 12:01:11      阅读:205      评论:0      收藏:0      [点我收藏+]

所谓的缺省函数,就是已经定义了初始变量的函数。

以下代码对于类A中的方法set() 和 普通函数 func() 均有默认值!!

#include <iostream>
using namespace std;

class A
{
public:
	A(){cout<<"执行构造函数创建一个对象\n";}
	~A(){cout<<"执行构析构函数\n";}
	void set(int width= 10,int height =6);
private:
	int w;
	int h;
};
void A::set(int width, int height)
{
	w=width;
	h=height;
	cout<<"h="<<h<<endl<<"w="<<w<<endl;
	cout<<"S=h*w="<<h*w<<endl;
}

void func(int a=5, int b=4)	
{	
	cout<<"a="<<a<<endl<<"b="<<b<<endl;
}

int main()
{
	A a;
	a.set();
	func();
	return 0;
}
/*****************************************************
执行构造函数创建一个对象
h=6
w=10
S=h*w=60
a=5
b=4
执行构析构函数
******************************************************/

  

缺省函数

原文:http://www.cnblogs.com/simonLiang/p/5905592.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!