ch11newcounter.cpp

来自「此例子是学习C++的好东西」· C++ 代码 · 共 55 行

CPP
55
字号
// Program 11-2 NewCounter   A program that uses the class NewCounter 
// that is derived from Counter and protected count variable.

//File: Ch11NewCounter.cpp

#include <iostream.h>

class Counter
{
protected:
	int count;
public:
	Counter(){count = 0;}
	void operator ++(){ ++count; }
	int GetCount() { return count; }
	void SetCount( int c ) { count = c; }
	void PrintCount() { cout << "\n The count is " << count; }
};



class NewCounter:public Counter
{
public:
	void operator --() { --count;} 
	
};




int main()
{

	NewCounter nctr;  // new counter object

	cout << "\n Sample program with class NewCounter \n";

	cout << "\n What is in the nctr?";
	
	nctr.PrintCount();

	cout << "\n Increment the new counter object twice: ";
	++nctr;
	++nctr;

	nctr.PrintCount();

	cout << "\n Now decrement the new counter: ";
	--nctr;
	nctr.PrintCount();
	cout << "\n\n All finished counting! \n";
	return 0;
}

⌨️ 快捷键说明

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