*All right reserved.
*文件名称:test.cpp
*作 者:韩双志
*完成日期:2016年5月19日
*版本号:v1.0
*
*问题描述:字符串除了用C++扩充的string类型外,按C语言的传统,还可以用char 表示。请将类声明中的string全部改为char 后,重新写一遍程序(此时的区别是,类中有指针成员,构造和析构函数需要考虑深复制的问题了。)
*输入描述:输入姓名,ID,年龄,专业,薪水
*输出描述:输出姓名,ID,年龄,专业,薪水
/*
#include <iostream>
#include<cstring>
using namespace std;
class CPerson//员工信息类
{
protected:
char *m_szName;
char *m_szId;
char *m_nSex;//0:women,1:man
int m_nAge;
public:
CPerson(char *name,char *id,char *sex,int age);
void Show1();
~CPerson();
};
class CEmployee:public CPerson//类的继承
{
private:
char *m_szDepartment;
float m_Salary;
public:
CEmployee(char *name,char *id,char *sex,int age,char *department,float salary);
void Show2();
~CEmployee();
};
CPerson::CPerson(char *name,char *id,char *sex,int age)//复制构造函数的实现
{
m_szName=new char[strlen(name)+1];//分配内存
strcpy(m_szName,name);
m_szId=new char[strlen(id)+1];
strcpy(m_szId,id);
m_nSex=new char[strlen(sex)+1];
strcpy(m_nSex,sex);
m_nAge=age;
}
CEmployee::CEmployee(char *name,char *id,char *sex,int age,char *department,float salary)
:CPerson(name,id,sex,age)
{
m_szDepartment=new char[strlen(department)+1];
strcpy(m_szDepartment,department);
m_Salary=salary;
}
void CPerson::Show1()//输出相关信息
{
cout<<"name "<<"id "<<"sex "<<"age "<<"deparment "<<"salary"<<endl;
cout<<m_szName<<" "<<m_szId<<" "<<m_nSex<<" "<<m_nAge<<" " ;
}
CPerson::~CPerson()//析构函数的实现
{ delete [ ]m_szName;//内存的释放
delete [ ]m_szId;
delete [ ]m_nSex;
}
void CEmployee::Show2()
{
CPerson::Show1();
cout<<m_szDepartment<<" "<<m_Salary<<" "<<endl;
}
CEmployee::~CEmployee()
{
delete [ ]m_szDepartment;
}
int main()
{
char name[10],id[19],department[10];
int age;
char sex[10];
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;
}
*/
运行结果:
知识点结构
类的继承,类的派生
学习心得
学会了类的继承,类的派生