#include <iostream>
using namespace std;

class Student {
public:
    int num;
    string name;
    char sex;

    void display() {
        cout << "num: " << num << endl;
        cout << "name: " << name << endl;
        cout << "sex: " << sex << endl;
    }
};

class Teacher {
public:
    int num;
    string name;
    char sex;

    void display() {
        cout << "num: " << num << endl;
        cout << "name: " << name << endl;
        cout << "sex: " << sex << endl;
    }
};

int main() {
    Student s;
    s.num = 1001;
    s.name = 'Tom';
    s.sex = 'M';

    Teacher t;
    t.num = s.num;
    t.name = s.name;
    t.sex = s.sex;

    cout << "Student:" << endl;
    s.display();

    cout << "Teacher:" << endl;
    t.display();

    return 0;
}
C++ 学生类转换为教师类:保留相同数据成员

原文地址: https://www.cveoy.top/t/topic/nRna 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录