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

📄 4elist1015.cpp

📁 《21天学通C++》附盘的原代码。书上的每个例子在这里都有相应的C语言程序。
💻 CPP
字号:
// Listing 10.15
// Copy constructors

#include <iostream>

using namespace std;

class CAT
{
public:
	CAT();                         // default constructor
	// copy constructor and destructor elided!
	int GetAge() const { return *itsAge; }
	int GetWeight() const { return *itsWeight; }
	void SetAge(int age) { *itsAge = age; }
	CAT & operator=(const CAT &);

private:
	int *itsAge;
	int *itsWeight;
};

CAT::CAT()
{
	itsAge = new int;
	itsWeight = new int;
	*itsAge = 5;
	*itsWeight = 9;
}


CAT & CAT::operator=(const CAT & rhs)
{
	if (this == &rhs)
	return *this;
	*itsAge = rhs.GetAge();
	*itsWeight = rhs.GetWeight();
	return *this;
}


int main()
{
	CAT frisky;
	cout << "frisky's age: " << frisky.GetAge() << endl;
	cout << "Setting frisky to 6...\n";
	frisky.SetAge(6);
	CAT whiskers;
	cout << "whiskers' age: " << whiskers.GetAge() << endl;
	cout << "copying frisky to whiskers...\n";
	whiskers = frisky;
	cout << "whiskers' age: " << whiskers.GetAge() << endl;
	return 0;
}

⌨️ 快捷键说明

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