10_2.cpp

来自「C++语言程序设计案例教程,郑莉编的书」· C++ 代码 · 共 41 行

CPP
41
字号
#include <iostream> 
using namespace std; 
namespace CounterNameSpace 		//声明命名空间
{	int upperbound,lowerbound; 	//数据成员
	class counter				//类成员
	{	int count;
	public:
		counter(int n)
		{	if(n <= upperbound) count = n;
			else count = upperbound;
		}
		void reset(int n)	{ if(n <= upperbound) count = n; }
		int run()
		{	if(count > lowerbound) return count--;
			else return lowerbound;
		}
	}; 
} 

int main() 
{	using CounterNameSpace::upperbound; //使用CounterNameSpace的成员upperbound
	upperbound = 100; 				//无须使用限定符设置成员 upperbound
	CounterNameSpace::lowerbound = 0; 	//仍旧需要使用限定符来访问成员lowerbound.
	CounterNameSpace::counter ob1(10);
	int i;
	do {	
     i = ob1.run(); cout << i << " ";
	} while(i > CounterNameSpace::lowerbound);	cout << endl; 
	using namespace CounterNameSpace;  //使用整个CounterNameSpace的成员
	counter ob2(20); 
	do {		
     i = ob2.run(); cout << i << " ";
	} while(i > lowerbound);	cout << endl; 
	ob2.reset(100);
	lowerbound = 90;
	do {		
     i = ob2.run(); cout << i << " ";
	} while(i > lowerbound); 
	return 0; 
} 

⌨️ 快捷键说明

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