C++面向对象-类继承练习

      假设某销售公司有一般员工,销售员工和销售经理。月工资计算办法:

      一般员工月薪=基本工资

      销售员工月薪=基本工资+销售额*提成率 

      销售经理月薪=基本工资+职务工资+销售额提成率

      编写程序,定义一个表示基本员工的基类Employee,他包含三个表示员工基本信息的成员:

编号number,姓名name,和基本工资basicSalary。

由Employee类派生销售员工Salasman类,Salasman类包含两个新数据成员:销售额sales和静态数据成员提成比例commrate。

再由Salasman类派生表示销售经理的Salamanager类。Salasmanager类包含新书局成员:岗位工资jobSalary。

     为这些类定义初始化数据的构造函数,以及输入数据Input 计算工资Pay 和输出工资条Print的成员函数。

该公司员工基本工资2000元,销售经理的岗位工资3000元,提成率=5/1000。在main函数中,输入若干不同的员工信息测试你的类结构。

 

 

代码如下:

仅供个人学习使用。

 

/*员工工资管理*/
#include<iostream>
#include<string>
using namespace std;
class Employee
{
public:
	Employee(string number1=" ",string name1=" ",double bsalary=2000):number(number1),name(name1),basicSalary(bsalary){} 
	void Input();
	void Print();
protected:
	string number;
	string name;
	double basicSalary;
			
};
void Employee::Input()
{
	cout<<"编号:"; 
	cin>>this->number;
	cout<<"姓名:";
	cin>>this->name; 
}
void  Employee::Print()
{
	cout<<"编号:"<<this->number<<"\t普通员工:"<<this->name<<"\t本月工资:"<<this->basicSalary<<endl;
}
class Salesman:public Employee
{
public:
	Salesman(string number1=" ",string name1=" ",int sales1=0,double bsalary=2000):Employee(number1,name1,bsalary)
	{
		sales=sales1;
	}
	void Input()
	{
		Employee::Input();
		cout<<"销售额:";
		cin>>this->sales; 
	}
	double Pay()
	{
		double salary;
		salary=this->basicSalary+(commrate*this->sales);
		return salary;
	}
	void Print()
	{
		cout<<"编号:"<<this->number<<"\t销售员:"<<this->name<<"\t本月工资:"<<this->Salesman::Pay()<<endl; 
	}
protected:
	int sales;
	static double commrate;
};
double Salesman::commrate=5.0/1000.0; //静态数据成员类内声明,类外定义 
class Salesmanager:public Salesman
{
public:
	Salesmanager(string number1=" ",string name1=" ",int sales1=0,double bsalary=2000,double jsalary=1000):Salesman(number1,name1,sales1,bsalary)
	{
		jobSalary=jsalary;
	}
	void Input()
	{
		Employee::Input();
		cout<<"部门总销售额:";
		cin>>this->sales;
	}
	double Pay()
	{
		double salary;
		salary=this->jobSalary+this->basicSalary+(commrate*this->sales);
		return salary;
	}
	void Print()
	{
		cout<<"编号:"<<this->number<<"\t销售经理:"<<this->name<<"\t本月工资:"<<this->Salesmanager::Pay()<<endl;
	}
protected:
	double jobSalary;
};
int main()
{
	cout<<"普通员工测试:"<<endl;
	Employee emp1;
	emp1.Input();
	emp1.Print();
	cout<<endl;
	cout<<"销售员工测试:"<<endl;
	Salesman emp2;
	emp2.Input();
	emp2.Print();
	cout<<endl;
	cout<<"销售经理测试:"<<endl;
	Salesmanager emp3;
	emp3.Input();
	emp3.Print(); 
	return 0;
} 


    

  • 9
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值