C ++ ---------- study notes polymorphism and virtual functions (one of the three properties)

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Polymorphism is a very important feature of the C ++ language, want to achieve polymorphism that will rely heavily on virtual functions, let's take a look at their use and contact us now!

Definition of the usage rule polymorphism and virtual functions (additional abstract classes and pure virtual function):
Here Insert Picture Description
Here Insert Picture Description
Experiment 1:
a virtual function to achieve polymorphism function:

#include<iostream>
using namespace std;

class Base
{
public:
	void print()
	{
		cout<<"调用了基类的print函数"<<endl;
	}
	virtual void display()
	{
		cout<<"调用了基类的display函数"<<endl;
	}
};

class Derived:public Base
{
public:
	void print()
	{
		cout<<"调用了派生类的print函数"<<endl;
	}
	virtual void display()
	{
		cout<<"调用了派生类的display函数"<<endl;
	}
};

int main()
{
	Base *p;
	Derived d1;
	p=&d1;
	p->print();
	p->display();
	return 0;
}

Experiment 2:
a software company employee (Employee) can be divided into two categories of existing staff: administrative staff (Manager) and project developer (Developer). Now the company needs for unified management of personnel information, personnel number is stored, name, job level, fixed monthly bonus, calculated monthly monthly monthly income and monthly income, and can display all the information of employees.
The base staff number of 8000, an increase every time a new person is added to the order number 1 can;
administrators and project developers are divided into three levels, administrative staff divided into general manager, department managers and team leader; project developers into working for less than a year of work for more than one year and less than three years of seniority in more than three years.
The highest level of administrative staff to the general manager, a monthly salary of 12,000 yuan, the formula for calculating monthly income of 12,000 * (3 levels +1) / 3 + fixed monthly bonus of 3,500 yuan.
Project developers highest level of seniority of employees is more than three years, the monthly salary of 6,000 yuan, calculated monthly income of 6,000 * (3 levels +1) / 3 + bonus for overtime hours * 40 yuan / hour + fixed monthly bonus of 500 yuan.
Programming the above test personnel management, implemented with virtual functions reflect polymorphism.

#include <iostream>
#include <string>
using namespace std;

/*对于两类公司职员,都有其共同的特征:一个雇员的基本信息。
一个雇员的基本信息包括这个雇员的编号、姓名、职务级别、每月固定奖金和月收入信息。
所以首先声明一个公司雇员的基类Employee,包含一个基本雇员的信息,Employee类声明代码如下:*/

class Employee  //定义公司雇员的基类
{

public:
    Employee();//无参构造函数,系统分配空间。
	Employee(string name,int number,int position);//有参构造函数,分配输入值及定义变量。
	virtual void calculateMonthIncome()=0;  //计算月薪虚函数
	virtual void showInfo()=0;  //显示雇员信息虚函数

protected:
    string name;  //姓名
	 int number;  //编号
	 int position;  //职务级别
	 float monthIncome; //月收入
    float monthBonus;//每月固定奖金
};  

Employee::Employee()
{
	this->name="";
	this->number=8000;
	this->position=0;
	this->monthIncome=0;
this->monthBonus=0;
}

Employee::Employee(string name,int number,int position)
{
	this->name=name;
	this->number=number;
	this->position=position;
	this->monthIncome=0;
this->monthBonus=0;
}

//第二,声明行政管理人员类Manager,行政管理人员类Manager声明代码如下:
class Manager:  public Employee  //行政管理人员类
{

public:
    Manager();
	Manager(string name,int number,int position);
	void calculateMonthIncome();
	void showInfo();
};  

Manager::Manager()
{
	monthBonus=3500;
}

Manager::Manager(string name,int number,int position):Employee(name,number,position)
{
	monthBonus=3500;
}

void Manager::calculateMonthIncome()
{
	monthIncome=12000*(3-position+1)/3+monthBonus;
	cout<<"行政管理人员"<<name<<"  月收入为:"<<monthIncome<<"元"<<endl; 
}

void Manager::showInfo()
{
	cout<<"行政管理人员"<<name<<"  编号"<<number<<"  级别"<<position<<"  本月收入"<<monthIncome<<"元"<<endl;
	cout<<endl; 
}

/*第三,声明项目开发人员类Developer,项目开发人员比普通的雇员增加了加班的小时数和每月的固定奖金。
所以以Employee为基类派生出的Developer派生类中增加成员hourSalary表示每小时的加班费、workHours表示加班累计小时数。
所以项目开发人员类Developer声明代码如下:*/
class Developer:  public Employee  //项目开发人员类
{

public:
    Developer(){workHours=0;hourSalary=40;monthBonus=500;}
	Developer(string name,int number,int position);
	void calculateMonthIncome();
	void showInfo();

 protected:
	int hourSalary;  //每小时的加班费
	int workHours;  //加班累计小时数

};

Developer::Developer(string name,int number,int position):Employee(name,number,position)
{
	cout<<"请输入职员的加班累计小时数:";
	cin>>workHours;
	hourSalary=40;
	monthBonus=500; 
}

void Developer::calculateMonthIncome()
{
	monthIncome=6000*(3-position+1)/3+workHours*hourSalary+500;
	cout<<"项目开发人员"<<name<<"  本月收入"<<monthIncome<<"元"<<endl; 
}

void Developer::showInfo()
{
	cout<<"项目开发人员"<<name<<"  编号"<<number<<"  级别"<<position<<"  本月收入"<<monthIncome<<"元"<<endl;
	cout<<endl; 
}

void input(string &name,int &number,int &position)
{
	cout<<"请输入职员的编号:";
	cin>>number;
	while(number<8000)
	{
		cout<<"输入编号错误,请重新输入!"<<endl;
		cout<<"请输入职员的编号:";
		cin>>number;
	}
	cout<<"请输入职员的姓名:";
	cin>>name;
	cout<<"请输入职员的级别:";
	cin>>position; 
}

int main()
{
	string name;  //姓名
	int number;  //编号
	int position;  //职务级别
	Employee *p;
	input(name,number,position);
	Manager m(name,number,position);
	p=&m;
	p->calculateMonthIncome();
	p->showInfo();
	//m.calculateMonthIncome();	
	//m.showInfo();

	input(name,number,position);
	Developer d(name,number,position);
	p=&d;
	p->calculateMonthIncome();
	p->showInfo();
	//d.calculateMonthIncome();
	//d.showInfo();
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_43595030/article/details/92079746