前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++实例员工工资系统

C++实例员工工资系统

作者头像
Twcat_tree
发布2022-11-30 17:00:54
4180
发布2022-11-30 17:00:54
举报
文章被收录于专栏:二猫の家

此章节学习了虚函数和动态联边 用抽象类实现一下员工工资的简易系统 代码如下:

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-05-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档