/*
* Copyright (c) 2013, 烟台大学计算机学院
* All rights reserved.
* 作 者: 沈远宏
* 完成日期:2014年05月06日
* 版 本 号:v1.0
* 问题描述:定义一个名为CPerson的类,有以下私有成员:姓名、身份证号、性别和年龄,成员函数:构造函数、析构函数、输出信息的函数。
并在此基础上派生出CEmployee类,派生类CEmployee增加了两个新的数据成员,分别用于表示部门和薪水。要求派生类CEmployee
的构造函数显示调用基类CPerson的构造函数,并为派生类CEmployee定义析构函数,定义输出信息的函数。
* 输出:职员的信息
*/
#include <iostream>
#include <cstring>
using namespace std;
class CPerson
{
protected:
char *m_szName;
char *m_szId;
int m_nSex;//0:women,1:man
int m_nAge;
public:
CPerson(char* name,char* id,int sex,int age);
void Show1();
~CPerson()
{
cout<<"已释放1"<<endl;
}
};
class CEmployee:public CPerson
{
private:
char *m_szDepartment;
float m_Salary;
public:
CEmployee(char *name,char *id,int sex,int age,char *department,float salary);
void Show2();
~CEmployee()
{
cout<<"已释放2"<<endl;
}
};
CPerson::CPerson(char *name,char *id,int sex,int age)
{
int n=strlen(name)+1;
m_szName=new char[n];
strcpy(m_szName,name);
int j=strlen(id)+1;
m_szId=new char[j];
strcpy(m_szId,id);
m_nSex=sex;
m_nAge=age;
}
CEmployee::CEmployee(char *name,char *id,int sex,int age,char *department,float salary):CPerson(name,id,sex,age)
{
int k=strlen(department)+1;
m_szDepartment=new char[k];
strcpy(m_szDepartment,department);
m_Salary=salary;
}
void CPerson::Show1()
{
cout<<"姓名 "<<m_szName<<"\t身份证号 "<<m_szId;
if(m_nSex==0)
{
cout<<"\t性别 女";
}
else
{
cout<<"\t性别 男";
}
cout<<"\t年龄 "<<m_nAge<<endl;
}
void CEmployee::Show2()
{
Show1();
cout<<"该职员的部门为 "<<m_szDepartment<<"\t薪水为 "<<m_Salary<<endl;
}
int main()
{
char name[10],id[19],department[10];
int sex,age;
float salary;
cout<<"input employee's name,id,sex(0:women,1:man),age,department,salary:\n";
cin>>name>>id>>sex>>age>>department>>salary;
CEmployee employee1(name,id,sex,age,department,salary);
employee1.Show2();
return 0;
}
运行结果:
心得体会:
深复制==神复制
相对来说还是喜欢string,好方便!