Problem A: 克隆人来了!

网友投稿 502 2022-11-28

Problem A: 克隆人来了!

Problem A: 克隆人来了!

Problem A: 克隆人来了!

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

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

HINT

注意:输出中有“”!

#include #include using namespace std;class Person{private: string name; int age;public: Person(string n = "no name", int a = 0){ 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 *s) { name = s; 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;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:POJ 1739 Tony's Tour——插头dp
下一篇:POJ 3368 Frequent values——RMQ
相关文章

 发表评论

暂时没有评论,来抢沙发吧~