c++中virtual的用法实践(六)

#include<iostream>

using namespace std;

class Employee							//员工类
{
public:
	int m_id;
	char *m_name;
	double m_money;
public:
	static int m_num;
	Employee(char *n);
	virtual double Getmoney()=0;
	virtual void show()=0;
};

int Employee::m_num = 1000;

Employee::Employee(char *n)
{
	m_name = new char [10];
	strcpy(m_name,n);
	m_id = m_num;
	m_num++;
}


class Sales									//销售额类
{
protected:
	static int m_sales_sum;
	int m_sales;
	
public:
	Sales(int s);
};

int Sales::m_sales_sum=0; 

Sales::Sales(int s)
{
	m_sales = s;
	m_sales_sum += m_sales;
}


class Manager:public Employee
{
public:
	Manager(char *n);
	double Getmoney();
	void show();
};

Manager::Manager(char *n):Employee(n)
{
	m_money = 8000;
}

double Manager::Getmoney()
{
	return m_money;
}

void Manager::show()
{
	cout<<" 姓名 "<<m_name<<"职位: 经理 "<<" 工号 "<<m_id<<" 工资 "<<m_money<<endl;
}




class Technician :public Employee
{
protected:
	int m_time;
public:
	Technician(char *n,int t);
	double Getmoney();
	void show();
};

Technician::Technician(char *n,int t):Employee(n)
{
	m_time = t;
}

double Technician::Getmoney()
{
	m_money =100*m_time;
	return m_money;
}

void Technician::show()
{
	cout<<" 姓名 "<<m_name<<"职位: 工程师 "<<" 工号 "<<m_id<<" 工资 "<<m_money<<endl;
}


class Saleman:public Employee,public Sales
{
public:
	Saleman(char *n,int s);
	double Getmoney();
	void show();
};

Saleman::Saleman(char *n,int s):Employee(n),Sales(s)
{
	m_money = 0.04*m_sales;
}

double Saleman:: Getmoney()
{
	return m_money;
}

void Saleman::show()
{
	cout<<" 姓名 "<<m_name<<"职位: 销售员 "<<" 工号 "<<m_id<<"销售额:"<<m_sales<<" 工资 "<<m_money<<endl;
}



class Salemanager:public Employee,public Sales
{

public:
	Salemanager(char *n,int s=0);
	double Getmoney();
	void show();
};

Salemanager::Salemanager(char *n,int s):Employee(n),Sales(s)
{
	
}


double Salemanager:: Getmoney()
{
	m_money = 4000+0.04*m_sales_sum;
	return m_money;
}

void Salemanager::show()
{
	cout<<"姓名"<<m_name<<"职位: 销售经理 "<<"工号 "<<m_id<<"销售总额"<<m_sales_sum<<"工资 "<<m_money<<endl;
}


int main()
{
	srand(time(NULL));
	Employee *m = new Manager("manager");
	m->show();
	char name[20]={0};
	
	Employee *ps[3];
	for(int i=0;i<3;i++)
	{
		sprintf(name,"test%d",i+2);
		ps[i] = new Saleman(name,rand()%100000);
	}
	for(int i=0;i<3;i++)
	{
		ps[i]->Getmoney();
		ps[i]->show();
	}
	
	Employee *pt[3];
	
	for(int i=0;i<3;i++)
	{
		sprintf(name,"Technician%d",i+2);
		pt[i] = new Technician(name,rand()%300);
	}
	
	for(int i=0;i<3;i++)
	{
		pt[i]->Getmoney();
		pt[i]->show();
	}
	
	Employee *pe = new Salemanager("Salemanager");
	pe->Getmoney();
	pe->show();
	
	
	
	return 0;
}


答案为随机取值:
 姓名 manager职位: 经理  工号 1000 工资 8000
 姓名 test2职位: 销售员  工号 1001销售额:74708 工资 2988.32
 姓名 test3职位: 销售员  工号 1002销售额:98858 工资 3954.32
 姓名 test4职位: 销售员  工号 1003销售额:436 工资 17.44
 姓名 Technician2职位: 工程师  工号 1004 工资 4000
 姓名 Technician3职位: 工程师  工号 1005 工资 4000
 姓名 Technician4职位: 工程师  工号 1006 工资 1400
姓名Salemanager职位: 销售经理 工号 1007销售总额174002工资 10960.1

猜你喜欢

转载自blog.csdn.net/chenlj_1/article/details/80024431
今日推荐