C++ 私有继承 多重继承

workermi.h

  1. #ifndef PRO1_WORKERMI_H
  2. #define PRO1_WORKERMI_H
  3. #include <string>
  4. using namespace std;
  5. class Worker {
  6. private:
  7. string fullname;
  8. long id;
  9. protected:
  10. virtual void Data()const ;
  11. virtual void Get();
  12. public:
  13. Worker():fullname("no one"), id(0L){}
  14. Worker(const string & s, long n) :fullname(s), id(n){}
  15. virtual ~Worker();
  16. virtual void Set()=0;
  17. virtual void Show()const = 0;
  18. };
  19. class Waiter:virtual public Worker{
  20. private:
  21. int panache;
  22. protected:
  23. void Data()const ;
  24. void Get();
  25. public:
  26. Waiter():Worker(), panache(0){}
  27. Waiter(const string & s, long n, int p = 0):Worker(s, n), panache(p){}
  28. Waiter(const Worker & wk, int p = 0):Worker(wk), panache(p){}
  29. void Set();
  30. void Show()const ;
  31. };
  32. class Singer: virtual public Worker{
  33. protected:
  34. enum {other, alto, contralto, soprano, bass, baritone, tenor};
  35. enum {Vtypes = 7};
  36. void Data()const ;
  37. void Get();
  38. private:
  39. static char *pv[Vtypes];
  40. int voice;
  41. public:
  42. Singer():Worker(), voice(other){}
  43. Singer(const string & s, long n, int v = other):Worker(s, n), voice(v){}
  44. Singer(const Worker & wk, int v = other): Worker(wk), voice(v){}
  45. void Set();
  46. void Show()const ;
  47. };
  48. // 多重继承
  49. class SingingWaiter:public Singer,public Waiter{
  50. protected:
  51. void Data()const ;
  52. void Get();
  53. public:
  54. SingingWaiter(){}
  55. SingingWaiter(const string & s, long n, int p = 0, int v = other)
  56. :Worker(s, n), Waiter(s, n, p), Singer(s, n, v){}
  57. SingingWaiter(const Worker & wk, int p = 0, int v = other)
  58. :Worker(wk), Waiter(wk, p), Singer(wk, v){}
  59. SingingWaiter(const Waiter &wt, int v = other)
  60. :Worker(wt), Waiter(wt), Singer(wt, v){}
  61. SingingWaiter(const Singer & wt, int p = 0)
  62. :Worker(wt), Waiter(wt, p), Singer(wt){}
  63. void Set();
  64. void Show()const ;
  65. };
  66. #endif //PRO1_WORKERMI_H

workermi.cpp

  1. #include "workermi.h"
  2. #include <iostream>
  3. using namespace std;
  4. Worker::~Worker() {}
  5. void Worker::Data() const {
  6. cout << "Name: " << fullname << endl;
  7. cout << "Employee ID: " << id << endl;
  8. }
  9. void Worker::Get() {
  10. getline(cin, fullname);
  11. cout << "Enter worker's ID: ";
  12. cin >> id;
  13. while (cin.get()!='\n')
  14. continue;
  15. }
  16. void Waiter::Set() {
  17. cout << "Enter waiter's name: ";
  18. Worker::Get();
  19. Get();
  20. }
  21. void Waiter::Show() const {
  22. cout << "Category: waiter\n";
  23. Worker::Data();
  24. Data();
  25. }
  26. void Waiter::Data() const {
  27. cout << "Panache rating: " << panache << endl;
  28. }
  29. void Waiter::Get() {
  30. cout << "Enter waiter's panache rating: ";
  31. cin >> panache;
  32. while (cin.get()!='\n')
  33. continue;
  34. }
  35. char * Singer::pv[Singer::Vtypes] = {"other", "alto", "contralto", "soprano", "bass", "baritone", "tenor"};
  36. void Singer::Set() {
  37. cout << "Enter singer's name: ";
  38. Worker::Get();
  39. Get();
  40. }
  41. void Singer::Show() const {
  42. cout << "Category : singer \n";
  43. Worker::Data();
  44. Data();
  45. }
  46. void Singer::Data() const {
  47. cout << "Vocal range: " << pv[voice] << endl;
  48. }
  49. void Singer::Get() {
  50. cout << "Enter number for singer's vocal range:\n";
  51. int i;
  52. for(i = 0; i < Vtypes; i++){
  53. cout << i << " : " << pv[i] << " ";
  54. if (i % 4 == 3) {
  55. cout << endl;
  56. }
  57. }
  58. if (i % 4 != 0) {
  59. cout << endl;
  60. }
  61. cin >> voice;
  62. while (cin.get() != '\n') {
  63. continue;
  64. }
  65. }
  66. void SingingWaiter::Data() const {
  67. Singer::Data();
  68. Waiter::Data();
  69. }
  70. void SingingWaiter::Get() {
  71. Waiter::Get();
  72. Singer::Get();
  73. }
  74. void SingingWaiter::Set() {
  75. cout << "Enter singing waiter's name: ";
  76. Worker::Get();
  77. Get();
  78. }
  79. void SingingWaiter::Show() const {
  80. cout << "Category : singing waiter\n";
  81. Worker::Data();
  82. Data();
  83. }

main.cpp

  1. #include <iostream>
  2. #include <cstring>
  3. #include "module10_code_reuse/workermi.h"
  4. using namespace std;
  5. // 代码的重用,私有继承,多重继承
  6. const int SIZE_M = 5;
  7. void workmi(){
  8. Worker * lolas[SIZE_M];
  9. int ct;
  10. for (ct = 0; ct < SIZE_M; ct++) {
  11. char choise;
  12. cout << "Enter the employee category:\n"
  13. << "w: waiter s: singer t: singing waiter q: quit\n";
  14. cin >> choise;
  15. while (strchr("wstq", choise) == NULL) {
  16. cout << "Please enter a w, s, t, or q: ";
  17. cin >> choise;
  18. }
  19. if (choise == 'q')
  20. break;
  21. switch (choise) {
  22. case 'w': lolas[ct] = new Waiter;
  23. break;
  24. case 's': lolas[ct] = new Singer;
  25. break;
  26. case 't': lolas[ct] = new SingingWaiter;
  27. break;
  28. }
  29. cin.get();
  30. lolas[ct]->Set();
  31. }
  32. cout << "\nHere is your sraff:\n";
  33. int i;
  34. for (i = 0; i < ct; i++) {
  35. cout << endl;
  36. lolas[i]->Show();
  37. }
  38. for (i = 0; i < ct; i++)
  39. delete lolas[i];
  40. cout << "Bye.\n";
  41. }
  42. int main() {
  43. workmi();
  44. return 0;
  45. }