/* * Copyright (c) 2013, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作者:邱学伟 * 完成日期:2013 年 5 月 11 日 * 版本号:v1.0 * 输入描述:无 * 问题描述: 定义一个名为CPerson的类,有以下私有成员:姓名、身份证号、性别和年龄,成员函数:构造函数、析构函数、输出信息的函数。并在此基础上派生出CEmployee类,派生类CEmployee增加了两个新的数据成员,分别用于表示部门和薪水。 * 程序输出: 派生类CEmployee的构造函数显示调用基类CPerson的构造函数,并为派生类CEmployee定义析构函数,定义输出信息的函数。 * 问题分析: * 算法设计:略 */ #include <iostream> #include <Cstring> #include <iomanip> using namespace std; class CPerson { public: CPerson(char *name,char *id,int sex1,int age1); void display1(); ~CPerson(); protected: char *c_name; char *c_id; int sex;//性别:1、男性;0、女性 int age; }; CPerson::CPerson(char *name,char *id,int sex1,int age1) { c_name=new char[strlen(name)+1]; strcpy(c_name,name); c_id=new char[strlen(id)+1]; strcpy(c_id,id); sex=sex1; age=age1; } void CPerson::display1() { cout<<"This employee'message is:"<<endl; cout<<setw(10)<<"name"<<setw(25)<<"id"<<setw(7)<<"sex"<<setw(5)<<"age"<<endl; cout<<setw(10)<<c_name<<setw(25)<<c_id<<setw(7); if(sex==1) cout<<"men"; if(sex==0) cout<<"women"; cout<<setw(5)<<age<<endl; } CPerson::~CPerson() { delete [ ]c_name; delete [ ]c_id; } class CEmployee:public CPerson { public: CEmployee(char *name,char *id,int sex,int age,char *department,float salary); void display2(); ~CEmployee(); private: char *CE_department; float CE_salary; }; CEmployee::CEmployee(char *name,char *id,int sex,int age,char *department,float salary):CPerson(name,id,sex,age) { CE_department=new char(strlen(department)+1); strcpy(CE_department,department);//部门 CE_salary=salary;//薪水 } void CEmployee::display2() { cout<<"This student'message is:"<<endl; cout<<setw(10)<<"name"<<setw(25)<<"id"<<setw(7)<<"sex"<<setw(5)<<"age"<<setw(12)<<"department"<<setw(10)<<"salary"<<endl; cout<<setw(10)<<c_name<<setw(25)<<c_id<<setw(7); if(sex==1) cout<<"men"; if(sex==0) cout<<"women"; cout<<setw(5)<<age<<setw(12)<<CE_department<<setw(10)<<CE_salary<<endl; } CEmployee::~CEmployee() { delete []CE_department; } int main() { char name[20],id[20],department[20]; int sex,age; float salary; cout<<"Input employee's name,id,sex(1:men;0:women),age,department,salary:"<<endl; cin>>name>>id>>sex>>age>>department>>salary; CEmployee employee(name,id,sex,age,department,salary); employee.display2(); return 0; }
作者:qiuxuewei2012 发表于2013-5-12 0:09:57 原文链接
阅读:193 评论:0 查看评论