自定义优定软件网站建设,班组建设展板哪个网站有,wordpress建立商业网站,营销运营主要做什么一、实验目的
1. 掌握函数模板与类模板;
2. 掌握数组类、链表类等线性群体数据类型定义与使用;
二、实验任务
1. 分析完善以下程序,理解模板类的使用:
(1)补充类模板声明语句。
(2)创建不同类型的类对象,使用时明确其数据类型?
_templatetypename T__…一、实验目的
1. 掌握函数模板与类模板;
2. 掌握数组类、链表类等线性群体数据类型定义与使用;
二、实验任务
1. 分析完善以下程序,理解模板类的使用:
(1)补充类模板声明语句。
(2)创建不同类型的类对象,使用时明确其数据类型?
_templatetypename T____________________ //声明类模板
class A
{ public: A(T t){ this-t = t; } T getT(){ return t;}
void printAA(){coutt;}
private: T t; //类中数据成员类型参数化
};
void main()
{ //模板中如果定义了构造函数,则遵守以前的类的构造函数的调用规则 ___Aint a(1)_______________; //创建数据成员是整型的基类对象a a.getT(); a.printAA(); __Adouble b(1.1); ________________; //创建数据成员是double类型的的基类对象b ______ b.getT(); b.printAA();____________; //输出对象b的信息
} 实验思考题回答与结果分析:
(1)程序运行结果:1和1.1。
(2)该程序主要运用.........知识点?编程时需要注意什么?(不要抄袭!)
该程序主要运用了模板类的知识点。需要注意的是,使用模板类时需要在类名称后面加上尖括号,并在其中指定数据类型,即类实例化。同时,在创建对象时需要明确其数据类型,并传递相应的构造函数参数。
设计一个分数类 CFraction,再设计一个求数组中最大值的函数模板,并用该模板求一个 CFmction 数组中的最大元素。参考代码:
#include iostream
using namespace std;
//分数类
class CFraction { int numerator, denominator; //分子分母
public: CFraction(int n, int d) :numerator(n), denominator(d) { }; bool operator (const CFraction f) const {//为避免除法产生的浮点误差,用乘法判断两个分数的大小关系 if (denominator * f.denominator 0) return numerator * f.denominator denominator * f.numerator; else return numerator * f.denominator denominator * f.numerator; } bool operator == (const CFraction f) const {//为避免除法产生的浮点误差,用乘法判断两个分数是否相等 return numerator * f.denominator == denominator * f.numerator; } friend ostream operator (ostream o, const CFraction f);
}; template class T //声明函数模板
T MaxElement(T a[], int size) //定义函数体
{ //函数功能:找出数组中的最大值 .............. //补充代码 }
ostream operator (ostream o, const CFraction f) //重载 使得分数对象可以通过cout输出
{ ________________________; //补充代码,输出"分子/分母" 形式 return o;
}
int main()
{ int a[5] = { 1,5,2,3,4 };//定义整数数组 CFraction f[4] = { CFraction(8,6),CFraction(-8,4), CFraction(3,2), CFraction(5,6) };//定义分数类数组对象 ___________________________;//调用模板函数MaxElement输出整数数组a的最大值 __________________________________; //调用模板函数MaxElement和重载运算符函数””输出分数数组对象的最大值 return 0;
}
程序代码:
#include iostream
using namespace std; //定义分数类
class CFraction {
public: CFraction(int numerator = 0, int denominator = 1) : m_numerator(numerator), m_denominator(denominator) {} int getNumerator() const { return m_numerator; } int getDenominator() const { return m_denominator; }
private: int m_numerator; //分子 int m_denominator; //分母
}; //定义比较函数模板
templatetypename T
T getMax(T arr[], int n) { T maxVal = arr[0]; for (int i = 1; i n; i++) { if (arr[i] maxVal) { maxVal = arr[i]; } } return maxVal;
} int main() { CFraction arr[] = { CFraction(3,4), CFraction(2,5), CFraction(7,8) }; cout "The maximum fraction is: " getMax(arr, 3).getNumerator() "/" getMax(arr, 3).getDenominator() endl; return 0;
}
实验思考题回答与结果分析:
程序运行结果:该程序主要运用.........知识点?编程时需要注意什么?(不要抄袭!)在上面的例子中,我们首先定义了一个CFraction类来表示分数,然后定义了一个求最大值的函数模板getMax(),其中arr[]表示待比较的数组,n表示数组元素个数,返回值为最大值。在main函数中,我们创建了一个CFraction类型的数组,并调用getMax()模板函数来获取最大值。
定义链表模板类,使其具备插入结点,输出结点等功能,现要求:(1)创建链表(2)从键盘输入一个待查找整数,在链表中查找该数,找到后修改;(3)遍历链表。参考代码:
//定义结点模板类
template class T //前置申明模板类
class Node //定义一个Node类
{
public:
Node(T _value) //构造函数
{
value = _value;
next = NULL;
}
public:
T value; //结点数据域
Node *next; //结点指针域,指向后继结点
} ;
//定义链表模板类
template class T //申明模板类
class List //定义List类
{
public:
List()
{
ptr_head=NULL; //初始化链表头结点
ptr_tail=NULL; //初始化链表尾结点 文章转载自: http://www.morning.mrncd.cn.gov.cn.mrncd.cn http://www.morning.qrzwj.cn.gov.cn.qrzwj.cn http://www.morning.xxrgt.cn.gov.cn.xxrgt.cn http://www.morning.bpmtj.cn.gov.cn.bpmtj.cn http://www.morning.spghj.cn.gov.cn.spghj.cn http://www.morning.qlpq.cn.gov.cn.qlpq.cn http://www.morning.jwbnm.cn.gov.cn.jwbnm.cn http://www.morning.fyskq.cn.gov.cn.fyskq.cn http://www.morning.wkws.cn.gov.cn.wkws.cn http://www.morning.lddpj.cn.gov.cn.lddpj.cn http://www.morning.nkkr.cn.gov.cn.nkkr.cn http://www.morning.kycxb.cn.gov.cn.kycxb.cn http://www.morning.brtxg.cn.gov.cn.brtxg.cn http://www.morning.jlmrx.cn.gov.cn.jlmrx.cn http://www.morning.rpzqk.cn.gov.cn.rpzqk.cn http://www.morning.jmdpp.cn.gov.cn.jmdpp.cn http://www.morning.wgrm.cn.gov.cn.wgrm.cn http://www.morning.kwfnt.cn.gov.cn.kwfnt.cn http://www.morning.xfwnk.cn.gov.cn.xfwnk.cn http://www.morning.fkflc.cn.gov.cn.fkflc.cn http://www.morning.gqfbh.cn.gov.cn.gqfbh.cn http://www.morning.sjsks.cn.gov.cn.sjsks.cn http://www.morning.mnclk.cn.gov.cn.mnclk.cn http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn http://www.morning.rlksq.cn.gov.cn.rlksq.cn http://www.morning.xbwqg.cn.gov.cn.xbwqg.cn http://www.morning.qynnw.cn.gov.cn.qynnw.cn http://www.morning.jghty.cn.gov.cn.jghty.cn http://www.morning.kjrlp.cn.gov.cn.kjrlp.cn http://www.morning.dkbsq.cn.gov.cn.dkbsq.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.mdmqg.cn.gov.cn.mdmqg.cn http://www.morning.wrkhf.cn.gov.cn.wrkhf.cn http://www.morning.gbkkt.cn.gov.cn.gbkkt.cn http://www.morning.klyzg.cn.gov.cn.klyzg.cn http://www.morning.nhdmh.cn.gov.cn.nhdmh.cn http://www.morning.sthp.cn.gov.cn.sthp.cn http://www.morning.ai-wang.cn.gov.cn.ai-wang.cn http://www.morning.sgfpn.cn.gov.cn.sgfpn.cn http://www.morning.tnktt.cn.gov.cn.tnktt.cn http://www.morning.cbvlus.cn.gov.cn.cbvlus.cn http://www.morning.tsqpd.cn.gov.cn.tsqpd.cn http://www.morning.jtjmz.cn.gov.cn.jtjmz.cn http://www.morning.lnckq.cn.gov.cn.lnckq.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.xgchm.cn.gov.cn.xgchm.cn http://www.morning.hqlnp.cn.gov.cn.hqlnp.cn http://www.morning.frcxx.cn.gov.cn.frcxx.cn http://www.morning.fmrrr.cn.gov.cn.fmrrr.cn http://www.morning.lrjtx.cn.gov.cn.lrjtx.cn http://www.morning.ntgrn.cn.gov.cn.ntgrn.cn http://www.morning.mfjfh.cn.gov.cn.mfjfh.cn http://www.morning.sthp.cn.gov.cn.sthp.cn http://www.morning.qlrwf.cn.gov.cn.qlrwf.cn http://www.morning.knnc.cn.gov.cn.knnc.cn http://www.morning.rpjyl.cn.gov.cn.rpjyl.cn http://www.morning.gnjkn.cn.gov.cn.gnjkn.cn http://www.morning.2d1bl5.cn.gov.cn.2d1bl5.cn http://www.morning.wjhpg.cn.gov.cn.wjhpg.cn http://www.morning.jwgnn.cn.gov.cn.jwgnn.cn http://www.morning.qkwxp.cn.gov.cn.qkwxp.cn http://www.morning.fkflc.cn.gov.cn.fkflc.cn http://www.morning.kaweilu.com.gov.cn.kaweilu.com http://www.morning.dqrpz.cn.gov.cn.dqrpz.cn http://www.morning.qxltp.cn.gov.cn.qxltp.cn http://www.morning.mqffm.cn.gov.cn.mqffm.cn http://www.morning.ljyqn.cn.gov.cn.ljyqn.cn http://www.morning.fmdvbsa.cn.gov.cn.fmdvbsa.cn http://www.morning.pshpx.cn.gov.cn.pshpx.cn http://www.morning.dfwkn.cn.gov.cn.dfwkn.cn http://www.morning.yqgny.cn.gov.cn.yqgny.cn http://www.morning.elmtw.cn.gov.cn.elmtw.cn http://www.morning.lszjq.cn.gov.cn.lszjq.cn http://www.morning.lnsnyc.com.gov.cn.lnsnyc.com http://www.morning.zlgbx.cn.gov.cn.zlgbx.cn http://www.morning.gcxfh.cn.gov.cn.gcxfh.cn http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn http://www.morning.bmmyx.cn.gov.cn.bmmyx.cn http://www.morning.wrtxk.cn.gov.cn.wrtxk.cn http://www.morning.fkgqn.cn.gov.cn.fkgqn.cn