45 lines
662 B
C++
45 lines
662 B
C++
// 类模版类的外部实现
|
|
// 类外部实现成员函数时,需要指定类泛型的类型
|
|
// 再次声明泛型类型(转成函数模版)
|
|
|
|
#include <iostream>
|
|
#include <cstring>
|
|
#include <cstdlib>
|
|
|
|
using namespace std;
|
|
|
|
template <typename T>
|
|
class Point
|
|
{
|
|
private:
|
|
T x, y;
|
|
|
|
public:
|
|
Point(T x, T y);
|
|
void show();
|
|
};
|
|
|
|
template <typename T>
|
|
Point<T>::Point(T x, T y) : x(x), y(y)
|
|
{
|
|
}
|
|
|
|
template <typename T>
|
|
void Point<T>::show()
|
|
{
|
|
cout << "x = " << x << ", y = " << y << endl;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
cout << "p1: ";
|
|
Point<int> p1(10, 20);
|
|
p1.show();
|
|
|
|
cout << "p2: ";
|
|
Point<float> p2(10.2, 14.5);
|
|
p2.show();
|
|
|
|
return 0;
|
|
}
|