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

📄 pex3_6.cpp

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 CPP
字号:
#include <iostream.h>

class Ref
{
	private:
		// number of data values submitted that are > 0
		int positiveCount;
		// number of data values submitted that are < 0
		int negativeCount;

	public:
		// constructor
		Ref(void);
		
		void Count(int x);
		void Write(void) const;
};

// constructor. initialize count data members to 0
Ref::Ref(void) : positiveCount(0), negativeCount(0)
{}

// increment negativeCount if x < 0 and positiveCount if x > 0
void Ref::Count(int x)
{
	if (x < 0)
		negativeCount++;
	else
	if (x > 0)
		positiveCount++;
}

void Ref::Write(void) const
{
	cout << "Negative values: " << negativeCount << "  ";
	cout << "Positive values: " << positiveCount << endl;
}

// record the results of reading five numbers in v.
// pass v by value
void PassByValue(Ref v)
{
	int num;

	for (int i = 0;i < 5;i++)
	{
		cin >> num;			// input 1  2  3  -1  -7
		if (num != 0)
			v.Count(num);
	}
}
		
// record the results of reading five numbers in v.
// pass v by reference
void PassByReference(Ref &v)
{
	int num;

	for (int i = 0;i < 5;i++)
	{
		cin >> num;			// input 1  2  3  -1  -7
		if (num != 0)
			v.Count(num);
	}
}

void main(void)
{
	Ref  obj;
	
	PassByValue(obj);
	obj.Write();
	
	PassByReference(obj);
	obj.Write();
}

/*
<Run>

1  2  3  -1  -7
Negative values: 0  Positive values: 0
1  2  3  -1  -7
Negative values: 2  Positive values: 3
*/

⌨️ 快捷键说明

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