Advertisement
daniv1

Task3Pllug

May 9th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.37 KB | None | 0 0
  1. // PrintEmployers.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <vector>
  7. #include <string>
  8. class Printable {
  9. public:
  10.     virtual void print() const;
  11.    
  12. };
  13.  void Printable::print()const {};
  14. class Employee : public Printable
  15. {
  16. public:
  17.     Employee() {
  18.         mName = "Vasulko";
  19.         mYear = 2014;
  20.         mSalary = 200;
  21.         mTelephoneNumber = "380631234567";
  22.         mAdress = " Lviv";
  23.     };
  24.     Employee(std::string Name, std::string TelephoneNumber, std::string Adress, double Salary, int Year) {
  25.         mName = Name;
  26.         mTelephoneNumber = TelephoneNumber;
  27.         mAdress = Adress;
  28.         mSalary = Salary;
  29.         mYear = Year;
  30.     };
  31.     ~Employee() {};
  32.  
  33.     std::string Name() ;
  34.     std::string TelephoneNumber() ;
  35.     std::string Adress() ;
  36.     double Salary() ;
  37.     int Year() ;
  38.     void setName(std::string Name) ;
  39.     void setTelephoneNumber(std::string TelephoneNumber) ;
  40.     void setAdress(std::string Adress) ;
  41.     void setSalary(double Salary) ;
  42.     void setYear(int Year) ;
  43.     virtual void print() const override ;
  44. private:
  45.     std::string mName;
  46.     std::string mTelephoneNumber;
  47.     std::string mAdress;
  48.     double mSalary;
  49.     int mYear;
  50. };
  51.  
  52. void Employee::print() const {
  53.     std::cout << mAdress << std::endl << mName << std::endl << mYear << std::endl << mTelephoneNumber << std::endl << mSalary << std::endl;
  54. };
  55. std::string Employee::Name() {
  56.     return mName;
  57. };
  58.  
  59. void Employee::setName(std::string Name) {
  60.     mName = Name;
  61. };
  62.  
  63. double Employee::Salary() {
  64.     return mSalary;
  65. };
  66.  
  67. void Employee::setSalary(double Salary) {
  68.     mSalary = Salary;
  69. };
  70.  
  71. std::string Employee::TelephoneNumber() {
  72.     return mTelephoneNumber;
  73. };
  74.  
  75. void Employee::setTelephoneNumber(std::string TelephoneNumber) {
  76.     mTelephoneNumber = TelephoneNumber;
  77. };
  78.  
  79. std::string Employee::Adress() {
  80.     return mAdress;
  81. };
  82.  
  83. void Employee::setAdress(std::string Adress) {
  84.     mAdress =Adress;
  85. };
  86.  
  87. int Employee::Year() {
  88.     return mYear;
  89. };
  90.  
  91. void Employee::setYear(int Year) {
  92.     mYear = Year;
  93. };
  94.  
  95.  
  96. class Office : public Printable
  97. {
  98. public:
  99.     Office() {
  100.         mOfficeName = "Cipipi";
  101.             mOfficeAdress = "Dragomanova 50";
  102.             mOfficeArea = 3;
  103.             mOfficeNumberOfEmployees = 2;
  104.        
  105.     };
  106.     Office(std::string OfficeName, std::string OfficeAdress, double OfficeArea, int OfficeNumberOfEmployees) {
  107.    
  108.         mOfficeName = OfficeName;
  109.         mOfficeAdress = OfficeAdress;
  110.         mOfficeArea = OfficeArea;
  111.         mOfficeNumberOfEmployees = OfficeNumberOfEmployees;
  112.     };
  113.     ~Office() {};
  114.  
  115.     std::string OfficeName();
  116.     std::string OfficeAdress();
  117.  
  118.     double OfficeArea();
  119.     int OfficeNumberOfEmployees();
  120.     void setOfficeName(std::string Name);
  121.     void setOfficeAdress(std::string Adress);
  122.     void setOfficeArea(double Area);
  123.     void setOfficeNumberOfEmployees(int NumberOfEmployees);
  124.     virtual void print() const override;
  125. private:
  126.  
  127.     std::string mOfficeName;
  128.     std::string mOfficeAdress;
  129.     double mOfficeArea;
  130.     int mOfficeNumberOfEmployees;
  131.        
  132. };
  133. std::string Office::OfficeName()
  134. {
  135.     return mOfficeName;
  136. };
  137. std::string Office::OfficeAdress()
  138. {
  139.     return mOfficeAdress;
  140. };
  141. double Office::OfficeArea()
  142. {
  143.     return mOfficeArea;
  144. };
  145. int Office::OfficeNumberOfEmployees()
  146. {
  147.     return mOfficeNumberOfEmployees;
  148. };
  149. void Office::setOfficeName(std::string Name)
  150. {
  151.     mOfficeName = Name;
  152. };
  153. void Office::setOfficeAdress(std::string Adress)
  154. {
  155.     mOfficeAdress = Adress;
  156. };
  157. void Office::setOfficeArea( double Area)
  158. {
  159.     mOfficeArea = Area;
  160. };
  161. void Office::setOfficeNumberOfEmployees(int NumberOfEmployees)
  162. {
  163.     mOfficeNumberOfEmployees = NumberOfEmployees;
  164. };
  165.  
  166.  void Office::print() const
  167. {
  168.      std::cout << mOfficeAdress <<std::endl<<mOfficeArea<<std::endl<<mOfficeName <<std::endl <<mOfficeNumberOfEmployees<<std::endl;
  169. };
  170. /*
  171. * Функція print друкує інформацію про кожен об'єкт у списку. Ідея полягає у тому, що функція
  172. * працює з нащадками класу Printable, який є базовим класом для всіх об'єктів, які можуть
  173. * роздрукувати інформацію про себе у консоль. Завдяки цьому ми можемо використовувати одну
  174. * і ту ж функцію для виводу інформації про об'єкти різних класів (головне щоб вони успадковували
  175. * від класу Printable). Тобто нам не потрібно змінювати функцію, чи писати її заново,
  176. * чи писати заново схожий код для виводу інших об'єктів - ми можемо використати функцію повторно.
  177. *
  178. * Для того, щоб функція print могла вивести інформацію про ваш клас необхідно успадкувати його
  179. * від класу Printable та перевизначити метод print().
  180. *
  181. * Параметер listToPrint - Список об'єктів про які необхідно вивести інформацію у консоль.
  182. */
  183. void printList(const std::vector<Printable *> listToPrint)
  184. {
  185.     for (auto item : listToPrint)
  186.     {
  187.         item->print();
  188.         std::cout << std::endl;
  189.     }
  190. }
  191. int main()
  192. {
  193.     Office *o1 = new Office;
  194.     Office *o2  = new Office;
  195.      std::vector <Office *> a;
  196.      a.push_back(o1);
  197.      a.push_back(o2);
  198.      printList(a);
  199.    
  200.     system("pause");
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement