美文网首页
数据结构(类模板使用)

数据结构(类模板使用)

作者: ZaleJ | 来源:发表于2017-03-13 20:43 被阅读0次

《数据结构》

实验报告

题目:_类模板的定义与应用

学号:_____88888888___

姓名:____Coder_J______

东南大学计算机系

实验题目

一、实验目的

1.掌握类模板的声明方法和对象的定义及使用。

二、实验内容

1、设计一个类模板,其中包括数据成员num1和num2,以及实现两个数据交换的函数。定义类对象,完成交换两个整数、两个双精度浮点数、两个字符以及两个字符串的程序。

2、设计一个类模板,其中包括数据成员T a[n],以及在其中进行查找数据元素的函数

int  search(T)

定义类对象,完成在整型数组、浮点型数组、字符数组中查找元素。

三、实验结果

1.实验代码

#include

#include

#include

#define N 3

using namespace std;

template

class Swap{

public :

Swap(numtype a,numtype b){x=a;y=b;}

numtype ___(){

temp=x;

x=y;

y=temp;

return x;

}

//test

numtype ____(){

return x;

}

//test

private :

numtype x,y;

numtype temp;

};

template

class Seh_arr{

public:

Seh_arr(numtypeeee a[N]){

for(int i = 0; i < N; ++i){

*(x+i)=*(a+i);

}

}

numtypeeee seh(){

cout << "input the Number of the element you want:";

cin >> T;

return x[T-1];

}

private:

numtypeeee x[N];

int T;

};

int main(void){

Swap wap1(3,7);

cout<

Swap wap2(45.78,93.6);

cout<

Swap wap3('a','A');

cout<

Swap wap4("nice", "good");

cout<

int a[N]={1,2,3};

Seh_arr_testInt(a);

cout << _testInt.seh() << "--search int."<< endl;

float b[N]={1.1, 2.2, 3.3};

Seh_arr_testFlo(b);

cout << _testFlo.seh() << "--search float." << endl;

char c[N]={'a', 'b', 'c'};

Seh_arr_testCha(c);

cout << _testCha.seh() << "--search char." << endl;

string d[N]={"my", "name", "is"};

Seh_arr_testStr(d);

cout << _testStr.seh() << "--search string." << endl;

getch();

return 0;

}

2.实验效果

XXXXXX

相关文章

  • 数据结构(类模板使用)

    《数据结构》 实验报告 题目:_类模板的定义与应用 学号:_____88888888___ 姓名:____Code...

  • STL容器 算法 迭代器 适配器

    STL包括容器,算法和迭代器 STL的模板类为c++提供了完善的数据结构,它的模板类的样式就好象数据结构中用类c或...

  • 模板方法模式

    模板抽象类 模板抽象类实现类 使用

  • C++ 中的模板

    类模板 函数模板 成员模板(member template) 成员模板其实就是一个类里面使用了一个模板函数。使用模...

  • [C++]模板类的使用(Stack)

    模板类的使用 测试类

  • D面向对象

    ###内存管理 类:对象的模板,是对象的数据结构定义 对象:利用类作为模板创建的个体实例,本质是数据。 引用:是对...

  • 模板

    使用场景:设计单例类的时候,可以设计通用的单例类,使用模板。 KCBP中使用模板的地方:

  • 五、(三)持久层框架——JDBC

    使用JDBC 使用JDBC模板 使用DAO支持类

  • 使用类模板

    #include #include #include template struct MksUn...

  • 第1章 变量和类型

    1.类型 1.1什么是类型 类型可以看成用来创建数据结构的模板,但模板本身不是数据结构.所以在定义类型以后,要使用...

网友评论

      本文标题:数据结构(类模板使用)

      本文链接:https://www.haomeiwen.com/subject/urmqnttx.html