第5章实验答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
person类(拷贝构造练习)new
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 231 Solved: 98
[Submit][Status][Web Board] Description
定义一个人员类,描述如下,请补充代码完善类并使测试结果正确class Person{
private:
char *name;
int age;
public:
Person(char *Name,int Age);
Person(const Person &p);
~Person();
void setAge(int x){ age=x; }
void print();
};
Input
Output
构造,析购,拷贝构造函数调用信息
Sample Input
Sample Output
constructor ....
Copy constructor ....
The Address of name: bree
The Address of name: bree
destructor (2)
destructor (1)
#include
#include
using namespace std;
class Person
{
private:
char *name;
int age;
static int num;
public:
Person(char *Name,int Age);
Person(const Person &p);
~Person();
void setAge(int x){ age=x; }
void print()const;
};
Person::Person(char *Name,int Age)
{
name=new char[strlen(Name)+1];
strcpy(name,Name);
age=Age;
num++;
cout<<"constructor ...."< } Person::Person(const Person &p) { name=new char[strlen()+1]; strcpy(name,); age=p.age ; num++; cout<<"Copy constructor ...."< } Person::~Person() { cout<<"destructor..."< } void Person::print() const { cout<<"The Address of name: "< } int Person::num=0; int main() { const Person p1("bree",2); Person p2=p1; //p1.setAge(1); //p2.setAge(2); p1.print(); p2.print(); return 0; } 职工类(静态成员练习) Time Limit: 1 Sec Memory Limit: 128 MB Submit: 107 Solved: 93 [Submit][Status][Web Board] Description 设计一个职工类,职工类中包括职工姓名、薪水和所有职工的薪水总和。int main() { employee e1("zhang",100); employee e2("wang",200); employee e3("li",300); cout<<"allsalary is "< } Input Output 输出所有职工的薪水总和 Sample Input Sample Output allsalary is 600 HINT Append Code #include using namespace std; class employee { private: double salary; static int number; static double allsalary; public: employee(string,double); static double getallsalary() { return allsalary; } }; employee::employee(string name,double salary) { salary=salary; number++; allsalary+=salary; } int employee::number=0; double employee::allsalary=0; int main() { employee e1("zhang",100); employee e2("wang",200); employee e3("li",300); cout<<"allsalary is "< }