【题目】定义一个名为CPerson的类,有以下私有成员:姓名、身份证号、性别和年龄,成员函数:构造函数、析构函数、输出信息的函数。并在此基础上派生出CEmployee类,派生类CEmployee增加了两个新的数据成员,分别用于表示部门和薪水。要求派生类CEmployee的构造函数显示调用基类CPerson的构造函数,并为派生类CEmployee定义析构函数,定义输出信息的函数。
给定的程序是:
#include <iostream> #include <string.h> #include <iomanip> 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(); //需要释放建立对象时动态分配的内存 }; 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(); }; 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(); system("pause"); return 0; }
【题目分析】本题主要体会用类的继承解决实际问题:派生类CEmployee在基类CPerson的基础上,增加用于表达“职员”的新的数据成员。完成任务时,注意实现要求完成的各个成员函数即可。
【参考解答】程序的结构中规是矩,但由于类中有多个指针类型的数据成员,在此必须注意在构造函数中,分配专门的存储单元,并让指针指向这个由类本身能够控制的存储单元。这种情形处理不好,将可能会造成灾难性的后果,尽管多数情况程序看上去执行还算正常(这种错误是真正最可怕的错误)。重点理解第30-38行和第56-61行的程序,更详细的解释参考博文《C++防灾——为指针成员分配专门的存储空间》(建议读,你将在参数传递、内存管理方面有更多体会。)
#include <iostream> #include <string.h> #include <iomanip> 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(); }; 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(); }; CPerson::CPerson(char *name,char *id,int 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=sex; m_nAge=age; } void CPerson::Show1() { cout<<setw(10)<<m_szName<<setw(25)<<m_szId; //setw:设置输出数据的宽度,使用时应#include <iomanip.h> if(m_nSex==0) cout<<setw(7)<<"women"; else cout<<setw(7)<<"man"; cout<<setw(5)<<m_nAge<<endl; } CPerson::~CPerson() { delete [ ]m_szName; delete [ ]m_szId; } 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]; strcpy(m_szDepartment,department); m_Salary=salary; } void CEmployee::Show2()//注意派生类输出函数应输出所有成员变量(含基类继承的成员变量)的值 { cout<<setw(10)<<"name"<<setw(25)<<"id"<<setw(7)<<"sex"<<setw(5)<<"age"<<setw(12)<<"department"<<setw(10)<<"salary"<<endl; cout<<setw(10)<<m_szName<<setw(25)<<m_szId; if(m_nSex==0) cout<<setw(7)<<"women"; else cout<<setw(7)<<"man"; cout<<setw(5)<<m_nAge; //由于基类CPerson的成员变量采用了protected属性,因此可采用上述述代码显示来自基类的数据成员,否则 //若基类CPerson的成员变量采用了privated属性,则只能使用CPerson::Show();实现 cout<<setw(12)<<m_szDepartment<<setw(10)<<m_Salary<<endl; } CEmployee::~CEmployee() { delete [ ]m_szDepartment; } 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(); system("pause"); return 0; }