虚基类函数调用的问题

问题遇到的现象和发生背景

img


在打印student,teacher时调用Person::print()失败

用代码块功能插入代码,请勿粘贴截图
#include
#include
#include 
using namespace std;
class Person {
public:
    /*Person(string name, string age, string sex) {
        this->name = name;
        this->age = age;
        this->sex = sex;
    }*/
    Person();
    void print();
protected:
    string name;
    string age;
    string sex;
};


class Student :virtual public Person {
public:
    /*Student(string name, string age, string sex, string speciality) :Person(name, age, sex) {
        this->speciality = speciality;
    }*/
    Student();
    void print();
protected:
    string speciality;
};


class Teacher :virtual public Person {
public:
    /*Teacher(string name, string age, string sex, string department) :Person(name, age, sex) {
        this->department = department;
    }*/
    Teacher();
    void print();
protected:
    string department;
};


class stuTeacher :public Student, public Teacher {
public:
    //stuTeacher(string name, string age, string sex, string speciality, string department) :Student(name, age, sex, speciality), Teacher(name, age, sex, department), Person(name, age, sex) {}
    void print();
    stuTeacher();
};



void menu();
void display_menu();


Person::Person() {
    cout << "姓名" << endl;
    cin >> name;
    cout << "年龄" << endl;
    cin >> age;
    cout << "性别" << endl;
    cin >> sex;
}


Student::Student() {
    cout << "专业" << endl;
    cin >> speciality;
}


Teacher::Teacher() {
    cout << "院系" << endl;
    cin >> department;
}


stuTeacher::stuTeacher() {}


void Person::print() {
    cout << "姓名:" << name << endl;
    cout << "年龄:" << age << endl;
    cout << "性别:" << sex << endl;
}


void Student::print() {
    Person::print();
    cout << "专业是:" << speciality << endl;
}


void Teacher::print() {
    Person::print();
    cout << "院系是:" << department << endl;
}


void stuTeacher::print() {
    Person::print();
    cout << "专业是:" << Student::speciality << endl;
    cout << "院系是:" << Teacher::department << endl;
}


void menu() {
    system("cls");
    cout << "选择功能" << endl;
    cout << "1.录入学生信息" << endl;
    cout << "2.录入老师信息" << endl;
    cout << "3.录入在修读学位的老师的信息" << endl;
    cout << "4.信息显示" << endl;
    cout << "5.退出" << endl;
}


void display_menu() {
    system("cls");
    cout << "选择功能" << endl;
    cout << "1.显示学生信息" << endl;
    cout << "2.显示老师信息" << endl;
    cout << "3.显示在修读学位的老师的信息" << endl;
    cout << "4.返回主菜单" << endl;

}


int choice, nums, numt, numst, i;
int main(void) {
    while (1) {
    menu();
    Student* s = nullptr;
    Teacher* t = nullptr;
    stuTeacher* st = nullptr;
    cin >> choice;
    switch (choice) {
    case 1:
        cout << "输入学生数量" << endl;
        cin >> nums;
         s = new Student[nums];
    break; 
    case 2: 
        cout << "输入老师数量" << endl;
        cin >> numt;
        t = new Teacher[numt];
        break; 
    case 3: 
        cout << "输入在修读学位的老师的数量" << endl;
        cin >> numst;
        st = new stuTeacher[numst];
        break; 
    case 4:
        display_menu();
        cin >> choice;
        switch (choice) {
        case 1:
            for (i = 0; i < nums; i++) {
            s->print();
            s++;
            }break;
        case 2:
            for (i = 0; i < numt; i++) {
                t->print();
                t++;
            }break;
        case 3:
            for (i = 0; i < numst; i++) {
                st->print();
                st++;
            }break;
        case 4:break;
        }
    case 5:
        delete[] s;
        delete[] t;
        delete[] st;
        return 0;
    }
    }
    
}



运行结果及报错内容

在打印student,teacher时调用Person::print()失败

img

我的解答思路和尝试过的方法

暂时想不到啥办法,也不知道是为什么

我想要达到的结果

可以正常输出就行

别调用它就行了。你要重写基类的虚函数,而不是调用基类的虚函数。