Problem D: 模板是个好东西
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 442 Solved: 412
[ Submit][ Status][ Web Board]
Description
定义Point类:
1. 有两个int类型的数据成员,表示其横纵坐标。
2. 无参构造函数,初始化两个坐标为0。
3. 带参构造函数。
4. 重载其输出运算符<<,用于输出一个点的横坐标和纵坐标,中间用一个空格隔开。
定义一个类模板Data:
1. 只有一个数据成员data,data的类型是由类型参数指定的。
2. 定义该类模板的构造函数。
3. 定义void show()方法,用于显示data的值。
Input
输入有5行。
第1行是一个不含空白符的字符串。
第2~4行分别是一个整数,其中第2、3行是点的坐标值。
最后一行是一个字符。
Output
见样例。
Sample Input
test123c
Sample Output
c3test1 2
HINT
Append Code
#include <iostream>
#include <iomanip>
#include <string>
#include<cstring>
using namespace std;
class Point
{
public:
int x,y;
Point(int x=0,int y=0):x(x),y(y){}
friend ostream &operator<<(ostream &os,const Point&p)
{
os<<p.x<<" "<<p.y<<endl;
return os;
}
};
template <typename T>
class Data
{
public:
T data;
Data(T data=0):data(data){}
void show()
{
cout<<data<<endl;
}
};
int main()
{
string n;
int x, y, d;
char c;
cin>>n;
cin>>x>>y>>d;
cin>>c;
Point p(x, y);
Data<char> aChar(c);
Data<int> anInt(d);
Data<Point> aPoint(p);
Data<string> aString(n);
aChar.show();
anInt.show();
aString.show();
aPoint.show();
return 0;
}