- /*
- 类模板,但是在类外定义成员函数的时候,需要使用函数模板
- */
- #include <iostream>
- using namespace std ;
- template <class T>
- class Base
- {
- public :
- T a ;
- Base(T b)
- {
- a = b ;
- }
- T getA(){ return a ;} //类内定义
- void setA(T c);
- };
- template <class T> //模板在类外的定义
- void Base<T>::setA(T c)
- {
- a = c ;
- }
- int main(void)
- {
- Base <int>b(4);
- cout<<b.getA()<<endl;
- Base <double> bc(4);
- bc.setA(4.3);
- cout<<bc.getA()<<endl;
- system("pause");
- return 0 ;
- }
模板类的使用- 简单使用
最新推荐文章于 2023-09-05 11:14:16 发布