2016年計算機軟件設(shè)計師程序模擬練習題

字號:

●試題五
    閱讀下列程序說明,將應填入(n)處的字句寫在答卷紙的對應欄內(nèi)。
    【程序說明】
    對于一個公司的雇員來說,無非有3種:普通雇員、管理人員和主管。這些雇員有共同的數(shù)據(jù):名字、每小時的工資,也有一些共同的操作:數(shù)據(jù)成員初始化、讀雇員的數(shù)據(jù)成員及計算雇員的工資。但是,他們也有不同。例如,管理人員除有這些共同的特征外,有可能付固定薪水,主管除有管理人員的共同特征外,還有其他物質(zhì)獎勵等。3種雇員中,管理人員可以看作普通雇員的一種,而主管又可以看作管理人員的一種。我們很容易想到使用類繼承來實現(xiàn)這個問題:普通雇員作為基類,管理人員類從普通雇員類中派生,而主管人員類又從管理人員類中派生。
    下面的程序1完成上述各個類的定義,并建立了3個雇員(一個普通雇員、一個管理人員和一個主管)的檔案,并打印出各自的工資表。將"程序1"中的成員函數(shù)定義為內(nèi)聯(lián)函數(shù),pay成員函數(shù)定義為虛函數(shù),重新完成上述要求。
    【程序1】
    //普通雇員類
    class Employee
    {
    public:
    Employee(char*theName,float thePayRate);
    char*getName()const;
    float getPayRate()const;
    float pay(float hoursWorked)const;
    protected:
    char*name;//雇員名稱
    float payRate;//薪水等級
    };
    Employee::Employee(char*theName,float thePayRate)
    {
    name=theName;
    payRate=thePayRate;
    }
    char*Employee::getName() const
    {
    return name;
    }
    float Employee::getPayRate()const
    {
    return payRate;
    }
    float Employee::pay(float hoursWorked)const
    {
    return hoursWorked*payRate;
    }
    //管理人員類
    class Manager∶public Employee
    {
    public:
    //isSalaried付薪方式:true付薪固定工資,false按小時付薪
    Manager(char*theName,float thePayRate,bool isSalaried);
    bool getSalaried()const;
    float pay(float hoursWorked)const;
    protected:
    bool salaried;
    };
    Manager::Manager(char*theName,float thePayRate,bool isSalaried)
    ∶Employee(theName,thePayRate)
    {
    salaried=isSalaried;
    }
    bool Manager::getSalaried() const
    {
    return salaried;
    }
    float Manager::pay(float hoursWorked)const
    {
    if(salaried)
    return payRate;
    /*else*/
    return Employee::pay(hoursWorked);
    }
    //主管人員類
    class Supervisor:public Employee
    {
    public:
    Supervisor(char*theName,float thePayRate,float theBouns):
    Employee(theName,thePayRate, (1) ),bouns(theBouns){}
    float getBouns()const{return bouns;}
    float pay(float hoursWorked)const
    return (2) ;
    }
    protected:
    float bouns;
    }
    #include"iostream.h"
    void main()
    {
    Employee e("Jack",50.00);
    Manager m("Tom",8000.00,true);
    Supervior s("Tanya",8000.00,8000.00);
    cout<<"Name:"<
    cout<<"Pay:"<
    cout<<"Name:"<
    cout<<"Pay:"<
    cout<<"Name:"<
    cout<<"Pay:"<
    }
    【程序2】
    #include"employee.h"
    //普通雇員類
    class Employee
    {
    public:
    //構(gòu)造函數(shù)
    Employee(string theName,float thePayRate):
    name(theName),payRate(thePayRate){}
    //取雇員姓名
    string getName() const{returnname;}
    //取雇員薪水等級
    float getPayRate()const{return payRate;}
    //計算雇員薪水
    virtual float pay(float hoursWorked)const{return (3) ;}
    protected:
    string name;//雇員名稱
    float payRate;//薪水等級
    };
    //管理人員類
    //繼承普通雇員類
    class Manager:public Employee
    {
    public:
    //構(gòu)造函數(shù)
    //isSalaried標識管理人員類的付薪方式
    //true 按階段付薪(固定工資)
    //false按小時付薪
    Manager(string theName,float thePayRate,bool isSalaried):
    Employee(theName,thePayRate),salaried(isSalaried){}
    //取付薪方式
    bool getSalaried()const{return salaried;}
    //計算薪水
    virtual float pay(float (4) )const;
    protected:
    bool salaried;
    };
    float Manager::pay(float hoursWorked)const
    {
    if(salaried)//固定付薪方式
    return payRate;
    else//按小時付薪
    return (5) ; }
    //主管人員類
    class Supervisor: (6)
    {
    public:
    //構(gòu)造函數(shù)
    Supervisor(string theName,float thePayRate,float theBouns):
    Manager(theName,thePayRate,true),bouns(theBouns){}
    //取獎金數(shù)額
    float getBouns()const{return bouns;}
    //計算薪水
    virtual float pay(float hoursWorked)const
    {
    retum payRate+bouns;
    }
    (7)
    float bouns;
    }
    #include"employee.h"
    #include"iostream.h"
    void main()
    {
    (8) *ep[3];ep[0]=new Employee("Jack","50.00");
    ep[1]=new Manager("Tom","8000.00",true);
    ep[2]=new Supervior("Tanya","8000.00","8000.00");
    for(int i=0;i<3;i++){
    cout<<"Name:"<< (9) <
    cout<<"Pay:"<< (10) <
    }
    }