将例3.14改写为在类模板外定义各成员函数。C++

2016-06-25 22:34:57  分类: c++程序设计第三版谭浩强课后答案  参与:

将例3.14改写为在类模板外定义各成员函数。

c++设计(第3版)题解与上机指导 谭浩强 编著
谭浩强c++程序设计第3版答案

以下是此题的【c++源代码】
#include <iostream>
using namespace std;
template<class numtype>
class Compare
 {public:
   Compare(numtype a,numtype b);
   numtype max();
   numtype min();
  private:
   numtype x,y;
 };
template <class numtype>
Compare<numtype>::Compare(numtype a,numtype b)
  {x=a;y=b;}
template <class numtype>
numtype Compare<numtype>::max()
 {return (x>y)?x:y;}
template <class numtype>
numtype Compare<numtype>::min()
  {return (x<y)?x:y;}

int main()
{Compare<int> cmp1(3,7);
 cout<<cmp1.max()<<" is the Maximum of two integer numbers."<<endl;
 cout<<cmp1.min()<<" is the Minimum of two integer numbers."<<endl<<endl;
 Compare<float> cmp2(45.78,93.6);
 cout<<cmp2.max()<<" is the Maximum of two float numbers."<<endl;
 cout<<cmp2.min()<<" is the Minimum of two float numbers."<<endl<<endl;
 Compare<char> cmp3('a','A');
 cout<<cmp3.max()<<" is the Maximum of two characters."<<endl;
 cout<<cmp3.min()<<" is the Minimum of two characters."<<endl;
 return 0;
}

来源:c++程序设计第三版谭浩强课后答案

本文链接:http://www.wb98.com/cjia/post/cjia_9.12.html


本站文章搜索:

<< 上一篇下一篇 >>

搜索

Tags列表

赞助商链接