题目:
定义类模板Point,有两个坐标(x,y),x和y的类型可以不同,坐标的数据类型可以是int、float,double类型. 具有获取坐标的成员函数getX和getY。
接口定义:
class Point
裁判测试程序
#include <iostream>
using namespace std;
@@@
int main()
{
Point<int, int> p1(10, 20);
Point<int, float> p2(10, 20.5f);
Point<float, float> p3(10.5f, 20.5f);
Point<double, double > p4(10.5, 20.5);
cout<<p1.getX()<<","<<p1.getY()<<endl;
cout<<p2.getX()<<","<<p2.getY()<<endl;
cout<<p3.getX()<<","<<p3.getY()<<endl;
cout<<p4.getX()<<","<<p4.getY()<<endl;
return 0;
}
/* 请在这里填写答案 */
10,20
10,20.5
10.5,20.5
10.5,20.5
解题代码:
#include <iostream>
using namespace std;
template <class T1,class T2> class Point{
private:
T1 x;
T2 y;
public:
Point(T1 a,T2 b){
x = a;
y = b;
}
T1 getX(){
return x;
}
T2 getY(){
return y;
}
};