⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 5.11-参数默认值.cpp

📁 Visual C++的课件
💻 CPP
字号:
//Listing 10.2 Default values in member functions
#include <iostream.h>

// Rectangle class declaration
class Rectangle
{
public:
	// constructors
	Rectangle(int width, int height);
	~Rectangle(){}
	void DrawShape(int aWidth, int aHeight, bool UseCurrentVals =
												false) const;

private:
	int itsWidth;
	int itsHeight;
};

//Constructor implementation
Rectangle::Rectangle(int width, int height):
itsWidth(width),       // initializations
itsHeight(height)
{}                     // empty body


// default values used for third parameter
void Rectangle::DrawShape(
int width,
int height,
bool UseCurrentValue
) const
{
	int printWidth;
	int printHeight;

	if (UseCurrentValue == true)
	{
		printWidth = itsWidth;       // use current class values
		printHeight = itsHeight;
	}
	else
	{
		printWidth = width;         // use parameter values
		printHeight = height;
	}


	for (int i = 0; i<printHeight; i++)
	{
		for (int j = 0; j< printWidth; j++)
		{
			cout << "*";
		}
		cout << "\n";
	}
}

// Driver program to demonstrate overloaded functions
int main()
{
	// initialize a rectangle to 30,5
	Rectangle theRect(30,5);
	cout << "DrawShape(0,0,true)...\n";
	theRect.DrawShape(0,0,true);
	cout <<"DrawShape(40,2)...\n";
	theRect.DrawShape(40,2);
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -