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

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

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.