收藏 分销(赏)

C--实验六-函数模板-类模板和标准模板库.doc

上传人:仙人****88 文档编号:11959040 上传时间:2025-08-22 格式:DOC 页数:6 大小:55KB 下载积分:10 金币
下载 相关 举报
C--实验六-函数模板-类模板和标准模板库.doc_第1页
第1页 / 共6页
C--实验六-函数模板-类模板和标准模板库.doc_第2页
第2页 / 共6页


点击查看更多>>
资源描述
福 建 工 程 学 院 实 验 报 告 面向对象程序设计(C++) 专 业 电子信息工程 班 级 1303 座 号 姓 名 日 期 2015-12-01 实验六 函数模板,类模板和标准模板库 一、 实验目的: 掌握函数模板、类模板的格式、语法及应用。 掌握标准模板库的应用。 二、 实验时间:2015-12-01 三、 实验地点:C2-101 四、 实验内容: 1.编写函数模板: 编写一求两个数的最大值的函数Max(),要求用模板实现对任意数据类型数据都可应用该函数求取结果,在main()函数中分别用整型、实型、字符型数据进行测试。 #include <iostream> using namespace std; template <typename T> T Max (T x, T y) { return x>y?x:y; } int main() { int x1=10,x2=20; double d1=15.123,d2=19.0321; char c1='a',c2='y'; cout<<"Max(10,20):"<<Max(x1,x2)<<endl; cout<<"Max(15.123,19.0321):"<<Max(d1,d2)<<endl; cout<<"Max('a','y'):"<<Max(c1,c2)<<endl; return 0; } 2.编写一冒泡排序的函数模板: 函数的定义为: template <typename ElementType > void SortBubble ( ElementType *a , int size ),完成该函数并在main()中调用。 #include <iostream> using namespace std; template <typename ElementType > void SortBubble ( ElementType *a , int size ) { int i, work ; ElementType temp ; for (int pass = 1; pass < size; pass ++ ) { work = 1; for ( i = 0; i < size-pass; i ++ ) if ( a[i] > a[i+1] ) { temp = a[i] ; a[i] = a[i+1] ; a[i+1] = temp ; work = 0 ; } if ( work ) break ; } } int main() { int arr[5] = {3,12,2,10,0}; SortBubble(arr,5); cout<<arr[0]<<" "<<arr[1]<<" "<<arr[2]<<" "<<arr[3]<<" "<<arr[4]<<" "<<endl; double dArr[4] = {1.20,5.62,3.012,8.101}; SortBubble(dArr,4); cout<<dArr[0]<<" "<<dArr[1]<<" "<<dArr[2]<<" "<<dArr[3]<<" "<<endl; char cArr[3] = {'s','d','w'}; SortBubble(cArr,3); cout<<cArr[0]<<" "<<cArr[1]<<" "<<cArr[2]<<" "<<endl; return 0; } 3. 类模板操作: 有以下类模板 template <class T> //类模板:实现对任意类型数据进行存取 class Store { private: T item; // 用于存放任意类型的数据 int haveValue; // 用于标记item是否已被存入内容 public: Store(void); // 默认形式(无形参)的构造函数 T GetElem(void); //提取数据函数 void PutElem(T x); //存入数据函数 }; 请实现并测试该类。 #include <iostream> #include <string> using namespace std; struct People { string name; int age; }; class Dog { public: string name; public: Dog() {} Dog(string n):name(n) {} void wang() {cout<<"Wang Wang。。。";} }; 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> void Store<T>::PutElem(T x) { haveValue=1; item = x; } template<class T> T Store<T>::GetElem(void) { if(haveValue == 0) { cout<<"No item present!"<<endl; exit(1); } return item; } int main() { //int 类型 Store<int> i1,i2; i1.PutElem(5); i2.PutElem(999); cout<<i1.GetElem()<<" "<<i2.GetElem()<<endl; //People 类型 People p = {"Tom",20}; Store<People>ps; ps.PutElem(p); cout<<"Name is:"<<ps.GetElem().name<<" age is:"<<ps.GetElem().age<<endl; //Dog 类型 Dog d("snoopy"); Store<Dog>ds; ds.PutElem(d); cout<<"Dog's name is :"<<ds.GetElem().name<<endl; //double 类型 Store<double>d1,d2; d1.PutElem(3.14159); cout<<d1.GetElem()<<endl; cout<<d2.GetElem()<<endl; return 0; } 4. 应用标准模板库的容器进行编程: 类A的定义如下,对<进行重载,请补全该类,并在main()中测试<对容器对象的应用程序: include <vector> #include <iostream> class A { private: int n; public: friend bool operator < (const A &, const A &); A(int n_ ) { n = n_ ; } }; 五、 实验要求: 在上机前写出全部源程序; 六、实验结果分析: 1, 模板函数: 2, 冒泡排序: 3, 对模板类进行不同数据类型的测试,结果如下: 七、实验心得体会及问题反馈: 1、 对模板函数,模板类加深理解并掌握使用方法。 2、 加深了C++泛型编程的理解,编写的程序更加通用。 教师评定: 签字: 年 月 日
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服