1、类模板使用举例
#include <iostream>
using namespace std;
/* 定义类模板 */
template <class T1, class T2 = int> // 默认模板参数类型设置为int
class MyTemplate
{
private:
T1 t1;
T2 t2;
public:
/* 构造函数 */
MyTemplate(T1 x, T2 y)
{
t1 = x;
t2 = y;
}
/*成员函数*/
void display()
{
cout << "t1="<<t1 << " " << "t2="<< t2<< endl;
}
};
int main(int argc, char *argv[])
{
int a = 123;
double b = 10.12;
MyTemplate<int, double> mt1(a, b); //通过MyTemplate<int, double>定义对象mt, 传递参数a和b
MyTemplate<int> mt2(a,b); // 第二个参数采用默认参数类型int
mt1.display();
mt2.display();
}
| xuanmiao@linux:~/Test/C++$ ./test t1=123 t2=10.12 t1=123 t2=10 |
2、参数默认值
#include <iostream>
using namespace std;
template <typename T1, typename T2, int defaultNum = 10>
class MyTemplate
{
private:
T1 t1;
T2 t2;
public:
MyTemplate(T1 x, T2 y)
{
t1 = x + defaultNum;
t2 = y + defaultNum;
}
void display()
{
cout <<"t1="<<t1<<" "<<"t2="<<t2<<endl;
}
};
int main(int argc, char *argv[])
{
int a = 10;
double b = 3.15;
MyTemplate<int , double> mt(a,b); //defaultNum采取默认参数10
MyTemplate<int , double, 100> mt2(a,b); //defaultNum采取设置参数100
mt.display();
mt2.display();
}
| xuanmiao@linux:~/Test/C++$ ./test t1=20 t2=13.15 t1=110 t2=103.15 |
3、通用模板类的使用
#include <iostream>
using namespace std;
/* 普通类 */
class Date
{
int iMonth, iDay, iYear;
char Format[128];
public:
Date(int m=0, int d=0, int y=0)
{
iMonth = m;
iDay = d;
iYear = y;
}
void display()
{
cout << "Month=" << iMonth<<" ";
cout << "Day=" << iDay<<" ";
cout << "Year=" << iYear<<" ";
cout << endl;
}
friend ostream& operator << (ostream& os, const Date t)
{
cout << "Month=" << t.iMonth << " ";
cout << "Day=" << t.iDay << " ";
cout << "Year=" << t.iYear << " ";
cout << endl;
return os;
}
};
/* 通用模板类 */
template <class T>
class Set
{
T t;
public:
Set(T st) : t(st)
{
}
void Display()
{
cout << t << endl;
}
};
int main(int argc, char * argv[ ])
{
cout << endl << "通过普通类Date定义对象date,通过display函数展示:"<< endl;
Date date(5,6,2025); // 创建Date类对象date
date.display();
cout << endl << "通过通用模板类Set<Date>定义对象sdate:" << endl;
Set<Date> sdate = date; // 将Date类对象赋值给Set<Date>类对象sdate
sdate.Display();
return 0;
}
|
xuanmiao@linux:~/Test/C++$ ./test 通过普通类Date定义对象date,通过display函数展示: 通过通用模板类Set<Date>定义对象sdate,通过友元运算符展示: |
4、定制模板类成员函数
#include <iostream>
using namespace std;
/* 普通类 */
class Date
{
int iMonth, iDay, iYear;
char Format[128];
public:
Date(int m=0, int d=0, int y=0)
{
iMonth = m;
iDay = d;
iYear = y;
}
void display()
{
cout << "Month=" << iMonth<<" ";
cout << "Day=" << iDay<<" ";
cout << "Year=" << iYear<<" ";
cout << endl;
}
friend ostream& operator << (ostream& os, const Date t)
{
cout << "Month=" << t.iMonth << " ";
cout << "Day=" << t.iDay << " ";
cout << "Year=" << t.iYear << " ";
cout << endl;
return os;
}
};
/* 通用模板类 */
template <typename Type>
class Set
{
Type t;
public:
Set(Type input) : t(input) {};
void templateDisplay();
};
/* 定义Date类的Display函数 */
template<>
void Set<Date>::templateDisplay()
{
cout <<"Date:"<< t << endl;
}
/* 主程序 */
int main(int argc, char * argv[ ])
{
cout << endl << "通过普通类Date定义对象date,通过display函数展示:"<< endl;
Date date(5,6,2025); // 创建Date类对象date
date.display();
cout << endl << "通过通用模板类Set<Date>定义对象sdate,通过templateDisplay函数展示:" << endl;
Set<Date> sdate = date; // 将Date类对象赋值给Set<Date>类对象sdate
sdate.templateDisplay();
return 0;
}
|
xuanmiao@linux:~/Test/C++$ ./test 通过普通类Date定义对象date,通过display函数展示: 通过通用模板类Set<Date>定义对象sdate,通过templateDisplay函数展示: |
类模板
浙公网安备 33010602011771号