欢迎来到天天文库
浏览记录
ID:38699064
大小:248.00 KB
页数:9页
时间:2019-06-17
《实验八、函数模板与类模板的应用》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、南昌大学实验报告学生姓名:学号:专业班级:实验类型:■验证□综合□设计□创新实验日期:实验成绩:一、实验项目名称函数模板与类模板的应用二、实验目的1、掌握模板的概念与应用2、掌握函数模板的定义和使用方法3、掌握多个模板参数的类模板的定义和使用方法4、掌握泛型程序设计的基本方法三、实验任务利用VC++6.0编写程序,然后编译、运行,并查看结果是否正确。题目1、3、4(p211)四、主要仪器设备及耗材软件:visualc++五、实验步骤1、阅读实验指导中实验8的相关内容,了解实验目的和实验要求。2、根据实验指导把每个
2、要解决的问题罗列出来。3、根据要求分别编写代码,上机实验,查错,并将结果截图。六、实验数据及处理结果题目一:(1)代码:#include#includetemplateTMax(Tvalue[],intsize){Tmax=value[0];Ttemp;for(inti=0;imax){temp=max;max=value[i];value[i]=temp;}returnmax;}constintN=5;#in
3、clude"exp8_1.h"intmain(){inti,arr[N];doubledou[N];cout<<"输入"<>arr[i];cout<<"最大值为"<>dou[i];cout<<"最大值为"<4、templateclassTest{public:Test(T1x,T2y,T3z){data1=x;data2=y;data3=z;}voidDisplay();private:T1data1;T2data2;T3data3;};templatevoidTest::Display(){cout<5、;}#include"exp8_2.h"intmain(){Testobj1(1,2,3.3);obj1.Display();Testobj2('A',1.1,2.2);obj2.Display();Testobj3("C++",98,"分");obj3.Display();return0;}(1)结果:题目三:(1)代码:#includeusingnamespacestd;templat6、eclassComplex{public:Complex(T1r,T2i);voidPrint();Complexoperator+(constComplex&a);Complexoperator-(constComplex&a);private:T1real;T2imag;};//构造函数templateComplex::Complex(T1r,T2i){real=r;imag=i;}//打印函数template7、classT2>voidComplex::Print(){cout<0)cout<<"+";cout<ComplexComplex::operator+(constComplex&a){real+=a.real;imag+=a.imag;return*this;}//以成员函数形式重载运算符-t8、emplateComplexComplex::operator-(constComplex&a){real-=a.real;imag-=a.imag;return*this;}#include"exp8_3.h"intmain(){ComplexA1(1,2);A1.Print();Compl
4、templateclassTest{public:Test(T1x,T2y,T3z){data1=x;data2=y;data3=z;}voidDisplay();private:T1data1;T2data2;T3data3;};templatevoidTest::Display(){cout<5、;}#include"exp8_2.h"intmain(){Testobj1(1,2,3.3);obj1.Display();Testobj2('A',1.1,2.2);obj2.Display();Testobj3("C++",98,"分");obj3.Display();return0;}(1)结果:题目三:(1)代码:#includeusingnamespacestd;templat6、eclassComplex{public:Complex(T1r,T2i);voidPrint();Complexoperator+(constComplex&a);Complexoperator-(constComplex&a);private:T1real;T2imag;};//构造函数templateComplex::Complex(T1r,T2i){real=r;imag=i;}//打印函数template7、classT2>voidComplex::Print(){cout<0)cout<<"+";cout<ComplexComplex::operator+(constComplex&a){real+=a.real;imag+=a.imag;return*this;}//以成员函数形式重载运算符-t8、emplateComplexComplex::operator-(constComplex&a){real-=a.real;imag-=a.imag;return*this;}#include"exp8_3.h"intmain(){ComplexA1(1,2);A1.Print();Compl
5、;}#include"exp8_2.h"intmain(){Testobj1(1,2,3.3);obj1.Display();Testobj2('A',1.1,2.2);obj2.Display();Testobj3("C++",98,"分");obj3.Display();return0;}(1)结果:题目三:(1)代码:#includeusingnamespacestd;templat
6、eclassComplex{public:Complex(T1r,T2i);voidPrint();Complexoperator+(constComplex&a);Complexoperator-(constComplex&a);private:T1real;T2imag;};//构造函数templateComplex::Complex(T1r,T2i){real=r;imag=i;}//打印函数template7、classT2>voidComplex::Print(){cout<0)cout<<"+";cout<ComplexComplex::operator+(constComplex&a){real+=a.real;imag+=a.imag;return*this;}//以成员函数形式重载运算符-t8、emplateComplexComplex::operator-(constComplex&a){real-=a.real;imag-=a.imag;return*this;}#include"exp8_3.h"intmain(){ComplexA1(1,2);A1.Print();Compl
7、classT2>voidComplex::Print(){cout<0)cout<<"+";cout<ComplexComplex::operator+(constComplex&a){real+=a.real;imag+=a.imag;return*this;}//以成员函数形式重载运算符-t
8、emplateComplexComplex::operator-(constComplex&a){real-=a.real;imag-=a.imag;return*this;}#include"exp8_3.h"intmain(){ComplexA1(1,2);A1.Print();Compl
此文档下载收益归作者所有