C++考试题 急急急急急急急急急急急急急急急

来源:百度知道 编辑:UC知道 时间:2024/06/18 06:35:52
程序设计
(1)给出employee类的定义与实现。employee类有protected的成员int ID,string name和char sex;有构造函数和显式定义的默认析构函数。
(2)给出employee类的公有派生类em1定义和实现。em1类有自定义成员int age 和string addr;有构造函数、显式定义的默认析构函数和show函数。在show中完成对所有数据的输出。
(3)给出main,定义两个em1类对象并对数据进行输出。

#include<iostream.h>
#include<string.h>
class employee
{
public:
employee(int a,char* b,char c)
{
ID=a;
sex=c;
strcpy(name,b);
}
~employee()
{}
protected:
int ID;
char name[20];
char sex;
};
class em1:public employee
{
public:
int age;
char addr[20];
em1(int a,char *b,char c,int d,char *e):employee(a,b,c)
{
age=d;
strcpy(addr,e);
}
~em1()
{}
void show();
};
void em1::show()
{
cout<<"ID号:"<<ID<<"姓名:"<<name<<"性别:"<<sex<<"年龄:"<<age<<"地址:"<<addr<<endl;
}
void main()
{
em1 employee1(10001,"张三",'M',19,"中国湖南长沙"),employee2(10002,"李四",'M',18,"长沙五一路");
employee1.show();
employee2.show()