首页 > 代码库 > 十一周 项目2 职员有薪水了 扩展
十一周 项目2 职员有薪水了 扩展
#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:\n"; cin>>name>>id>>sex>>age>>department>>salary; CEmployee employee1(name,id,sex,age,department,salary); employee1.Show2(); return 0; }
感悟:对深复制还是不多熟悉,会老是出错!不过这个点编程序还是很疯狂的啊!
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。