#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; }
时间: 2023-08-20 20:31:42 浏览: 44
这段代码展示了如何使用类模板来创建一个通用的比较类。具体来说,代码中定义了一个类模板 `Compare`,该模板接受一个类型参数 `numtype`,并定义了两个数据成员 `x` 和 `y`,以及三个成员函数:构造函数 `Compare`、`max` 函数和 `min` 函数。构造函数接受两个参数,将它们分别赋值给 `x` 和 `y`。`max` 函数返回 `x` 和 `y` 中的较大值,`min` 函数返回 `x` 和 `y` 中的较小值。
在主函数中,分别使用 `Compare` 类模板创建了三个对象 `cmp1`、`cmp2` 和 `cmp3`,分别用于比较两个整数、两个浮点数和两个字符的大小关系。对于每个对象,分别调用它的 `max` 和 `min` 函数,并在屏幕上输出结果。
由于 `Compare` 类模板是通用的,可以用于比较不同类型的数据,因此在创建对象时需要指定模板参数的类型。在本例中,分别使用了 `int`、`float` 和 `char` 作为模板参数。
相关问题
#include<iostream> #include<cstdio> using namespace std;是什么意思
#include<iostream> #include<cstdio> using namespace std; 是C++中的预处理指令,用于引入头文件和命名空间。
1. #include<iostream> 是引入iostream头文件,其中包含了输入输出流的定义,例如cout和cin等。
2. #include<cstdio> 是引入cstdio头文件,其中包含了C语言标准输入输出函数的定义,例如printf和scanf等。
3. using namespace std; 是使用std命名空间,std是C++标准库的命名空间,其中包含了很多常用的函数和类。
这些预处理指令的作用是为了在程序中能够使用输入输出流和标准库函数,使得程序更加方便和简洁。
#include<iostream>using namespace std;
这是一个C++的代码片段,它包含了iostream头文件和使用了命名空间std。iostream头文件提供了输入输出流的功能,而using namespace std;语句则是为了方便使用std命名空间中的函数和对象,避免每次都需要写std::。
以下是一个简单的示例代码,展示了如何使用iostream头文件和命名空间std来输出一句话:
```cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
```
这段代码会输出"Hello, World!",endl是一个特殊的控制符,用于在输出流中插入一个换行符。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)