抽象类小案例

 /*
编写一个C++程序, 计算程序员( programmer )工资
1 要求能计算出初级程序员( junior_programmer ) 中级程序员 ( mid_programmer )高级程序员( adv_programmer)的工资
2 要求利用抽象类统一界面,方便程序的扩展, 比如:新增, 计算 架构师 (architect ) 的工资
*/
#include "iostream"
using namespace std;

class programmer               //接口
{
public:
	virtual void getsal() = 0;
};

class junior_programmer : public programmer   //初级
{
private:
	char *name;
	char *job;
	int sal;
public:
	junior_programmer(char *name, char *job, int sal) 
	{
		this->name = name;
		this->job = job;
		this->sal = sal;
	}
	virtual void getsal()
	{
		cout << name << " " << job << " " << sal << endl;
	}
};
class mid_programmer : public programmer      //中级
{
private:
	char *name;
	char *job;
	int sal;
public:
	mid_programmer(char *name, char *job, int sal)
	{
		this->name = name;
		this->job = job;
		this->sal = sal;
	}
	virtual void getsal()
	{
		cout << name << " " << job << " " << sal << endl;
	}
};
class adv_programmer : public programmer      // 高级
{
private:
	char *name;
	char *job;
	int sal;
public:
	adv_programmer(char *name, char *job, int sal)
	{
		this->name = name;
		this->job = job;
		this->sal = sal;
	}
	virtual void getsal()
	{
		cout << name << " " << job << " " << sal << endl;
	}
};


class architect : public programmer          //新增架构师
{
private:
	char *name;
	char *job;
	int sal;
public:
	architect(char *name, char *job, int sal)
	{
		this->name = name;
		this->job = job;
		this->sal = sal;
	}
	virtual void getsal()
	{
		cout << name << " " << job << " " << sal << endl;
	}
};

void GetSal(programmer *base)
{
	base->getsal();           //产生多态
}

void main()
{
	junior_programmer	jp("小李", "初级", 4000);
	mid_programmer		mp("小王", "初级", 8000);
	adv_programmer		ap("小张", "初级", 10000);
	architect						at("老孙", "架构师", 20000);

	GetSal(&jp);
	GetSal(&mp);
	GetSal(&ap);
	GetSal(&at);

	system("pause");
}

 

你可能感兴趣的:(抽象类小案例)