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

📄 4elist1907.cpp

📁 《21天学通C++》附盘的原代码。书上的每个例子在这里都有相应的C语言程序。
💻 CPP
字号:
#include <iostream>
using namespace std;

const int DefaultSize = 3;

// A trivial class for adding to arrays
class Animal
{
public:
	// constructors
	Animal(int);
	Animal();
	~Animal();

	// accessors
	int GetWeight() const { return itsWeight; }
	void SetWeight(int theWeight) { itsWeight = theWeight; }

	// friend operators
	friend ostream& operator<< (ostream&, const Animal&);

private:
	int itsWeight;
};

// extraction operator for printing animals
ostream& operator<<
	(ostream& theStream, const Animal& theAnimal)
{
	theStream << theAnimal.GetWeight();
	return theStream;
}

Animal::Animal(int weight):
	itsWeight(weight)
{
	//cout << "animal(int) ";
}

Animal::Animal():
	itsWeight(0)
{
	//cout << "animal() ";
}

Animal::~Animal()
{
	//cout << "Destroyed an animal...";
}

template <class T>  // declare the template and the parameter
class Array            // the class being parameterized
{
public:
	// constructors
	Array(int itsSize = DefaultSize);
	Array(const Array &rhs);
	~Array() { delete [] pType;   itsNumberArrays--; }

	// operators
	Array& operator=(const Array&);
	T& operator[](int offSet) { return pType[offSet]; }
	const T& operator[](int offSet) const
	{ return pType[offSet]; }
	// accessors
	int GetSize() const { return itsSize; }
	static int GetNumberArrays() { return itsNumberArrays; }

	// friend function
	friend ostream& operator<< (ostream&, const Array<T>&);

private:
	T *pType;
	int  itsSize;
	static int itsNumberArrays;
};

template <class T>
int Array<T>::itsNumberArrays = 0;

template <class T>
Array<T>::Array(int size = DefaultSize):
	itsSize(size)
{
	pType = new T[size];
	for (int i = 0; i<size; i++)
		pType[i] = (T)0;
	itsNumberArrays++;
}

template <class T>
Array<T>& Array<T>::operator=(const Array &rhs)
{
	if (this == &rhs)
		return *this;
	delete [] pType;
	itsSize = rhs.GetSize();
	pType = new T[itsSize];
	for (int i = 0; i<itsSize; i++)
		pType[i] = rhs[i];
}

template <class T>
Array<T>::Array(const Array &rhs)
{
	itsSize = rhs.GetSize();
	pType = new T[itsSize];
	for (int i = 0; i<itsSize; i++)
		pType[i] = rhs[i];
	itsNumberArrays++;
}

template <class T>
ostream& operator<< (ostream& output, const Array<T>& theArray)
{
	for (int i = 0; i<theArray.GetSize(); i++)
		output << "[" << i << "] " << theArray[i] << endl;
	return output;
}

int main()
{
	cout << Array<int>::GetNumberArrays() << " integer arrays\n";
	cout << Array<Animal>::GetNumberArrays();
	cout << " animal arrays\n\n";
	Array<int> intArray;
	Array<Animal> animalArray;

	cout << intArray.GetNumberArrays() << " integer arrays\n";
	cout << animalArray.GetNumberArrays();
	cout << " animal arrays\n\n";

	Array<int> *pIntArray = new Array<int>;

	cout << Array<int>::GetNumberArrays() << " integer arrays\n";
	cout << Array<Animal>::GetNumberArrays();
	cout << " animal arrays\n\n";

	delete pIntArray;

	cout << Array<int>::GetNumberArrays() << " integer arrays\n";
	cout << Array<Animal>::GetNumberArrays();
	cout << " animal arrays\n\n";
	return 0;
}

⌨️ 快捷键说明

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