欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

模板类的使用

发布时间:2025/4/16 31 豆豆
生活随笔 收集整理的这篇文章主要介绍了 模板类的使用 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

使用模板类,可以使用各种的数据成员对函数进行操作,
模板类的形式
tempalte

#include<iostream> using namespace std; template<class T> class compare { public:compare(T a, T b);T min();T max(); private:T num1;T num2; }; template<class T> compare<T>::compare(T a, T b) {num1 = a;num2 = b; } template<class T> T compare<T>::min() {if (num1 > num2)return num2;elsereturn num1;} template<class T> T compare<T>::max() {if (num1 > num2)return num1;elsereturn num2; }int main() {compare<int> com1(20, 10);cout << "the min of the number" << com1.min() << endl;cout << "the max of the number" << com1.max() << endl;compare < double> com2(15.6, 13.8);cout << "the min of the min number" << com2.min() << endl;cout << "the max of the number" << com2.max() << endl;system("pause");return 0; }

总结

以上是生活随笔为你收集整理的模板类的使用的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。