此章节学习了虚函数和动态联边
用抽象类实现一下员工工资的简易系统
代码如下:
#include<iostream.h>
#include<string.h>
#include<assert.h>
#include<iomanip.h>
class employee
{
public:
employee(const long, const char*); virtual ~employee();
const char*getname()const; const long getnumber()const; virtual double earnings()const=0; virtual void print()const;
protected:
long number; char*name;
};
employee::employee(const long k,const char*str)
{
number=k;
name=new char[strlen(str)+1]; assert(name!=0); strcpy(name,str);
}
employee::~employee()
{
delete []name;
}
const char*employee::getname()const
{
return name;
}
const long employee::getnumber()const
{
return number ;
}
void employee::print()const
{
cout<<number<<setw(16)<<name<<endl;
}
class manager:public employee
{
public:
manager(const long,const char*,double=0.0);
~manager(){};
void setmonthsalary(double); virtual double earnings()const; virtual void print()const;
private:
double monthsalary;
};
manager::manager(const long k, const char*str,double sal):employee(k,str){setmonthsalary(sal);}
void manager::setmonthsalary(double sal)
{
monthsalary=sal;
}
double manager::earnings()const
{
return monthsalary;
}
void manager::print()const
{
employee::print(); cout<<setw(16)<<"manager\n"; cout<<"\tearning$"<<monthsalary<<endl;
}
class salesmanager:public employee
{
public:
salesmanager(const long , const char*, double=0.0,double=0.0);
~salesmanager(){} void setsalary(double); void setsales(double);
virtual double earnings()const; virtual void print()const; protected:
double sales; double salary;};
salesmanager::salesmanager(const long k,const char*str,double salary1,double sales2):employee(k,str){setsalary(salary1);setsales(sales2);}
void salesmanager::setsalary(double salary1)
{
salary=salary1;
}
void salesmanager::setsales(double sales2)
{
sales=sales2;
}
double salesmanager::earnings()const
{
return salary+0.04*sales;
}
void salesmanager::print()const
{
employee::print(); cout<<setw(16)<<"salesmanager\n"; cout<<"\tsalary: $"<<sales<<endl; cout<<"\tearnings: $"<<earnings()<<endl;
}
class hourworker:public employee
{
public:
hourworker(const long,const char*,double=0.0,int =0);
~hourworker(){} void sethour(int);
void setmoney(double);
virtual double earnings()const; virtual void print()const;
private:
double money; int hour;
};
hourworker::hourworker(const long k, const char*str,double m,int h):employee(k,str){ setmoney(m);
sethour(h);
}
void hourworker::sethour(int h)
{
hour=h;}
void hourworker::setmoney(double m)
{
money=m;
}
double hourworker::earnings()const
{
return money*hour;
}
void hourworker::print()const
{
employee::print(); cout<<setw(16)<<"hourwoker\n"; cout<<"\tworkhour: "<<hour<<endl; cout<<"\tearning: $"<<earnings()<<endl;
}
class salesman:public employee
{
public:
salesman(const long,const char*,double=0.0);
~salesman(){}
void setsales(double);
virtual double earnings()const; virtual void print()const;
private:
double sales;
};
salesman::salesman(const long k,const char* str,double s):employee(k,str){setsales(s);} void salesman::setsales(double s)
{
sales=s;
}
double salesman::earnings()const
{
return sales*0.04;
}
void salesman::print()const
{
employee::print(); cout<<setw(16)<<"salesman\n"; cout<<"\tsales: "<<sales<<endl; cout<<"\tearning: "<<earnings()<<endl;
}
int main()
{
manager a(61,"xiaohao",100000); a.print();
salesmanager B(10,"hao",5000,10000); B.print();
hourworker C(11,"shihao",80,5); C.print();
salesman D(90,"shihao",5000); D.print();
return 0;
}