ACOYE 2021-12-15 15:27 采纳率: 100%
浏览 230
已结题

定义人员(Person)类,数据包含姓名年龄性别等。

定义人员(Person)类,数据成员包含姓名(name)、年龄(age)和性别(sex)等属性,由Person类分别派生出教师(Teachar)类和干部(Cadre)类。Teachar类有职称(title)属性,Cadre类有职务(post)属性。再由Teachar类和Cadre类采用多重继承方式派生出教师兼干部(TeacherCadre)类,有工资(wages)属性。每个类有各自的构造函数和数据成员的输入输出函数。在主函数中定义对象进行测试。

  • 写回答

1条回答 默认 最新

  • CCodelab 2021-12-16 13:35
    关注
    namespace codelab
    {
        typedef enum sex {
            SEX_UNKNOWN,
            SEX_MAN,
            SEX_WOMAN,
        }sex_e;
        class Person {
        public:
            string *Name = nullptr;
            uint8_t Age;
            sex_e Sex;
            Person(const char *name)
            {
                if (name) {
                    this->Name = new string(name);
                }
            }
            Person(const char *name, sex_e sex)
            {
                if (name) {
                    this->Name = new string(name);
                }
                this->Sex = sex;
            }
            Person(const char *name, sex_e sex, uint8_t age)
            {
                if (name) {
                    this->Name = new string(name);
                }
                this->Sex = sex;
                this->Age = age;
            }
            virtual ~Person()
            {
                if (this->Name) {
                    delete this->Name;
                    this->Name = nullptr;
                }
            }
            virtual void SelfIntroduction(void)
            {
                cout << "I`m " << this->Name->c_str() << ",a(n) " << (int)this->Age <<
                    " years old average " << (this->Sex == SEX_UNKNOWN ? "person" :
                    (this->Sex == SEX_MAN ? "man" : "woman")) << "." << endl;
            }
        private:
    
        };
    
        class Teachar : virtual public Person{
        public:
            uint8_t Title = 0;
            Teachar(const char *name) : Person(name)
            {
    
            }
            void SelfIntroduction(void)
            {
                cout << "I`m " << this->Name->c_str() << ",a(n) " << (int)this->Age <<
                    " years old " << (this->Sex == SEX_UNKNOWN ? "" :
                    (this->Sex == SEX_MAN ? "Male " : "Female")) << "teacher." << endl;
            }
            ~Teachar()
            {
            }
        private:
    
        };
    
        class Cadre : virtual public Person {
        public:
            uint8_t Post = 0;
            Cadre(const char *name) : Person(name)
            {
    
            }
            void SelfIntroduction(void)
            {
                cout << "I`m " << this->Name->c_str() << ",a(n) " << (int)this->Age <<
                    "years old " << (this->Sex == SEX_UNKNOWN ? "" :
                    (this->Sex == SEX_MAN ? "Cadre " : "Cadre")) << "." << endl;
            }
            ~Cadre()
            {
            }
        private:
    
        };
    
        class TeacherCadre : public Cadre, public Teachar {
        public:
            TeacherCadre(const char *name) : Person(name),Cadre(name), Teachar(name)
            {
    
            }
            bool WagesSet(uint32_t wages)
            {
                if (wages < CONFIG_MINIMUM_WAGE) {
                    return false;
                }
                this->Wages = wages;
                return true;
            }
            uint32_t WagesGet(void)
            {
                return this->Wages;
            }
            void SelfIntroduction(void)
            {
                cout << "My name is " << this->Teachar::Name->data() <<
                    ", I`m a Supper " << (this->Sex == SEX_WOMAN ? "WOMAN" : "MAN")
                    << endl;
            }
        private:
            uint32_t Wages = CONFIG_MINIMUM_WAGE;
    
        };
    }
    
    using namespace codelab;
    
    void PersonTest(void)
    {
        TeacherCadre *tc = new TeacherCadre("WangMing");
        Teachar *t = new Teachar("LiDong");
        Cadre *c = new Cadre("ZhangSan");
        Person *p = new Person("WenXin");
    
        t->Age = 28;
        t->Sex = SEX_MAN;
    
        c->Age = 24;
        c->Sex = SEX_MAN;
    
        p->Age = 23;
        p->Sex = SEX_WOMAN;
    
        tc->Age = 54;
        tc->Sex = SEX_MAN;
    
        tc->SelfIntroduction();
        t->SelfIntroduction();
        c->SelfIntroduction();
        p->SelfIntroduction();
    
        delete tc;
        tc = nullptr;
        delete t;
        t = nullptr;
        delete c;
        c = nullptr;
        delete p;
        p = nullptr;
    }
    

    ->

    My name is WangMing, I`m a Supper MAN
    I`m LiDong,a(n) 28 years old Male teacher.
    I`m ZhangSan,a(n) 24years old Cadre .
    I`m WenXin,a(n) 23 years old average woman.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月27日
  • 已采纳回答 12月19日
  • 创建了问题 12月15日

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)