厦大计算机系C++程序设计实验(五)
2018-01-20 14:30
351 查看
1 实验目的
模板使类和函数可在编译时定义所需处理和返回的数据类型,一个模板并非一个实实在在的类或函数,仅仅是一个类和函数的描述。由于模板可以实现逻辑相同、数据类型不同的程序代码复制,所以使用模板机制可以减轻编程和维护的工作量和难度。模板一般分为模板函数和类模板。以所处理的数据类型的说明作为参数的类就叫类模板,或者模板类,而以所处理的数据类型的说明作为参数的函数,则称为函数模板。
本实验通过编写一些简单的程序,使学生掌握泛型编程,并能够基本运用模板的语法,培养理论联系实际的和自主学习的能力,提高程序设计水平。
2 实验内容
(1)编写模板函数,它将一个包含5个T类型元素的数组作为参数,并返回数组的最小值。在一个程序中使用该函数,将T替换为一个包含5个int值的数组和一个包含5个double值的数组,以测试该函数。
(2)定义一个模板类(BinaryOperation),包含构造函数BinaryOperation(T x, T y)以及方法add、sub、mul、div、operator(char op),通过调用operator,判断传入的op变量的值,如果是“+”,则进行加法,并输出结果,其他方法类似。
例:BinaryOperation bo = new BinaryOperation(1,1);
bo.operator(’+’);会输出结果2
bo.operator(’-’);会输出结果0
3 实验步骤
(1)模板函数
首先编写一个寻找T类型数组的最小值的函数,然后在主函数内对int型和double型数组分别进行调用,输出数组的最小值。
源代码如下:
(2)类模板
首先声明一个BinaryOperation的模板类,包括数据域x和y,以及题目要求的成员函数。由于题目中要求计算结果通过函数Operator获得,所以add、sub、mul、div函数通通设置为私有成员。在主函数中实例化一个int型和一个double型的实例,并进行测试。
源代码如下:
4 实验结果
(1)模板函数
若int型数组内数据为3, 8, 1, 4, 9,而double型数组内数据为4.3, 2.9, 1.4, 5.5, 8.3。输出结果为:
(2)类模板
若int型实例内数据为1, 1,double型实例内数据为3.0, 1.5。输出结果为:
5 心得小结
通过本次实验,我掌握了C++中泛型编程的模式,学会了如何定义并使用模板函数和模板类。在实际编程中,需要注意的是类构造函数的类名后面必须跟着<实际类型名>,否则将会报错。
这是本学期C++的最后一次实验了,通过一学期的学习,我了解了C++语言,提高了编程能力。但是,我距离高水平的C++编程还有很长的路要走,需要我不断学习,提高自我。
模板使类和函数可在编译时定义所需处理和返回的数据类型,一个模板并非一个实实在在的类或函数,仅仅是一个类和函数的描述。由于模板可以实现逻辑相同、数据类型不同的程序代码复制,所以使用模板机制可以减轻编程和维护的工作量和难度。模板一般分为模板函数和类模板。以所处理的数据类型的说明作为参数的类就叫类模板,或者模板类,而以所处理的数据类型的说明作为参数的函数,则称为函数模板。
本实验通过编写一些简单的程序,使学生掌握泛型编程,并能够基本运用模板的语法,培养理论联系实际的和自主学习的能力,提高程序设计水平。
2 实验内容
(1)编写模板函数,它将一个包含5个T类型元素的数组作为参数,并返回数组的最小值。在一个程序中使用该函数,将T替换为一个包含5个int值的数组和一个包含5个double值的数组,以测试该函数。
(2)定义一个模板类(BinaryOperation),包含构造函数BinaryOperation(T x, T y)以及方法add、sub、mul、div、operator(char op),通过调用operator,判断传入的op变量的值,如果是“+”,则进行加法,并输出结果,其他方法类似。
例:BinaryOperation bo = new BinaryOperation(1,1);
bo.operator(’+’);会输出结果2
bo.operator(’-’);会输出结果0
3 实验步骤
(1)模板函数
首先编写一个寻找T类型数组的最小值的函数,然后在主函数内对int型和double型数组分别进行调用,输出数组的最小值。
源代码如下:
#include <iostream> using namespace std; template <typename T> T getMin(T a[5]) { T min = a[0]; for(int i = 1; i < 5; ++i) { if(a[i] < min) min = a[i]; } return min; } int main() { int intA[5] = {3, 8, 1, 4, 9}; double doubleA[5] = {4.3, 2.9, 1.4, 5.5, 8.3}; cout << "The smallest element in intA is " << getMin(intA) << endl; cout << "The smallest element in doubleA is " << getMin(doubleA) << endl; return 0; }
(2)类模板
首先声明一个BinaryOperation的模板类,包括数据域x和y,以及题目要求的成员函数。由于题目中要求计算结果通过函数Operator获得,所以add、sub、mul、div函数通通设置为私有成员。在主函数中实例化一个int型和一个double型的实例,并进行测试。
源代码如下:
#include <iost 4000 ream> using namespace std; template <class T> class BinaryOperation { T x, y; public: BinaryOperation(T x, T y); T Operator(char op); private: T add(); T sub(); T mul(); T div(); }; template <class T> BinaryOperation<T>::BinaryOperation(T x, T y) { this->x = x; this->y = y; } template <class T> T BinaryOperation<T>::add() { return (this->x + this->y); } template <class T> T BinaryOperation<T>::sub() { return (this->x - this->y); } template <class T> T BinaryOperation<T>::mul() { return (this->x * this->y); } template <class T> T BinaryOperation<T>::div() { return (this->x / this->y); } template <class T> T BinaryOperation<T>::Operator(char op) { switch(op) { case '+': return add(); break; case '-': return sub(); break; case '*': return mul(); break; case '/': return div(); break; default: return 0; } } int main() { BinaryOperation<int> intBO(1, 1); BinaryOperation<double> doubleBO(3.0, 1.5); cout << "int type: " << endl; cout << "add: " << intBO.Operator('+') << endl; cout << "subtract: " << intBO.Operator('-') << endl; cout << "multiply: " << intBO.Operator('*') << endl; cout << "divide: " << intBO.Operator('/') << endl; cout << endl; cout << "double type: " << endl; cout << "add: " << doubleBO.Operator('+') << endl; cout << "subtract: " << doubleBO.Operator('-') << endl; cout << "multiply: " << doubleBO.Operator('*') << endl; cout << "divide: " << doubleBO.Operator('/') << endl; cout << endl; return 0; }
4 实验结果
(1)模板函数
若int型数组内数据为3, 8, 1, 4, 9,而double型数组内数据为4.3, 2.9, 1.4, 5.5, 8.3。输出结果为:
(2)类模板
若int型实例内数据为1, 1,double型实例内数据为3.0, 1.5。输出结果为:
5 心得小结
通过本次实验,我掌握了C++中泛型编程的模式,学会了如何定义并使用模板函数和模板类。在实际编程中,需要注意的是类构造函数的类名后面必须跟着<实际类型名>,否则将会报错。
这是本学期C++的最后一次实验了,通过一学期的学习,我了解了C++语言,提高了编程能力。但是,我距离高水平的C++编程还有很长的路要走,需要我不断学习,提高自我。
相关文章推荐
- 厦大计算机系C++程序设计实验(三)
- 厦大计算机系C++程序设计实验(四)
- 厦大计算机系C++程序设计实验(一)
- 厦大计算机系C++程序设计实验(二)
- 厦大计算机系Java程序设计实验(三)
- 厦大计算机系Java程序设计实验(四)
- 厦大计算机系Java程序设计实验(五)
- 厦大计算机系Java程序设计实验(一)
- 厦大计算机系Java程序设计实验(二)
- C++程序设计实验报告(五)
- C++程序设计实验报告(8-1)
- C++程序设计实验报告【5.1.4】
- C++程序设计实验报告(六十一)---第九周任务5
- C++程序设计实验报告(9-2)
- C++程序设计实验报告(六)
- C++简单程序设计实验-1
- C++程序设计实验报告(十八)----从文件中读入数据,排序并输出到另外一个文件中
- C++第2次实验(提高班)—选择循环程序设计 :项目2、项目6
- c++程序设计——实验6
- C++程序设计实验报告(二十八)---第二周任务二