#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;
}
模板2
最新推荐文章于 2022-07-07 18:25:16 发布