缺省函数

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

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

#include <iostream>
using namespace std;

class A
{
public:
	A(){cout<<"执行构造函数创建一个对象
";}
	~A(){cout<<"执行构析构函数
";}
	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
执行构析构函数
******************************************************/

  

原文地址:https://www.cnblogs.com/simonLiang/p/5905592.html