- 1、本文档共80页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
AB第九章 群体类和群体数据的组织;*;*;*;*;*;*;*;*;template class T
//类模板:实现对任意类型数据进行存取
class Store
{ private:
T item; // 用于存放任意类型的数据
int haveValue; // 用于标记item是否已被存入内容
public:
Store(void); // 默认形式(无形参)的构造函数
T GetElem(void); //提取数据函数
void PutElem(T x); //存入数据函数
};
// 默认形式构造函数的实现
template class T
StoreT::Store(void): haveValue(0) {};template class T // 提取数据函数的实现
T StoreT::GetElem(void)
{ // 如果试图提取未初始化的数据,则终止程序
if (haveValue == 0)
{ cout No item present! endl;
exit(1);
}
return item; // 返回item中存放的数据
}
template class T // 存入数据函数的实现
void StoreT::PutElem(T x)
{ haveValue++; // 将haveValue 置为 TRUE,表示item中已存入数值
item = x; // 将x值存入item
};int main()
{ Student g= {1000, 23};
Storeint S1, S2;
StoreStudent S3;
Storedouble 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; //输出对象D的数据成员
// 由于D未经初始化,在执行函数D.GetElement()时出错
};*;*;*;*;#ifndef ARRAY_CLASS
#define ARRAY_CLASS
using namespace std;
#include iostream
#include cstdlib
#ifndef NULL
const int NULL = 0;
#endif // NULL
enum ErrorType
{ invalidArraySize, memoryAllocationError,
indexOutOfRange };
char *errorMsg[] =
{ Invalid array size, Memory allocation error,
Invalid index:
};;template class T
class Array
{ private:
T* alist;
int size;
void Error(ErrorType error,int badIndex=0) const;
public:
Array(int sz = 50);
Array(const ArrayT A);
~Array(void);
ArrayT operator= (const ArrayT rhs);
T operator[](int i);
operator T* (void) const;
int ListSize(void) const;
void Resize(int sz);
};;*;*;*;*;*;*;*;*;*;*;#include iostream
#include iomanip
#include 9_3.h
文档评论(0)