/*
*程序的版权和版本声明部分
* Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称:CPerson .cpp
* 作 者: 田凤
*完成日期:2013年5月21日
* 版本号: v1.0
* 输入描述: 输入个人信息
* 问题描述:略
* 输出:输出对应的信息
*代码:
#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];//多分配一个字节的空间来存放字符串结尾标记'\0'字符
strcpy(m_szName,name);
m_szId=new char[strlen(id)+1];//多分配一个字节的空间来存放字符串结尾标记'\0'字符
strcpy(m_szId,id);
m_nSex=sex;
m_nAge=age;
}
void CPerson::Show1()
{
cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(2);
cout<<setw(6)<<m_szName<<setw(16)<<m_szId<<setw(10);
if(m_nSex==0)
{
cout<<"women";
}
else
{
cout<<"man";
}
cout<<setw(8)<<m_nAge;
}
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];//多分配一个字节的空间来存放字符串结尾标记'\0'字符
strcpy(m_szDepartment,department);
m_Salary=salary;
}
void CEmployee::Show2()
{
cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(2);
cout<<setw(6)<<"姓名"<<setw(16)<<"学号"<<setw(10)<<"性别"<<setw(8)<<"年龄"<<setw(10)<<"部门"<<setw(10)<<"薪水"<<endl;
Show1();
cout<<setw(10)<<m_szDepartment<<setw(10)<<m_Salary;
}
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();
return 0;
}
*运行结果:
*心得体会:注意new的用法,用完后要撤销空间,注意使用析构函数!