fork download
  1. //This program demostrates a class and a derived class with constructors.
  2. #include <iostream>
  3. #include <string>
  4. #include <iomanip>
  5. using namespace std;
  6. class Employee
  7. {
  8. private:
  9. string EmpName;
  10. int EmpNum;
  11. int HireDate;
  12. public:
  13. void setEmpName(std::string& name);
  14. void setEmpNum(int);
  15. void setHireDate(int);
  16. string getEmpName() const;
  17. int getEmpNum() const;
  18. int getHireDate() const;
  19. Employee();
  20. };
  21. void Employee::setEmpName(std::string& name)
  22. {
  23. EmpName = name;
  24. }
  25. void Employee::setEmpNum(int y)
  26. {
  27. EmpNum = y;
  28. }
  29. void Employee::setHireDate(int z)
  30. {
  31. HireDate = z;
  32. }
  33. string Employee::getEmpName() const
  34. {
  35. return EmpName;
  36. }
  37. int Employee::getEmpNum() const
  38. {
  39. return EmpNum;
  40. }
  41. int Employee::getHireDate() const
  42. {
  43. return HireDate;
  44. }
  45. Employee::Employee()
  46. {
  47. cout << "I will ask you some questions about an employee.\n\n";
  48. }
  49. class ProductionWorker : public Employee
  50. {
  51. private:
  52. int Shift;
  53. double HourlyPayRate;
  54. public:
  55. void setShift(int);
  56. void setHourlyPayRate(double);
  57. int getShift() const;
  58. double getHourlyPayRate() const;
  59. ProductionWorker();
  60. };
  61. void ProductionWorker::setShift(int a)
  62. {
  63. Shift = a;
  64. }
  65. void ProductionWorker::setHourlyPayRate(double b)
  66. {
  67. HourlyPayRate = b;
  68. }
  69. int ProductionWorker::getShift() const
  70. {
  71. return Shift;
  72. }
  73. double ProductionWorker::getHourlyPayRate() const
  74. {
  75. return HourlyPayRate;
  76. }
  77. ProductionWorker::ProductionWorker()
  78. {
  79. cout << "After answering the questions,\n";
  80. cout << "I will display the employee's information.\n\n\n";
  81. }
  82. int main()
  83. {
  84. ProductionWorker info;
  85. string name;
  86. int num;
  87. int date;
  88. int shift;
  89. double rate;
  90. cout << "What is the employee's name? ";
  91. getline(cin, name);
  92.  
  93. cout << "What is the employee's number? ";
  94. cin >> num;
  95.  
  96. cout << "What is the employee's hire date?\n";
  97. cout << "(Month, day, and year without any slashes,\n";
  98. cout << "dashes, commas, or other punctuation.)\n";
  99. cout << "For example, January 14, 1983 would look like 01141983. ";
  100. cin >> date;
  101.  
  102. cout << "Does the employee work shift 1 or shift 2? ";
  103. cin >> shift;
  104.  
  105. cout << "How much does the employee make per hour? ";
  106. cin >> rate;
  107. info.setEmpName(name);
  108. info.setEmpNum(num);
  109. info.setHireDate(date);
  110. info.setShift(shift);
  111. info.setHourlyPayRate(rate);
  112. cout << "\n\nHere is the employee's data:\n\n";
  113. cout << "Employee's Name: " << info.getEmpName() << endl;
  114. cout << "Employee's Number: " << info.getEmpNum() << endl;
  115. cout << "Employee's Hire Date: " << info.getHireDate() << endl;
  116. cout << "Employee's Shift: " << info.getShift() << endl;
  117. cout << setprecision(2) << fixed;
  118. cout << "Employee's Hourly Pay Rate: $" << info.getHourlyPayRate() << endl << endl;
  119. return 0;
  120. }
Success #stdin #stdout 0s 3480KB
stdin
John Doe
1782
12111983
1
2.0
stdout
I will ask you some questions about an employee.

After answering the questions,
I will display the employee's information.


What is the employee's name? What is the employee's number? What is the employee's hire date?
(Month, day, and year without any slashes,
dashes, commas, or other punctuation.)
For example, January 14, 1983 would look like 01141983. Does the employee work shift 1 or shift 2? How much does the employee make per hour? 

Here is the employee's data:

Employee's Name: John Doe
Employee's Number: 1782
Employee's Hire Date: 12111983
Employee's Shift: 1
Employee's Hourly Pay Rate: $2.00