利用抽象類別及多型觀念建立薪資計算系統
#員工及薪資計算請看投影片
-----------------------------
#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";
}
每行依序輸入employee name 及salary,sales,rate等資料
請見範例
John 800 Sue 10000 0.06 Bob 5000 0.04 300
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
編號 | 身分 | 題目 | 主題 | 人氣 | 發表日期 |
沒有發現任何「解題報告」 |