#include<iostream> using namespace std; #include<string>; class People { public: People(const string& name,const string& sex, int age) :_name(name) , _sex(sex) , _age(age) { } virtual void Display() { cout << "name: " << _name << endl; cout << "sex: " << _sex << endl; cout << "age: " << _age << endl; } protected: string _name; string _sex; int _age; }; class Student:virtual public People { public: Student(const string& name, const string& sex, int age, int num) :People(name,sex,age) , _num(num) { } virtual void Display() { cout << "Student" << endl; } protected: int _num; }; class Teacher:virtual public People { public: Teacher(const string& name, const string& sex, int age, int employeeNumber) :People(name, sex, age) , _employeeNumber(employeeNumber) { } virtual void Display() { cout << "Teacher" << endl; } protected: int _employeeNumber; }; class Graduate:public Teacher,public Student { public: Graduate(const string& name, const string& sex, int age, int employeeNumber, int num,const string& learnClass) :People(name, sex, age) , Teacher(name, sex, age,employeeNumber) , Student(name,sex,age,num) { _learnClass = learnClass; } virtual void Display() { cout << "Graduate" << endl; } protected: string _learnClass;//研究科目 }; void Test1() { People p("lxj", "女", 18); People* p1 = &p; p.Display(); p1->Display(); Student s("lt", "女", 21, 413); p1 = &s;//p1=(People*)&s; p1->Display(); } void Test2() { People p("lxj", "女", 18); People& p1 = p; p.Display(); p1.Display(); Student s("lt", "女", 21, 413); People& p2 =(People&) s; s.Display(); p2.Display(); } void Test3() { Graduate g("lxj", "女", 11, 22, 11, "software"); g.Display(); } int main() { Test3(); system("pause"); return 0; }
时间: 2024-12-13 01:44:14