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

以下代码对于类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
执行构析构函数
******************************************************/

  

转载于:https://www.cnblogs.com/simonLiang/p/5905592.html

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐