數據結構(類模板使用)

《數據結構》

實驗報告

題目:_類模板的定義與應用

學號:_____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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容