constk

A bit of inheritance

Sep 17th, 2020
85
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include "Windows.h"
  4.  
  5. using namespace std;
  6.  
  7. class Employee
  8. {
  9. public:
  10.     Employee() : name("dafault"), id(0) {}
  11.     Employee(string name, int id) : name(name), id(id) {}
  12.     ~Employee() {}
  13.  
  14.     string getName() { return name; }
  15.     int getId() { return id; }
  16.  
  17.     void setName(string name) { this->name = name; }
  18.     void setId(int id) { this->id = id; }
  19.  
  20.     void show() { cout << id << " > " << name + "." << endl; }
  21.  
  22. protected:
  23.     string name;
  24.     int id;
  25. };
  26.  
  27. class Manager : public Employee
  28. {
  29. public:
  30.     Manager() : Employee(), position("default"), fitnessTaxes(0) {}
  31.     Manager(string name, int id, string position, double fitnessTaxes) : Employee(name, id), position(position), fitnessTaxes(fitnessTaxes) {}
  32.     ~Manager() {}
  33.  
  34.     string getPosition() { return position; }
  35.     double getFitnessTaxes() { return fitnessTaxes; }
  36.  
  37.     void setPosition(string position) { this->position = position; }
  38.     void setFitnessTaxes(double fitnessTaxes) { this->fitnessTaxes = fitnessTaxes; }
  39.  
  40.     void show() { cout << id << " > " << name + ", " + position + " pays for fitness " << fitnessTaxes << " bucks." << endl; }
  41.  
  42. protected:
  43.     string position;
  44.     double fitnessTaxes;
  45. };
  46.  
  47. class Scientist : public Employee
  48. {
  49. public:
  50.     Scientist() : Employee(), amountOfArticles(0) {}
  51.     Scientist(string name, int id, int amountOfArticles) : Employee(name, id), amountOfArticles(amountOfArticles) {}
  52.     ~Scientist() {}
  53.  
  54.     int getAmountOfArticles() { return amountOfArticles; }
  55.  
  56.     void setAmountOfArticles(int amountOfArticles) { this->amountOfArticles = amountOfArticles;  }
  57.  
  58.     void show() { cout << id << " > " << name + ", published " << amountOfArticles << " articles." << endl; }
  59.  
  60. protected:
  61.     int amountOfArticles;
  62. };
  63.  
  64. class Worker : public Employee
  65. {
  66. public:
  67.     Worker() { Employee(); }
  68.     Worker(string name, int id) : Employee(name, id) {}
  69.  
  70.     void show() { cout << id << " > " << name + " is a bricklayer, works for three days, without salary!!!" << endl; }
  71.  
  72.     ~Worker() {}
  73. };
  74.  
  75. class Tmp
  76. {
  77. public:
  78.     // What's why you should be carefull using initialisation lists
  79.     // x = y will done first, but y is still unititialised
  80.     // y = 1 will done second cause of class fields order ("int x" is wrote before "int y")
  81.     Tmp(int x, int y) : y(1), x(this->y) { cout << endl << "x = " << this->x << endl << "y = " << this->y << endl; }
  82.  
  83. private:
  84.     int x;
  85.     int y;
  86. };
  87.  
  88. int main()
  89. {
  90.     SetConsoleCP(1251);
  91.     SetConsoleOutputCP(1251);
  92.  
  93.     Employee * e = new Employee("Todd Howard", 0);
  94.     e->show();
  95.  
  96.     Manager * m = new Manager("Kim Chen In", 1, "the dictator", 300);
  97.     m->show();
  98.  
  99.     Scientist * s = new Scientist("Popov", 2, 150);
  100.     s->show();
  101.  
  102.     Worker * w = new Worker("Rabotyaga", 3);
  103.     w->show();
  104.  
  105.     //Tmp * tmp = new Tmp(1, 2);
  106.  
  107.     getchar();
  108.     return 0;
  109. }
RAW Paste Data