给定人类`Human`及其二子类教师类`Teacher`与学生类`Student`如下:

我的解答思路和尝试过的方法
我想要达到的结果

给定人类Human及其二子类教师类Teacher与学生类Student如下:

class Human {
protected:
    char sex_;
    int id_;
    std::string name_;
};

class Teacher {
    int num_teaching_;
    std::vector<std::string> courses_teaching_;
};

class Student {
    int num_learning_;
    int class_;
};

编写类型转换函数,可在学生和教师之间互相转换,转换时需保留二者公共成员变量(必要时需改变类的定义)

在类里你需要用友元函数来转换。
如果不用友元函数
就定义一个返回值是Teacher 形参是Student的方法。
然后设置值就好了。

参考如下:

Teacher* trans2tea(Student* stu)
{
    return (Teacher*)((Human*)stu);
}
Student* trans2stu(Teacher* tea)
{
    return (Student*)((Human*)tea);
}