struct Student { // 结构体Student int id; //学号 float gpa; //平均分 }; //定义类模板及函数,函数在类体外定义 template class Store //类模板:实现对任意类型数据进行存取 裁判测试程序样例: int main() { try { Store<int> s1, s2; //定义两个Store<int>类对象,其中数据成员item为int类型 s1.putElem(3); //向对象S1中存入数据(初始化对象S1) s2.putElem(-7); //向对象S2中存入数据(初始化对象S2) cout << s1.getElem() << " " << s2.getElem() << endl; //输出对象S1和S2的数据成员 Student g = { 1000, 23 }; //定义Student类型结构体变量的同时赋以初值 Store<Student> s3; //定义Store<Student>类对象s3,其中数据成员item为Student类型 s3.putElem(g); //向对象D中存入数据(初始化对象D) cout << "The student id is " << s3.getElem().id << endl; //输出对象s3的数据成员 Store<double> d; //定义Store<double>类对象s4,其中数据成员item为double类型 cout << "Retrieving object d... "; cout << d.getElem() << endl; //输出对象D的数据成员 //由于d未经初始化,在执行函数D.getElement()过程中导致程序终止 } catch (...) { cout << "get item error!"; } return 0; }用c++程序实现
时间: 2024-03-22 21:38:08 浏览: 30
这是一个完整的 C++ 程序实现:
```cpp
#include <iostream>
using namespace std;
struct Student {
int id; // 学号
float gpa; // 平均分
};
template <class T>
class Store
{
private:
T item;
bool haveValue;
public:
Store()
{
haveValue = false;
}
Store(T tItem)
{
item = tItem;
haveValue = true;
}
void putElem(T tItem)
{
item = tItem;
haveValue = true;
}
T getElem()
{
if (!haveValue) {
throw runtime_error("No value stored.");
}
return item;
}
};
int main()
{
try {
Store<int> s1, s2; // 定义两个 Store<int> 类对象,其中数据成员 item 为 int 类型
s1.putElem(3); // 向对象 s1 中存入数据(初始化对象 s1)
s2.putElem(-7); // 向对象 s2 中存入数据(初始化对象 s2)
cout << s1.getElem() << " " << s2.getElem() << endl; // 输出对象 s1 和 s2 的数据成员
Student g = {1000, 23}; // 定义 Student 类型结构体变量的同时赋以初值
Store<Student> s3; // 定义 Store<Student> 类对象 s3,其中数据成员 item 为 Student 类型
s3.putElem(g); // 向对象 s3 中存入数据(初始化对象 s3)
cout << "The student id is " << s3.getElem().id << endl; // 输出对象 s3 的数据成员
Store<double> d; // 定义 Store<double> 类对象 s4,其中数据成员 item 为 double 类型
cout << "Retrieving object d... ";
cout << d.getElem() << endl; // 输出对象 d 的数据成员
// 由于 d 未经初始化,在执行函数 d.getElement() 过程中导致程序终止
}
catch (exception& e) {
cout << "Exception: " << e.what() << endl;
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)