克隆人来了
来源:互联网 发布:淘宝商品为何会下架 编辑:程序博客网 时间:2024/06/11 00:46
Problem E: 克隆人来了!
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 1084 Solved: 598
[Submit][Status][Web Board]
Description
列表内容
标题
克隆技术飞速发展,克隆人已经成为现实了!!所以,现在由你来编写一个Person类,来模拟其中的克隆过程。这个类具有2个属性:name——姓名(char*类型),和age——年龄(int类型)。
该类具有无参构造函数(人名为“no name”,年龄是0)、带参数构造函数、拷贝构造函数以及析构函数外,还有以下3个成员函数:
void Person::showPerson():按照指定格式显示人的信息。
Person& Person::setName(char *):设定人的姓名。
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
注意:输出中有“”!
#include <iostream>#include <cstring>using namespace std; class Person { private: int age; char* name; public: Person() { age=0; name="no name"; cout<<"A person whose name is \""<<name<<"\" and age is "<<age<<" is created!"<<endl; } Person(char *Name,int Age) { age=Age; name=new char[strlen(Name)+1]; strcpy(name,Name); cout<<"A person whose name is \""<<name<<"\" and age is "<<age<<" is created!"<<endl; } ~Person() { cout<<"A person whose name is \""<<name<<"\" and age is "<<age<<" is erased!"<<endl; } Person(const Person &p) { age=p.age; name=new char[strlen(p.name)+1]; strcpy(name,p.name); cout<<"A person whose name is \""<<name<<"\" and age is "<<age<<" is cloned!"<<endl; } void showPerson() { cout<<"This person is \""<<name<<"\" whose age is "<<age<<"."<<endl; } Person &setAge(int x1) { age=x1; return *this; // return age; } Person &setName(char p[]) { name=p; 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;}
- 克隆人来了
- Problem C: 克隆人来了!
- Problem A: 克隆人来了!
- Problem A: 克隆人来了!
- Problem A: 克隆人来了!
- 如何使用SSH来克隆项目
- 来了
- 来了
- 来了
- 来了
- 来了
- 来了
- 来了
- 来了
- 来了
- 来了
- 来了
- 来了
- PAT甲级1004
- 00003 不思议迷宫.0009.6:一键翻开石板捡取物品
- 山寨记事本
- Linux Kernel设备驱动模型之设备探测驱动
- kmp next 数组理解
- 克隆人来了
- 【Spring源码解读】BeanFactory和FactoryBean区别及类装载源码解读
- c++类、函数
- 用smarty模板做的登录
- python流程控制、函数总结
- 数据结构C语言版——初始化一个线性表
- Mutual Training for Wannafly Union #6 E
- Linux Kernel设备驱动模型之 总线注册
- 前端基础之三HTML元素分类