标签:style blog http io os ar 2014 art sp
程序代码
#include <iostream> #include <Cstring> using namespace std; class CPerson //定义一个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]; //赋值 strcpy(m_szName, name); strcpy(m_szId, id); m_nSex = sex; m_nAge = age; } //显示个人信息 void CPerson::Show1() { cout<<"姓名\t"<<"身份证号码\t"<<"性别\t"<<"年龄"<<endl; cout<<m_szName<<"\t"<<m_szId<<"\t"<<m_nSex<<"\t"<<m_nAge<<endl; } CPerson::~CPerson()//需要释放建立对象时动态分配的内存 { //释放内存 delete []m_szName; delete []m_szId; } class CEmployee:public CPerson//CEmployee类继承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]; strcpy(m_szDepartment,department);//初始化部门 m_Salary = salary;//初始化工资 } //显示员工信息 void CEmployee::Show2() { char sex[10]; if(0 == m_nSex) { strcpy(sex, "women"); } else { strcpy(sex, "man"); } cout<<"姓名\t"<<"身份证号码\t\t"<<"性别\t"<<"年龄\t"<<"部门\t"<<"工资"<<endl; cout<<m_szName<<"\t"<<m_szId<<"\t"<<sex<<"\t"<<m_nAge<<"\t"<<m_szDepartment<<"\t"<<m_Salary<<endl; } //析构函数 CEmployee::~CEmployee() { //释放内存 delete []m_szDepartment; } int main() { char name[10],id[19],department[10]; int sex,age; float salary; cout<<"请输入员工的姓名, 身份证号码, 性别(0:women,1:man), 年龄, 部门, 工资:"<<endl; cin>>name>>id>>sex>>age>>department>>salary; //初始化员工信息 CEmployee employee1(name,id,sex,age,department,salary); //输出员工信息 employee1.Show2(); system("pause"); }
执行结果
当输入 张三的信息后输出的结果
标签:style blog http io os ar 2014 art sp
原文地址:http://blog.csdn.net/u010105970/article/details/39233245