1 实验 09 群体类与群体数据的组织(4 学时)(第 9 章 群体类与群体数据的组织)一、实验目的(1) 掌握函数模板与类模板。(2) 了解线性群体与群体数据的组织。二、实验任务9_1 求绝对值的函数模板及其应用#include using namespace std; template T fun(T x) { return x < 0? -x : x; } int main() { int n = -5; double d = -5.5; cout << fun(n) << endl; cout << fun(d) << endl; return 0; } 《C++程序设计》课内实验03 数学与应用数学 2014 4 6 谭毓银曾悦16 2 9_2 函数模板的示例。#include using namespace std; template //定义函数模板void outputArray(const T *array, int count){ for (int i = 0; i < count; i++) cout << array[i] << " "; cout << endl; } int main(){ //主函数const int A_COUNT = 8, B_COUNT = 8, C_COUNT = 20; int a [A_COUNT] = { 1, 2, 3, 4, 5, 6, 7, 8 }; //定义 int 数组double b[B_COUNT] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8 }; //定义 double 数组char c[C_COUNT] = "Welcome to see you!"; //定义 char 数组cout << " a array contains:" << endl; outputArray(a, A_COUNT); //调用函数模板cout << " b array contains:" << endl; outputArray(b, B_COUNT); //调用函数模板cout << " c array contains:" << endl; outputArray(c, C_COUNT); //调用函数模板return 0; } 3 9_3 类模板应用举例。#include #include using namespace std; // 结构体 Student struct Student { int id; //学号float gpa; //平均分}; template class Store {//类模板:实现对任意类型数据进行存取private: T item; // item 用于存放任意类型的数据bool haveValue; // haveValue 标记 item 是否已被存入内容public: Store(); // 缺省形式(无形参)的构造函数T &getElem(); //提取数据函数void putElem(const T &x); //存入数据函数}; //以下实现各成员函数。template //缺省构造函数的实现Store::Store(): haveValue(false) { } template //提取数据函数的实...