Problem D: 模板是个好东西

Problem D: 模板是个好东西

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Usher_Ou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值