CPerson派生出CEmployee類

來源:互聯網
上載者:User
/* * 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;}

心得體會:注意解構函式!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.