6.25 被虐记 7 期末测试 类模板
来源:互联网 发布:淘宝怎么改旺旺名字 编辑:程序博客网 时间:2024/04/28 01:05
题目描述
有一种类,海纳百川,可以对任意类型的数据进行存取,造就这个传奇的,就是模板。
下面的程序中,定义一个类模板,但其中有些成份漏掉了,请你将他们补足,使程序能正确运行,得到要求的输出结果。
请提交begin到end部分的代码。
//************* begin *****************
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
______(1)_______//类模板,实现对任意类型数据进行存取
class Store
{
private:
T item; //用于存放任意类型的数据
int haveValue; //用于标记item是否为空,0表示为空,1表示有数据
public:
Store(); //默认构造构造函数
__(2)__ getElem(); //提取数据,返回item的值
void putElem(T x);//存入数据
};
______(3)_______//默认构造构造函数的实现
Store<T>::Store(void):haveValue(0){};
template<class T> //提取数据函数的实现,返回item中的数据
T Store<T>::getElem(void)
{
if (haveValue==0) //如果试图提取未初始化的数据,则终止程序
{
cout<<"NO item present!\n";
exit(1);
}
return item;
}
template<class T>//存入数据的实现
______(4)_______putElem(T x)
{
haveValue=1;
item = x;
}
//************* end *****************
int main()
{
Store<int> si;
Store<double> sd;
int i;
double d;
cin>>i>>d;
si.putElem(i);
sd.putElem(d);
cout <<setiosflags(ios::fixed)<<setprecision(2);
cout<<si.getElem()<<endl;
cout<<sd.getElem()<<endl;
return 0;
}
输入
一个整数和一个小数,将通过putElem函数存于相应的对象实例中
输出
通过getElem()取出相应对象中存入的数据,并且输出,浮点型保留两位小数
样例输入
240 56.7183
样例输出
240
56.72
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
template<typename T>//类模板,实现对任意类型数据进行存取
class Store
{
private:
T item; //用于存放任意类型的数据
int haveValue; //用于标记item是否为空,0表示为空,1表示有数据
public:
Store(); //默认构造构造函数
T getElem(); //提取数据,返回item的值
void putElem(T x);//存入数据
};
template<class T>//默认构造构造函数的实现
Store<T>::Store(void):haveValue(0){};
template<class T> //提取数据函数的实现,返回item中的数据
T Store<T>::getElem(void)
{
if (haveValue==0) //如果试图提取未初始化的数据,则终止程序
{
cout<<"NO item present!\n";
exit(1);
}
return item;
}
template<class T>//存入数据的实现
void Store<T>::putElem(T x)
{
haveValue=1;
item = x;
有一种类,海纳百川,可以对任意类型的数据进行存取,造就这个传奇的,就是模板。
下面的程序中,定义一个类模板,但其中有些成份漏掉了,请你将他们补足,使程序能正确运行,得到要求的输出结果。
请提交begin到end部分的代码。
//************* begin *****************
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
______(1)_______//类模板,实现对任意类型数据进行存取
class Store
{
private:
T item; //用于存放任意类型的数据
int haveValue; //用于标记item是否为空,0表示为空,1表示有数据
public:
Store(); //默认构造构造函数
__(2)__ getElem(); //提取数据,返回item的值
void putElem(T x);//存入数据
};
______(3)_______//默认构造构造函数的实现
Store<T>::Store(void):haveValue(0){};
template<class T> //提取数据函数的实现,返回item中的数据
T Store<T>::getElem(void)
{
if (haveValue==0) //如果试图提取未初始化的数据,则终止程序
{
cout<<"NO item present!\n";
exit(1);
}
return item;
}
template<class T>//存入数据的实现
______(4)_______putElem(T x)
{
haveValue=1;
item = x;
}
//************* end *****************
int main()
{
Store<int> si;
Store<double> sd;
int i;
double d;
cin>>i>>d;
si.putElem(i);
sd.putElem(d);
cout <<setiosflags(ios::fixed)<<setprecision(2);
cout<<si.getElem()<<endl;
cout<<sd.getElem()<<endl;
return 0;
}
输入
一个整数和一个小数,将通过putElem函数存于相应的对象实例中
输出
通过getElem()取出相应对象中存入的数据,并且输出,浮点型保留两位小数
样例输入
240 56.7183
样例输出
240
56.72
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
template<typename T>//类模板,实现对任意类型数据进行存取
class Store
{
private:
T item; //用于存放任意类型的数据
int haveValue; //用于标记item是否为空,0表示为空,1表示有数据
public:
Store(); //默认构造构造函数
T getElem(); //提取数据,返回item的值
void putElem(T x);//存入数据
};
template<class T>//默认构造构造函数的实现
Store<T>::Store(void):haveValue(0){};
template<class T> //提取数据函数的实现,返回item中的数据
T Store<T>::getElem(void)
{
if (haveValue==0) //如果试图提取未初始化的数据,则终止程序
{
cout<<"NO item present!\n";
exit(1);
}
return item;
}
template<class T>//存入数据的实现
void Store<T>::putElem(T x)
{
haveValue=1;
item = x;
}
类模板 转战下一篇博文
0 0
- 6.25 被虐记 7 期末测试 类模板
- 6.25 被虐记 10 期末测试
- 6.25 被虐记 3 期末测试 const
- 6.25 被虐记 6 期末测试 变相求和
- 6.25 被虐记 9 期末测试 玩游戏
- 被虐记 2 期末测试
- 6.25 被虐记 5 期末测试 盖房子 消耗费用
- 6.25 被虐记 8 分钱 求 总钱数 期末测试
- 6.25 被虐记 1 期末测试 派生继承 10+100=110
- 期末测试小结
- 测试货币银行学期末复习
- sicily 最短距离 期末测试
- 期末
- 期末
- 期末
- 期末
- java面向对象程序设计期末测试题
- sicily 选择排序比较次数 期末测试
- Oracle学习(十五)之用户配置文件及用户管理
- 问题
- SYBASE备份报错:Can't open a connection to site 'SYB_BACKUP',see the error log file in the ASE directory
- Firefly 3288重新制作android和lubuntu双系统固件
- Mysql常见四种索引的使用
- 6.25 被虐记 7 期末测试 类模板
- Mac下jdk的安装路径
- HTML5自动播放背景音乐
- oracle恢复数据库数据到一个点
- C++栈和队列
- 修改 eclipse 文件编码格式
- 九鼎创展s5p4418开发板-修改Android4.4系统开机LOGO
- Ubuntu下安装Mysql小结
- Error:Execution failed for task ':app:compileDebugAidl'. > Executor Singleton not started