Problem C: 克隆人来了!

本文介绍了一个基于克隆技术的Person类实现,通过C++代码展示了如何创建、复制和销毁具有姓名和年龄属性的对象,并提供了详细的输入输出示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

转载于:https://www.cnblogs.com/TogetherLaugh/p/6544696.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值