作业二:拷贝构造函数
来源:互联网 发布:sol数据修改器 编辑:程序博客网 时间:2024/04/29 06:31
#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;}
Problem C: 克隆人来了!
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 969 Solved: 550
[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
3
Zhang 20
Li 18
Zhao 99
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!
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
注意:输出中有“”!
0 0
- 作业二:拷贝构造函数
- C++拾遗--构造函数(二)拷贝构造
- C++拷贝构造函数(深拷贝,浅拷贝)参考二
- c++拷贝构造函数(二)
- C++拷贝构造函数(二)
- 构造函数、拷贝构造函数
- 拷贝构造函数,构造函数
- 拷贝构造函数--默认拷贝构造函数
- C++ 拷贝构造(二)
- 关于拷贝构造函数 (二) ——接一
- C++的一点基本自我修养(二)--拷贝构造函数
- 构造、拷贝构造函数基础
- 拷贝构造函数 深拷贝 浅拷贝
- 拷贝构造函数(深拷贝,浅拷贝)
- 拷贝构造函数浅拷贝深拷贝
- 拷贝构造函数,浅拷贝,深拷贝
- 拷贝构造函数 深拷贝 浅拷贝
- 拷贝构造函数----深拷贝、浅拷贝
- CodeVS3040 中国余数定理 1
- 设置session过期的各种方式(主要看weblogic的方式)
- 点击隐藏汉堡菜单、单页面导航插件—jquery.singlePageNav.min.js、
- Linux Kernel 设备驱动之I2C之i2c设备文件
- 指纹识别入门(1)
- 作业二:拷贝构造函数
- 响应者链机制
- 短信通 API 接口 说明文档
- 使用Maven tomcat:run命令启动web项目时修改默认端口
- 浅谈浏览器渲染页面的过程
- opencv调用basler简单示例
- 博客初创
- Spark学习笔记(七)
- URL中“#” “?” &“”号的作用