首页 > 其他分享 >Problem A: 克隆人来了!

Problem A: 克隆人来了!

时间:2023-05-29 14:04:58浏览次数:42  
标签:erased whose name Person age 克隆人 person Problem


Home

Web Board

ProblemSet

Standing

Status

Statistics


Problem A: 克隆人来了!


Time Limit: 1 Sec   Memory Limit: 128 MB

Submit: 1979  

Solved: 1072

[Submit][Status][Web Board]


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



3Zhang 20Li 18Zhao 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



append.cc,


[ Submit][Status][Web Board]


한국어<  中文 فارسی English ไทย All Copyright Reserved 2010-2011 SDUSTOJ TEAM
GPL2.0 2003-2011 HUSTOJ Project TEAM
Anything about the Problems, Please Contact Admin:admin


#include <iostream>
#include <cstring>
using namespace std;
class Person
{
public:
     Person& setName(string nn)
     {
         name = nn;
         return *this;
     }
    Person& setAge(int a)
    {
        age = a;
    }
    void showPerson()const
    {
        cout << "This person is \"" << name << "\" whose age is " << age << "." << endl;
    }
public:
    Person()
    {
        name = "no name";age = 0;
        cout << "A person whose name is \"" << name << "\" and age is " << age << " is created!" << endl;
    }
    Person(string nn)
    {
        name = nn;
        age = 0;
        cout << "A person whose name is \"" << name << "\" and age is " << age << " is created!" << endl;
    }
    Person(string s, int a)
    {
        name = s;
        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;
    }
private:
   string name;
    int age;
 
};
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;
}



标签:erased,whose,name,Person,age,克隆人,person,Problem
From: https://blog.51cto.com/u_16129621/6370413

相关文章

  • Problem L: STL——字符串排序
    HomeWebBoardProblemSetStandingStatusStatisticsProblemL:STL——字符串排序TimeLimit:1Sec  MemoryLimit:128MBSubmit:3482  Solved:1666[Submit][Status][WebBoard]Description  对N个字符串排序。  0<N<=5000......
  • Problem G: 时间类的12小时制输出
    HomeWebBoardProblemSetStandingStatusStatisticsProblemG:时间类的12小时制输出TimeLimit:4Sec  MemoryLimit:128MBSubmit:4541  Solved:2405[Submit][Status][WebBoard]Description封装一个时间类Time,用于时间处理的相关功能,支持24......
  • Problem F: 时间类的常量
    HomeWebBoardProblemSetStandingStatusStatisticsProblemF:时间类的常量TimeLimit:4Sec  MemoryLimit:128MBSubmit:2103  Solved:1715[Submit][Status][WebBoard]Description封装一个时间类Time,用于时间处理的相关功能,支持以下操作:......
  • Problem A: 平面上的点和线——Point类、Line类 (I)
    HomeWebBoardProblemSetStandingStatusStatisticsProblemA:平面上的点和线——Point类、Line类(I)TimeLimit:1Sec  MemoryLimit:128MBSubmit:3609  Solved:2357[Submit][Status][WebBoard]Description在数学上,平面直角坐标系上的点......
  • Problem B: 类的初体验(II)
    HomeWebBoardProblemSetStandingStatusStatisticsProblemB:类的初体验(II)TimeLimit:1Sec  MemoryLimit:128MBSubmit:715  Solved:653[Submit][Status][WebBoard]Description定义一个类Data,只有一个double类型的属性和如下3个方法:1. 带1......
  • Problem D: 类的初体验(IV)
    HomeWebBoardProblemSetStandingStatusStatisticsProblemD:类的初体验(IV)TimeLimit:1Sec  MemoryLimit:128MBSubmit:1075  Solved:657[Submit][Status][WebBoard]Description定义一个类Data,只有一个int类型的属性和如下方法:1. 缺省构造......
  • Problem A: 平面上的点——Point类 (I)
    ProblemA:平面上的点——Point类(I)TimeLimit:1Sec  MemoryLimit:4MBSubmit:8255  Solved:3705[Submit][Status][WebBoard]Description在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定。现在我们封装一个“Point类”来实现平面上......
  • Problem A: 你会定义类吗?
    ProblemA:你会定义类吗?TimeLimit:1Sec  MemoryLimit:128MBSubmit:1373  Solved:1078[Submit][Status][WebBoard]Description定义一个类Demo,有构造函数、析构函数和成员函数show(),其中show()根据样例的格式输出具体属性值。该类只有一个int类型的成......
  • 2022 AMC 10A Problems
    2022AMC10AProblemsProblem1Whatisthevalueof                                             Problem2Mikecycledlapsinminutes.Assumehecycledataconst......
  • 力扣 662 https://leetcode.cn/problems/maximum-width-of-binary-tree/
    需要了解树的顺序存储如果是普通的二叉树,底层是用链表去连接的如果是满二叉树,底层用的是数组去放的,而数组放的时候会有索引对应当前父节点是索引i,下一个左右节点就是2i,2i+1利用满二叉树的索引特征所以需要对每个节点进行一个索引赋值,赋值在队列中,队列用数组表示核心代码......