C++ Experiment 06 (02) College Staff-Polymorphism

Title description
The staff of a college includes teachers and administrative staff. Among them, administrative staff have basic salary and job allowance, and teachers have basic salary and class allowance (class allowance = class hours and hour class remuneration (yuan/hour)). It is known
that the annual salary of administrative staff = basic salary
12 +
the annual salary of post allowance teachers = Basic salary 12 + hours and hours class salary
definition abstract class Person class, data members have job ID, name, basic salary (double type); member functions have parameter constructor, virtual function print(), pure virtual function calSalary( ); Define the publicly derived teacher and administrative staff of Person, and add corresponding data members (all double types). Calculate and output the annual salary of various personnel.
The main function defines the teacher class and administrative staff class objects, the initial values ​​of which are input by the keyboard, use the polymorphism of C++, use the base class pointer to call the virtual function, calculate the annual salary of various personnel and output the
input description of the
administrative staff class object Initial value The initial value of the
teacher object
Output description
The job number, name, and annual salary of the administrative staff and teacher object
Input sample
1001 Zhao Yun 4500 1500
10003 Liu Xuande 6700 120 50
Output sample
Job number: 1001
Name: Zhao Yun
Annual salary: 55500
workers Number: 10003
Name: Liu Xuande
Annual Salary: 86400

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

class Person
{
    
    
public:
	Person(int a, string b, double c);
	virtual void print();
	virtual void calSalary() = 0;
	double GetWage()
	{
    
    
		return Wage;
	}
private:
	int Num;
	string Name;
	double Wage;
};

Person::Person(int a, string b, double c)
{
    
    
	Num=a;
	Name=b;
	Wage=c;
}

void Person::print()
{
    
    
	cout << "工号:" << Num << endl;
	cout << "姓名:" << Name<< endl;
}
class Teacher : public Person
{
    
    
public:
	Teacher(int a, string b, double c,double d,double e);
	virtual void print();
	void calSalary();
private:
	double HourlyPay;
	double Hours;
};

void Teacher::print()
{
    
    
	Person::print();
	Teacher::calSalary();
}

void Teacher::calSalary()
{
    
    
	cout << "年薪:" << GetWage() * 12 + Hours * HourlyPay << endl;
}
Teacher::Teacher(int a, string b, double c,double d,double e):Person(a, b, c)
{
    
    
	Hours=d;
	HourlyPay=e;
}

class Administrator : public Person
{
    
    
public:
	Administrator(int a, string b, double c, double d);
	virtual void print();
	void calSalary();
private:
	double PostAllowance;
};
Administrator::Administrator(int a, string b, double c, double d) :Person(a, b, c)
{
    
    
	PostAllowance = d;
}
void Administrator::calSalary()
{
    
    
	cout << "年薪:" << GetWage() * 12 + PostAllowance << endl;
}
void Administrator::print()
{
    
    
	Person::print();
	Administrator::calSalary();
}
int main()
{
    
    
	int Num1;
	string Name1;
	double Wage1;
	double PostAllowance;
	int Num2;
	string Name2;
	double Wage2;
	double HourlyPay;
	double Hours;
	cin >> Num1 >> Name1 >> Wage1 >> PostAllowance >> Num2 >> Name2 >> Wage2 >> Hours >> HourlyPay;
	Administrator A(Num1,Name1,Wage1,PostAllowance);
	Teacher B(Num2, Name2, Wage2, Hours, HourlyPay);
	Person *p1, *p2;
	p1 = &A;
	p2 = &B;
	p1->print();
	p2->print();
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44179485/article/details/105452268