NickAndNick

Абстрактные методы

Apr 10th, 2021
575
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. class Employee {
  5. public:
  6.     Employee(double value = 12792) : mrot(value) {}
  7.     virtual ~Employee() {}
  8.     virtual double salary()const {
  9.         return mrot;
  10.     }
  11. protected:
  12.     double mrot;
  13. };
  14. class Manager : virtual public Employee {
  15. public:
  16.     Manager(double a, double p) : a(a), p(p) {}
  17.     double salary()const override {
  18.         auto s = mrot * a;
  19.         return s += s * p;
  20.     }
  21. private:
  22.     double a;
  23.     double p;
  24. };
  25. class Administrator : virtual public Employee {
  26. public:
  27.     Administrator(double a) : a(a) {}
  28.     double salary()const override {
  29.         return mrot * a;
  30.     }
  31. private:
  32.     double a;
  33. };
  34. class Programmer : virtual public Employee {
  35. public:
  36.     Programmer(double a, double p, double x) : a(a), p(p), x(x) {}
  37.     double salary()const override {
  38.         auto s = mrot * a;
  39.         return s += s * p + s * x;
  40.     }
  41. private:
  42.     double a;
  43.     double p;
  44.     double x;
  45. };
  46. unsigned quantity(const char* msg) {
  47.     cout << msg;
  48.     unsigned value;
  49.     cin >> value;
  50.     cin.ignore(cin.rdbuf()->in_avail());
  51.     return value;
  52. }
  53. int main() {
  54.     Manager manager(6.5, 0.25);
  55.     Administrator administrator(4.8);
  56.     Programmer programmer(8.25, 0.15, 0.2);
  57.     auto managers = quantity("Managers: ");
  58.     auto administrators = quantity("Administrators: ");
  59.     auto programmers = quantity("Programmers: ");
  60.     auto budget
  61.         = manager.salary() * managers
  62.         + administrator.salary() * administrators
  63.         + programmer.salary() * programmers;
  64.     cout << "Budget: " << fixed << setprecision(2) << budget << '\n';
  65.     system("pause > nul");
  66. }
RAW Paste Data