#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;
}
c++ 多态和继承,虚类的作用
猜你喜欢
转载自blog.csdn.net/becky_w/article/details/80074663
今日推荐
周排行