码迷,mamicode.com
首页 > 其他好文 > 详细

类模板

时间:2015-07-01 17:29:41      阅读:126      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!