在程式中定義一個基類Person類,由這個基類派生出Teacher(教師)類和Leader(領導)類。採用多繼承的方式由這兩個類派生出Teacher_Leader類。並且滿足以下要求:
阿新 • • 發佈:2019-01-10
#include using namespace std; #include #include class Person { public: Person(char* name, int age, char *gender, char * address, long phone); void show(void); protected: char *name; int age; char *gender; char *address; long phone; }; Person::Person(char* name, int age, char *gender, char * address, long phone) { this->name = new char[strlen(name) + 1]; strcpy(this->name, name); this->age = age; this->gender = new char[strlen(gender) + 1]; strcpy(this->gender, gender); this->address = new char[strlen(address) + 1]; strcpy(this->address, address); this->phone = phone; } void Person::show(void) { cout << "姓名 " << name << endl << "年齡 " << age << endl << "性別 " << gender << endl << "地址 " << address << endl; cout<< "電話 " << phone << endl; } class Teacher :virtual public Person { public: Teacher(char* name, int age, char *gender, char * address, long phone, char *job, int salary) :Person(name, age, gender, address, phone) { this->job = new char[strlen(job) + 1]; strcpy(this->job, job); this->salary = salary; } void show(void); private: char *job; int salary; }; void Teacher::show(void) { cout << "職稱 " << job << endl << "工資 " << salary << endl; } class Leader :virtual public Person { public: Leader(char* name, int age, char *gender, char * address, long phone, char *duty) :Person(name, age, gender, address, phone) { this->duty = new char[strlen(duty) + 1]; strcpy(this->duty, duty); } void show(void); private: char *duty; }; void Leader::show(void) { cout << "職務 " << duty << endl; } class Teacher_Leader :public Teacher, public Leader { public: Teacher_Leader(char* name, int age, char *gender, char * address, long phone, char *job, int salary, char *duty) : Person(name, age, gender, address, phone), Teacher(name, age, gender, address, phone, job, salary), Leader(name, age, gender, address, phone, duty){}; void show(Teacher_Leader p); }; void Teacher_Leader::show(Teacher_Leader p) { p.Person::show(); p.Teacher::show(); p.Leader::show(); } int main(void) { Teacher_Leader p("張三", 45, "男", "四川大學", 10086, "大學老師", 4500, "教英語"); p.show(p); system("pause"); return 0; }