9_77.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:2k
源码类别:

文章/文档

开发平台:

C/C++

  1. #include<iostream.h>
  2. #include<string.h>
  3. class employee
  4. {
  5. protected:
  6. int no;
  7. char *name;
  8. float salary;
  9. static int totalno;
  10. public:
  11. employee()
  12. {
  13. char temname[20];
  14. cout<<"执行employee构造函数!"<<endl;
  15.         no=totalno++;
  16. cout<<"职工姓名:";
  17. cin>>temname;
  18. name=new char[strlen(temname)+1];
  19. strcpy(name,temname);
  20. salary=0;
  21. }
  22. ~employee()
  23. cout<<"执行employee析构函数!"<<endl;
  24. delete[] name;
  25. }
  26. void pay(){}
  27. void display(){}
  28. };
  29. class technician:public employee
  30. {
  31. private:
  32. float hourlyrate;
  33. int workhours;
  34. public:
  35. technician()
  36. {cout<<"执行technician构造函数!"<<endl;hourlyrate=100;}
  37. void pay()
  38. {
  39. cout<<name<<"本月工作时数:";
  40. cin>>workhours;
  41. salary=hourlyrate*workhours;
  42. }
  43. void display()
  44. {
  45. cout<<"兼职技术人员"<<name<<"(编号为"<<no
  46. <<")"<<"本月工资:"<<salary<<endl;
  47. }
  48. };
  49. int employee::totalno=100;  //静态数据成员totalno在类外进行初始化
  50. class saleman:virtual public employee
  51. {
  52. protected:
  53. float commrate;
  54. float sales;
  55. public:
  56. saleman()
  57. {cout<<"执行saleman构造函数!"<<endl;commrate=0.04f;}
  58. void pay()
  59. {
  60. cout<<name<<"本月销售额:";
  61. cin>>sales;
  62. salary=sales*commrate;
  63. }
  64. void display()
  65. {
  66. cout<<"销售员"<<name<<"(编号为"<<no<<")"
  67. <<")"<<"本月工资:"<<salary<<endl;
  68. }
  69. };
  70. class manager:virtual public employee
  71. {
  72. protected:
  73. float monthlypay;
  74. public:
  75. manager()
  76. cout<<"执行manager构造函数!"<<endl;
  77. monthlypay=8000; 
  78. }
  79. void pay(){salary=monthlypay;}
  80. void display()
  81. {
  82. cout<<"经理"<<name<<"(编号为"<<no<<")"
  83. <<")"<<"本月工资:"<<salary<<endl;
  84. }
  85. };
  86. class salesmanager:public manager,public saleman
  87. {
  88. public:
  89. salesmanager()
  90. {
  91. cout<<"执行salesmanager构造函数!"<<endl;
  92. monthlypay=5000;
  93. commrate=0.005f;
  94. }
  95. void pay()
  96. {
  97. cout<<name<<"所管部门月销售量:";
  98. cin>>sales;
  99. salary=monthlypay+commrate*sales;
  100. }
  101. void display()
  102. {
  103. cout<<"销售经理"<<name<<"(编号为"<<no<<")"
  104. <<")"<<"本月工资:"<<salary<<endl;
  105. }
  106. };
  107. void main()
  108. {
  109. manager m1;
  110. technician t1;
  111. saleman s1;
  112. cout<<endl<<"请注意以下建立类salesmanager的对象的过程!"<<endl;
  113. salesmanager sm1;
  114. m1.pay();
  115. m1.display();
  116. t1.pay();
  117. t1.display();
  118. s1.pay();
  119. s1.display();
  120. sm1.pay();
  121. sm1.display();
  122. }