• 析构函数构造函数CPerson派生出CEmployee类


    首先声明,我是一个菜鸟。一下文章中出现技术误导情况盖不负责

    /* 
    * 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;
    }
        每日一道理
    冰心说道:“爱在左,同情在右,走在生命的两旁,随时撒种,随时开花,将这一径长途,点缀得香花弥漫,使穿枝拂叶的行人,踏着荆棘,不觉得痛苦,有泪可落,却不是悲凉。”

        析构函数和构造函数

        心得体会:注意析构函数!

    文章结束给大家分享下程序员的一些笑话语录: IBM和波音777
      波音777是有史以来第一架完全在电脑虚拟现实中设计制造的飞机,所用的设备完全由IBM公司所提供。试飞前,波音公司的总裁非常热情的邀请IBM的技术主管去参加试飞,可那位主管却说道:“啊,非常荣幸,可惜那天是我妻子的生日,So..”..
      波音公司的总载一听就生气了:“胆小鬼,我还没告诉你试飞的日期呢!”

  • 相关阅读:
    【转】逆向工程:让我们剥开软件的坚果
    分散/聚集IO(scatter/gather)及iovec结构体
    C10K并发连接_转
    AVL平衡二叉树
    软中断
    应用层timer_如何序列化timer
    应用层timer_libc_posix timer
    linux/unix 段错误捕获_转
    C/C++捕获段错误,打印出错的具体位置(精确到哪一行)_转
    linux内存查看及释放
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3074219.html
Copyright © 2020-2023  润新知