首页 > 其他 > 详细

类模板

时间:2015-07-01 17:27:41      阅读:185      评论:0      收藏:0      [点我收藏+]

#include<iostream>
using namespace std;
template<class T>
class compare{
public:
compare(T aa,T bb):a(aa),b(bb){}
void update1();
void update2();
void update3();

private:
T a,b;
};

template<class T>
void compare<T>::update1()
{
if(a<b)
{
cout<<b<<" is the Maximum of two inteder numbers."<<endl;
cout<<a<<" is the Minimum of two inteder numbers."<<endl;
}
else
{
cout<<a<<" is the Maximum of two inteder numbers."<<endl;
cout<<b<<" is the Minimum of two inteder numbers."<<endl;
}
cout<<endl;
}

template<class T>
void compare<T>::update2()
{
if(a<b)
{
cout<<b<<" is the Maximum of two float numbers."<<endl;
cout<<a<<" is the Minimum of two float numbers."<<endl;
}
else
{
cout<<a<<" is the Maximum of two float numbers."<<endl;
cout<<b<<" is the Minimum of two float numbers."<<endl;
}
cout<<endl;
}

template<class T>
void compare<T>::update3()
{
if(a<b)
{
cout<<b<<" is the Maximum of two characters."<<endl;
cout<<a<<" is the Minimum of two characters."<<endl;
}
else
{
cout<<a<<" is the Maximum of two characters."<<endl;
cout<<b<<" is the Minimum of two characters."<<endl;
}
}

int main(){
int x1,y1;
cin>>x1>>y1;
compare<int>ii(x1,y1);
ii.update1 ();

float x2,y2;
cin>>x2>>y2;
compare<float>ff(x2,y2);
ff.update2();

char x3[30],y3[30];
cin>>x3>>y3;
compare<char>cc(x3[1],y3[1]);
cc.update3();
return 0;
}

类模板

原文:http://www.cnblogs.com/zeross/p/4613368.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!