#include <iostream> #include <string> using namespace std; class MyDate { public: MyDate(int y,int m,int d){year=y;month=m;day=d;} void diffYear(){diffyear=((2010-year)*12.0-month+1)/12.0;} int getyear(){return year;} int getmonth(){return month;} int getday(){return day;} double getdiffyear(){return diffyear;} private: int year, month, day; double diffyear; }; class Person {public: void setname(const string a,string b,string c){name=a;id=b;phonenumber=c;} void print(){cout<<name<<" "<<id<<" "<<phonenumber<<" ";} protected: string name,id,phonenumber; }; class Student:public Person {public: void setgrade(int a){grade=a;} void print(){ Person::print(); string a[4]={"Freshman","Sophomore","Junior","Senior"}; cout<<a[grade-1]<<endl; } private: int grade; }; class Employee:public Person {public: void setoffice(string a){office=a;} void setdateHired(MyDate&c){year=c.getyear();month=c.getmonth();day=c.getday();} void getdiffyear(MyDate&c){diffyear=c.getdiffyear();} void print(){ Person::print(); cout<<"office:"<<office<<" dateHired:"<<year<<" "<<month<<" "<<day<<endl; } protected: string office; double salary; int year,month,day; double diffyear; }; class Staff:public Employee {public: Staff(){BasicWages=3000;Allowance=500;} void getsalary(){ salary=BasicWages+Allowance*diffyear; cout<<"salary:"<<salary<<endl<<endl; } void print(){ Employee::print(); cout<<"BasicWages:"<<BasicWages<<" Allowance:"<<Allowance<<endl; } private: double Allowance, BasicWages; }; class Faculty:public Employee {public: Faculty(){BasicWages=3000;} void setrank(int a){rank=a;} void getsalary(){salary=BasicWages*rank;cout<<"salary:"<<salary<<endl<<endl;} void print(){ Employee::print(); string a[3] = {"Professor","AssociateProfessor ","AssistantProfessor"}; cout<<a[rank-1]<<" BasicWages:"<<BasicWages<<endl; } private: int rank; double BasicWages; }; int main() { MyDate a(2000, 1, 1); Student stu; Faculty fac; Staff sta; stu.setname("张三","123","12345678901"); stu.setgrade(2); stu.print(); sta.setname("李四","213","10987654321"); sta.setoffice("SSTA"); fac.diffYear(); sta.setdateHired(a); sta.getdiffyear(a); sta.print(); sta.getsalary(); fac.setname("王五","321","55589964823"); fac.setoffice("SSTA"); fac.setdateHired(a); fac.setrank(3); fac.print(); fac.getsalary(); return 0; }
这次的实验需要大量的脑补内容啊...