模板2

#include <iostream>
#include <string>

using namespace std;

// template<class T>  這就是模板,
template<class T,int size = 100>  // 這個就是模板中的常量,
class Array
{
private:
	//enum { size = 100 };
	int A[size];
public:
	T& operator[](T& index)
	{
		index > 0 && index <= size;
		return A[index];
	}
};

class Number
{
private:
	float f;
public:
	Number(float ff = 0.0f) :f(ff) {}
	Number& operator=(const Number& x)
	{
		f = x.f;
		return *this;
	}
	~Number(){}
	operator float() const
	{
		return f;
	}
	friend std::ostream& operator<<(std::ostream& os, const Number& x)
	{
		return os << x.f;
	}
};

template<class T,int size = 100>
class StackTemplate
{
private:
	T stack[size];
	int top;
public:
	StackTemplate() : top(0) {}
	void push(const T& i)
	{
		if (top < size)
			stack[top++] = i;
	}
	T pop()
	{
		if (top > 0)
			return stack[--top];
	}

};

class Apple
{
public:
	Apple()
	{
		cout << "created " << endl;
	}
	virtual ~Apple()
	{
		cout << "deleted " << endl;
	}
};

int main()
{
	StackTemplate<Apple, 2> a;
	cout << endl;     // 這用到所有權的問題,
	Apple a1, a2;
	a.push(a1);
	a.push(a2);

	/*StackTemplate<int,20> s;
	for (int i = 0; i < 20; i++)
		s.push(i);
	for (int j = 0; j < 20; j++)
		cout << s.pop() << endl;*/


	/*Array<int> a;
	for (int i = 0; i < 100; i++)
	{
		a[i] = i * i;
	}
	for (int j = 0; j < 100; j++)
	{
		cout << "a[" << j << "] = " << a[j] << endl;
	}*/

	/*Number cc = 3.14;
	cout << cc << endl;*/

	


	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章