prog6.cpp

来自「C++语言程序设计题典」· C++ 代码 · 共 43 行

CPP
43
字号
#include <iostream.h>
class Cat
{
public:
	Cat();
	Cat(const Cat&);
	~Cat();
	int getage() const { return *itsage; }
	void setage(int age) { *itsage=age; }
protected:
	int *itsage;
};
Cat::Cat()
{
	itsage=new int;
	*itsage=5;
}
Cat::Cat(const Cat& c)
{
	itsage=new int;
	*itsage=*c.itsage;
}
Cat::~Cat()
{
	delete itsage;
	itsage=0;
}
void main()
{
	Cat frisky;
	cout << "frisky's age:" << frisky.getage() << endl;
    cout << "setting frisky to 6...\n";
	frisky.setage(6);
	cout << "creating boots from frisky\n";
	Cat boots(frisky);
	cout << "frisky's age:" << frisky.getage() << endl;
	cout << "boots'age:" << boots.getage() << endl;
	cout << "setting frisky to 7...\n";
	frisky.setage(7);
	cout << "frisky's age:" << frisky.getage() << endl;
	cout << "boots'age:" << boots.getage() << endl;
}

⌨️ 快捷键说明

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