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日

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系