1.实验名称:实验七 公司工资管理程序
2.实验时间:2008-4-1
3.实验目的:编程用面向对象设计思想分析、设计并实现实验七的内容。
4.实验内容:
一、编写程序实现公司工资管理程序,其中抽象基类包括工资salary和员工staff姓名两个数据成员,提供工资计算和工资显示两个虚函数。派生类包括雇主master、推销员salesman、记件piecepayworker工资工人、记时hourpayworker工资工人,其中雇主工资固定为10000元,推销员工资为基本工资加推销业绩提成,记件工人工资为基本工资加记件提成,记时工工资为小时数乘以每小时工资。试完成上述各类的声明、定义并进行测试。
二、定义一个有10个字符串的数组,用起泡排序算法对其进行排序并输出排序后的结果(提示使用strcmp函数比较字符串的大小)。
//程序源代码
//SalaryManagement.cpp
#include <iostream.h>
#include <string.h>
class Staff
{
protected:
double salary;
char *sName;
public:
Staff()
{
char name[30];
salary=800;
cout<<"姓名:";
cin>>name;
sName=new char[strlen(name)+1];
strcpy(sName,name);
}
virtual ~Staff()
{delete[] sName;}
virtual void calSalary(){};
virtual void dispMsg(){};
};
class Master:public Staff
{
public:
Master()
{Staff::salary=10000;}
void calSalary()
{
salary=Staff::salary;
}
void dispMsg()
{
cout<<"雇主的工资是:"<<salary<<endl;
}
};
class Salesman:public Staff
{
private:
double sales;
double rate;
public:
Salesman(): rate(0.01)
{
cout<<"销售员的销售业绩:";
cin>>sales;
}
void calSalary()
{salary=salary+sales*rate;}
void dispMsg()
{
cout<<"销售员的工资是:"<<salary<<endl;
}
};
class PiecePayWorker:public Staff
{
private:
int pieces;
double rate;
public:
PiecePayWorker():rate(0.01)
{
cout<<"记件工人的件数:";
cin>>pieces;
}
void calSalary()
{salary=salary+pieces*rate;}
void dispMsg()
{
cout<<"记件工人的工资是:"<<salary<<endl;
}
};
class HourPayWorker:public Staff
{
private:
double hours;
double hourPay;
public:
HourPayWorker():hourPay(20)
{
cout<<"记时工人工作小时数:";
cin>>hours;
}
void calSalary()
{salary=hours*hourPay;}
void dispMsg()
{
cout<<"记时工人的工资是:"<<salary<<endl;
}
};
void main()
{
Master staff1;
Salesman staff2;
PiecePayWorker staff3;
HourPayWorker staff4;
Staff *ptrStaff;
ptrStaff=&staff1;
ptrStaff->calSalary();
ptrStaff->dispMsg();
ptrStaff=&staff2;
ptrStaff->calSalary();
ptrStaff->dispMsg();
ptrStaff=&staff3;
ptrStaff->calSalary();
ptrStaff->dispMsg();
ptrStaff=&staff4;
ptrStaff->calSalary();
ptrStaff->dispMsg();
}
/**************************************************************************
* (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
//实验结果
/*
姓名:master
姓名:salesman
销售员的销售业绩:10000
姓名:piecepayworker
记件工人的件数:150
姓名:hourpayworker
记时工人工作小时数:50
雇主的工资是:10000
销售员的工资是:900
记件工人的工资是:801.5
记时工人的工资是:1000
Press any key to continue
*/
//string.h
#include <iostream.h>
#include <stdio.h>
#include <string.h>
void scan(char **p,int n)
{
for(int i=0;i<n;i++)
{
*p=new char [10];
gets(*p);
// puts(*p);
p++;
}
}
void print(char **p,int n)
{
printf("排序结果为:/n");
for(int i=0;i<n;i++)
{
puts(p[i]);
}
}
/*********************起泡排序***************************/
void Bubble_Sort(char **p,int n)
{
char temp[30];
int jtemp;
printf("*******起泡排序*******/n");
for(int i=1;i<=n-1;i++)
for(int j=0;j<n-i;j++)
{
jtemp=j;
if(strcmp(p[jtemp],p[jtemp+1])>0)
{
strcpy(temp,p[j]);
strcpy(p[j],p[j+1]);
strcpy(p[j+1],temp);
}
}
}
void main()
{
char *s[10];
cout<<"请输入10个字符串:"<<endl;
scan(s,10);
Bubble_Sort(s,10);
print(s,10);
}
/*
请输入10个字符串:
abc
abb
aba
acc
acb
aca
aac
aab
aaa
aaaa
*******起泡排序*******
排序结果为:
aaa
aaaa
aab
aac
aba
abb
abc
aca
acb
acc
Press any key to continue
*/
5.实验总结:由于有课本的综合实例教职工工资管理程序做模板,做公司工资管理程序就比较容易了,对派生中类的多态性有更深的理解,以及虚函数的应用,还可以继续完善。
用二级指针指向指针数组来操控指针数组的方法。