#include <iostream> using namespace std; //模板函数 void mySwap(T &a, T &b){ T temp; temp = a; a = b; b = temp; cout<<"模板T"<<endl; } //模板类 template <typename T> class A { public: A(){} A(T a){ this ->a = a; } public: T a; }; template <typename T> class C:public A<T>{ public: C(T c,T a):A<T>(a){ this->c = c; } void tell(){ cout<<"a = "<<this->a<<";"<<"c = "<<c<<endl; } public: T c; }; int main(){ int a = 100; int b = 200; cout<<a<<b<<endl; mySwap(a, b); cout<<a<<b<<endl; C<int> c = C<int>(12,13); c.tell(); return 0; }template <typename T>
C++ 模板函数 和 模板类
最新推荐文章于 2024-08-30 19:50:32 发布
本文介绍了C++中模板函数和模板类的基本用法。通过一个简单的模板函数mySwap实现两个变量值的交换,并展示了如何定义及使用模板类A和其派生类C,最后实例化并调用了这些模板类。
1万+

被折叠的 条评论
为什么被折叠?



