c++ 多态和继承,虚类的作用

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+50;
const int mod=1e9+7;
class Employee
{
private:
    string firstname,lastname;
    string ssn;
    double earning;
public:
    Employee(string a,string b,string id,double e):firstname(a),lastname(b),ssn(id),earning(e){}
    virtual double getearnings();
    string getfirst()
    {
        return firstname;
    }
    string getlast()
    {
        return lastname;
    }
    string getssn()
    {
        return ssn;
    }
    virtual void toString();
};
double Employee:: getearnings()
{
    return earning;
}

void Employee:: toString()
{
    cout<<firstname<<" "<<lastname<<endl;
    cout<<"social security number:"<<ssn<<endl;
}
class hourlyEmployee:public Employee
{
private:
    double hours,wage;
public:
    hourlyEmployee(string a,string b,string  id,double e,int h,double w): Employee(a,b,id,e),hours(h),wage(w){}
    double getearnings()
    {
        if(hours<=40)
        {
            return wage*hours;
        }
    }
    void toString()
    {
        printf("hourly employee: ");
        cout<<getfirst()<<" "<<getlast()<<endl;
        printf("social security number:");
        cout<<getssn()<<endl;
        printf("hourly wage:%lf,hours:%d\n",wage,hours);
    }

};
class SalariedEmployee:public Employee
{
private:
    double weeklySalary;
public:
    SalariedEmployee(string a,string b,string id,double e,double tt):Employee(a,b,id,e),weeklySalary(tt){}
    double getearnings()
    {
        return weeklySalary;
    }
        void toString()
    {
        printf("Salaried Employee: ");
        cout<<this->getfirst()<<" "<<this->getlast()<<endl;
        printf("social security number:");
        cout<<this->getssn()<<endl;
        printf("weekly salary :%lf \n",weeklySalary);
    }

};

class CommissionEmployee:public Employee
{
private:
    double conmissionRate,grossSales;
public:
    CommissionEmployee(string a,string b,string id,double e,double a1,double b1):Employee(a,b,id,e),conmissionRate(a1),grossSales(b1){}

    double getearnings()
    {
        return conmissionRate*grossSales;
    }
        void toString()
    {
        printf("Commission Employee: ");
        cout<<this->getfirst()<<" "<<this->getlast()<<endl;
        printf("social security number:");
        cout<<this->getssn()<<endl;
        printf("gross sales :%lf \n",grossSales);
        printf("commission rate: %lf \n",conmissionRate);
    }

};

class BasedEmployee:public Employee
{
private:
    double conmissionRate,grossSales,basesalary;
public:
    BasedEmployee(string a,string b,string id,double e,double a1,double b1,double b3):Employee(a,b,id,e),conmissionRate(a1),grossSales(b1),basesalary(b3){}

    double getearnings()
    {
        return conmissionRate*grossSales+basesalary;
    }
    void toString()
    {
        printf("Based Employee: ");
        cout<<this->getfirst()<<" "<<this->getlast()<<endl;
        printf("social security number:");
        cout<<this->getssn()<<endl;
        printf("gross sales :%lf \n",grossSales);
        printf("commission rate: %lf \n",conmissionRate);
         printf("base salary: %lf \n",basesalary);
    }

};
void fun(Employee * eml)
{
    eml->toString();
}
int main()
{
    hourlyEmployee a("becky","w","1001",15,41,600);
    SalariedEmployee b("becky","w","1001",15,70);
    BasedEmployee c("becky","w","1001",15,10,20,30);
    CommissionEmployee d("becky","w","1001",15,100,200);
    Employee *em[]={&a,&b,&c,&d};
    for(int i=0;i<4;i++)
    {
        fun(em[i]);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值