#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(); }; CPerson::CPerson(char *name,char *id,int sex,int age) { m_szName=new char[strlen(name)+1]; m_szId=new char[strlen(id)+1]; m_szName=name;//以为有了m_szName=new char[strlen(name)+1]这句 就不用写 m_szName=name呢 看来我错了 m_szId=id; m_nSex=sex; m_nAge=age; } CPerson::~CPerson() { delete []m_szName; delete []m_szId; } 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(); }; CEmployee::CEmployee(char *name,char *id,int sex,int age,char *department,float salary):CPerson(name,id,sex,age) { m_szDepartment=new char[strlen(department)+1]; m_szDepartment=department; m_Salary=salary; } CEmployee::~CEmployee() { delete []m_szDepartment; } void CEmployee::Show2() { cout<<"姓名: "<<m_szName<<endl; cout<<"身份证号:"<<m_szId<<endl; cout<<"性别: "<<m_nSex<<endl; cout<<"年龄: "<<m_nAge<<endl; cout<<"部门: "<<m_szDepartment<<endl; cout<<"月薪: "<<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: "; cin>>name>>id>>sex>>age>>department>>salary; CEmployee employee1(name,id,sex,age,department,salary); employee1.Show2(); return 0; }
感悟:对深复制还是不多熟悉,会老是出错!但是,这一点还是很疯狂的编程啊。
版权声明:本文博客原创文章。博客,未经同意,不得转载。