#include<iostream>
#include<stdio.h>
struct Student
{
int id; // 学号
float gpa; // 平均分
};
template <class T>
class Store
{
private:
T item;
int haveValue;
public:
Store(void);
T GetElem(void); //提取数据函数
void PutElem(T x); // 存入数据函数
};
template <class T>
Store<T>::Store(void): haveValue(0){}
template <class T> //提取数据函数的实现
T Store<T>::GetElem(void)
{
//如果试图提取未初始化的数据,则终止程序
if(haveValue == 0){
cout<<"No item present!"<<endl;
exit(1);
}
return item; // 返回item中存放的数据
}
template <class T> // 存入数据函数的实现
void Store<T>::PutElem(T x)
{
haveValue++; // 将haveValue置为TRUE,表示item中已存入数值
item = x; //将x值存入item
}
void main(void)
{
Student g = {1000,23};
Store<int> S1,S2;
Store<Student> S3;
Store<double> D;
S1.PutElem(3);
S2.PutElem(-7);
cout<<S1.GetElem()<<" "<<S2.GetElem()<<endl;
S3.PutElem(g);
cout<<"The student id is "<<S3.GetElem().id<<endl;
cout<<"Retrieving object D ";
cout<<D.GetElem()<<endl;
}