Problem C: 克隆人来了!

Description

克隆技术飞速发展,克隆人已经成为现实了!!所以,现在由你来编写一个Person类,来模拟其中的克隆过程。这个类具有2个属性:name——姓名(char*类型),和age——年龄(int类型)。

该类具有无参构造函数(人名为“no name”,年龄是0)、带参数构造函数、拷贝构造函数以及析构函数外,还有以下3个成员函数:

1. void Person::showPerson():按照指定格式显示人的信息。

2. Person& Person::setName(char *):设定人的姓名。

3. Person& Person::setAge(int):设定人的年龄。

Input

输入分多行,第一行是一个正整数N,表示其后有N行输入。每行分两部分:第一部分是一个没有空白符的字符串,表示一个人的姓名;第二部分是一个正整数,表示人的年龄。

Output

呃~比较复杂,见样例吧!注意:要根据样例编写相应函数中的输出语句,注意格式哦!

Sample Input

3

Zhang 20

Li 18

Zhao 99

Sample Output

A person whose name is "no name" and age is 0 is created!

A person whose name is "Tom" and age is 16 is created!

A person whose name is "Tom" and age is 16 is cloned!

A person whose name is "Zhang" and age is 20 is created!

This person is "Zhang" whose age is 20.

A person whose name is "Zhang" and age is 20 is erased!

A person whose name is "Li" and age is 18 is created!

This person is "Li" whose age is 18.

A person whose name is "Li" and age is 18 is erased!

A person whose name is "Zhao" and age is 99 is created!

This person is "Zhao" whose age is 99.

A person whose name is "Zhao" and age is 99 is erased!

This person is "Zhao" whose age is 18.

This person is "no name" whose age is 0.

A person whose name is "Zhao" and age is 18 is erased!

A person whose name is "Tom" and age is 16 is erased!

A person whose name is "no name" and age is 0 is erased!

HINT

注意:输出中有“”!

Append Code

#include<iostream>

#include<string.h>

using namespace std;

class Person

{

private:

    string name;

    int age;

public:

    Person(){name="no name",age=0;cout<<"A person whose name is \""<<name<<"\" and age is "<<age<<" is created!\n";}

    Person(string n,int a){name=n,age=a;cout<<"A person whose name is \""<<name<<"\" and age is "<<age<<" is created!\n";}

    void showPerson(){cout<<"This person is \""<<name<<"\" whose age is "<<age<<".\n";}

    Person& setName(string m){name=m;}

    Person& setAge(int a){age=a;}

    Person(const Person &Tom){name=Tom.name,age=Tom.age;cout<<"A person whose name is \""<<name<<"\" and age is "<<age<<" is cloned!\n";}

    ~Person(){cout<<"A person whose name is \""<<name<<"\" and age is "<<age<<" is erased!\n";}

};

int main()

{

    int cases;

    char str[80];

    int age;

    Person noname, Tom("Tom", 16), anotherTom(Tom);

    cin>>cases;

    for (int ca = 0; ca < cases; ca++)

    {

        cin>>str>>age;

        Person newPerson(str, age);

        newPerson.showPerson();

    }

    anotherTom.setName(str).setAge(18);

    anotherTom.showPerson();

    noname.showPerson();

    return 0;

}

时间: 2024-12-20 05:40:15

Problem C: 克隆人来了!的相关文章

A Math Problem

A Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 237    Accepted Submission(s): 117 Problem Description You are given a positive integer n, please count how many positive integers

Water Problem

water problem 发布时间: 2015年10月10日 15:34   时间限制: 1000ms   内存限制: 256M 描述 题意很简单 给你N个数, Q个查询 每次查询给你一个区间[L, R] 你要找出 [L, R] 这个区间里面取模M后的最大值. 输入 第一行一个T,表示测试数据组数.第二行两个整数N, M (1<=N<=10^5, 1<=M<=10^9).第三行给你N个整数 整数范围在1到10^9之间.第四行给你一个整数Q. ( 1<=Q<=10^5)

FOJ Problem 2261 浪里个浪

                                                                                                                                                           Problem 2261 浪里个浪 Accept: 40    Submit: 106Time Limit: 1500 mSec    Memory Limit : 32768 KB Pro

XJTUOJ wmq的A&#215;B Problem FFT

wmq的A×B Problem 发布时间: 2017年4月9日 17:06   最后更新: 2017年4月9日 17:07   时间限制: 3000ms   内存限制: 512M 描述 这是一个非常简单的问题. wmq如今开始学习乘法了!他为了训练自己的乘法计算能力,写出了n个整数,并且对每两个数a,b都求出了它们的乘积a×b.现在他想知道,在求出的n(n−1)2个乘积中,除以给定的质数m余数为k(0≤k<m)的有多少个. 输入 第一行为测试数据的组数. 对于每组测试数据,第一行为2个正整数n,

hidden node and exposed node problem

Exposed node problem In wireless networks, theexposed node problem occurs when a node is prevented from sending packets to other nodes because of a neighboring transmitter. Consider an example of 4 nodes labeled R1, S1, S2, and R2, where the two rece

南阳524 A-B Problem

A-B Problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 A+B问题早已经被大家所熟知了,是不是很无聊呢?现在大家来做一下A-B吧. 现在有两个实数A和B,聪明的你,能不能判断出A-B的值是否等于0呢? 输入 有多组测试数据.每组数据包括两行,分别代表A和B. 它们的位数小于100,且每个数字前中可能包含+,- 号. 每个数字前面和后面都可能有多余的0. 每组测试数据后有一空行. 输出 对于每组数据,输出一行. 如果A-B=0,输出YES,否则输出NO

Lab 1: Write a java program for the triangle problem and test the program with Junit.

Tasks: 1. Install Junit(4.12), Hamcrest(1.3) with Eclipse 将两个jar包添加到工程中 2. Install Eclemma with Eclipse 3. Write a java program for the triangle problem and test the program with Junit. [Description of triangle problem]Function triangle takes three i

HDU 1016 Prime Ring Problem(DFS)

题目链接 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always

poj 3320 Jessica&#39;s Reading Problem(尺取法+map/hash)

题目:http://poj.org/problem?id=3320 题意:给定N个元素的数组,找出最短的一段区间使得区间里面的元素种类等于整个数组的元素种类. 分析:暴力枚举区间的起点x,然后找到最小的y,使得区间[x,y]满足条件,x向有移位后变成x',现在的y'肯定不至于在y的左边.存状态的话map和hash都可以. map代码: #include <iostream> #include <set> #include <map> #include <cstdi