# 实验9：Problem G: 克隆人来了

www.MyException.Cn  网友分享于：2013-09-28  浏览：0次

`cout<<"A person whose name is \""<<name<<"\" and age is "<<age<<" is created!"<<endl;`

Problem G: 克隆人来了！

## Problem G: 克隆人来了！

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 448  Solved: 263
[Submit][Status][Web Board]

## Description

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

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

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

## 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!

## Append Code

[Submit][Status][Web Board]
```#include<iostream>
using namespace std;
class Person{
public:
char* name;
int age;
Person():name("no name"),age(0){cout<<"A person whose name is \""<<name<<"\" and age is "<<age<<" is created!"<<endl;}
Person(char* n,int a):name(n),age(a){cout<<"A person whose name is \""<<name<<"\" and age is "<<age<<" is created!"<<endl;}
Person(const Person& p){name=p.name;age=p.age;cout<<"A person whose name is \""<<name<<"\" and age is "<<age<<" is cloned!"<<endl;}
~Person(){cout<<"A person whose name is \""<<name<<"\" and age is "<<age<<" is erased!"<<endl;}

void showPerson(){cout<<"This person is \""<<name<<"\" whose age is "<<age<<"."<<endl;}
Person&  setName(char* n){name=n;return *this;}
Person&  setAge(int a){age=a;return *this;}
};

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;
}```