回分類題庫
d242: 102OOP Lab Exercise: Advanced Inheritance
出處:

Difficulity : N/A
Accepted : 68 Times | Submit :97 Times | Clicks : 1255
Accepted : 67 Users | Submit : 68 Users | Accepted rate : 99%
Time Limit :10000 ms | Memory Limit : 64000 KBytes
題目加入時間 : 2013-12-02 16:55

Content :

利用抽象類別及多型觀念建立薪資計算系統

 

三種員工

Salaried Employee:固定薪資員工

Commission Employee:抽佣員工,會得到銷售總額某百分比的佣金收入

BasePlus Commission Employee:薪資加抽佣員工,會得到基底薪資加佣金收入

 

繼承階層示意圖:

                       Employee(抽象類別)
                                    |
           
┌──────┴─────┐

 SalariedEmployee              CommissionEmployee
                                                        |
                                          
BasePlusCommissionEmployee

 

薪資計算如下表:

 

 

earning

SalariedEmployee

weeklySalary

CommissionEmployee

Commission*grossSales

BasePlus-

CommissionEmployee

baseSalary+

( Commission*grossSales )


 

 

 #include <string>
#include <vector>
#include <iostream>
using namespace std;

/*Employee abstract base class.*/
class Employee
{
    public:
        Employee( const string & );
        void setName( const string & );
        string getName() const;

//your code...
 
    private:    
        string Name;    
};
Employee::Employee( const string & name ): Name(name)
{}
void Employee::setName( const string &name )
{
    Name = name;
}
string Employee::getName() const
{
    return Name;
}
void Employee::print() const
{
    cout << getName();
}

/*SalariedEmployee class derived from Employee.*/
class SalariedEmployee : public Employee
{
    public:
        SalariedEmployee( const string &, double = 0.0 );
        void setWeeklySalary( double );
        double getWeeklySalary() const;

        //your code...
    private:    
        double weeklySalary;    
};


/*CommissionEmployee class derived from Employee.*/
class CommissionEmployee : public Employee
{
    public:
        CommissionEmployee(const string&, double = 0.0, double = 0.0);
        void setGrossSales( double );
        double getGrossSales() const;
        void setCommissionRate( double );
        double getCommissionRate() const;

        //your code...
    private:        
        double grossSales;
        double commissionRate;
};

/*BasePlusCommissionEmployee class derived from CommissionEmployee.*/
class BasePlusCommissionEmployee : public CommissionEmployee
{
    public:
        BasePlusCommissionEmployee(const string&, double = 0.0, double = 0.0, double = 0.0 );
        void setBaseSalary( double );
        double getBaseSalary() const;

        //your code...
    private:        
        double baseSalary;
};
void virtualViaPointer( const Employee * const);

int main()
{
string name1,name2,name3;
double salary, sales, rate, basesales, baserate, basesalary;
while( cin >> name1 >> salary    >>
     name2 >> sales     >> rate     >>
     name3 >> basesales >> baserate >> basesalary)
{
SalariedEmployee salariedEmployee(name1,salary);
CommissionEmployee commissionEmployee(name2,sales,rate);
BasePlusCommissionEmployee basePlusCommissionEmployee(name3,basesales,baserate,basesalary);

vector < Employee* > employee(3);
employee[0] = &salariedEmployee;
employee[1] = &commissionEmployee;
employee[2] = &basePlusCommissionEmployee;

for( size_t i = 0; i < employee.size(); i++ )
virtualViaPointer( employee[i] );
}
}

void virtualViaPointer( const Employee * const baseClassPtr )
{
      baseClassPtr->print();
      cout << "\nearned $" << baseClassPtr->earning() << "\n\n";
}
 

Input :

每行依序輸入employee name salary,sales,rate等資料

Output :

請見範例

Sample Input :

John 800 Sue 10000 0.06 Bob 5000 0.04 300

Sample Output :

salaried employee: John
weekly salary: 800
earned $800

commission employee: Sue
gross sales: 10000; commission rate: 0.06
earned $600

base-salaried commission employee: Bob
gross sales: 5000; commission rate: 0.04; base salary: 300
earned $500

Hint :


  

Author :

(管理員:oopTA)

  Solve it!   Status Forum (0)

C++
C
JAVA
63164. oop102703013 (6 ms , 508KB)
62794. oop102703029 (6 ms , 510KB)
62697. oop100306055 (6 ms , 504KB)
62648. oop102703041 (6 ms , 504KB)
62586. oop102703039 (6 ms , 506KB)
沒有解題記錄 58693. nwgs524513cja (92 ms , 428KB)

執行時間會受很多因素影響因此僅供參考,主機等級請看這裡