Advertisement
MeehoweCK

Untitled

Dec 7th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. // main.cpp
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. #include "employee.h"           //defines class Employee
  6. #include "map_template.h"       //defines template map_template<Key,Value>
  7.  
  8. int main(void)
  9. {
  10.     typedef unsigned int ID;                            //Identification number of Employee
  11.     map_template<ID,Employee> Database;                 //Database of employees
  12.  
  13.     Database.Add(761028073,Employee("Jan Kowalski","salesman",28));     //Add first employee: name: Jan Kowalski, position: salseman, age: 28,
  14.     Database.Add(510212881,Employee("Adam Nowak","storekeeper",54));    //Add second employee: name: Adam Nowak, position: storekeeper, age: 54
  15.     Database.Add(730505129,Employee("Anna Zaradna","secretary",32));    //Add third employee: name: Anna Zaradna, position: secretary, age: 32
  16.  
  17.     cout << Database << endl;                           //Print databese
  18.  
  19.     map_template<ID,Employee> NewDatabase = Database;   //Make a copy of database
  20.  
  21.     Employee* pE;
  22.     pE = NewDatabase.Find(510212881);                   //Find employee using its ID
  23.     pE->Position = "salesman";                          //Modify the position of employee
  24.     pE = NewDatabase.Find(761028073);                   //Find employee using its ID
  25.     pE->Age = 29;                                       //Modify the age of employee
  26.  
  27.     //Database = NewDatabase;                               //Update original database
  28.  
  29.     cout << Database << endl;                           //Print original databese
  30. };
  31.  
  32. // employee.h
  33. #include <iostream>
  34.  
  35. using namespace std;
  36.  
  37. class Employee
  38. {
  39. public:
  40.     string Name;
  41.     string Position;
  42.     unsigned Age;
  43.     Employee(string, string, unsigned);
  44.     Employee();
  45.     friend ostream& operator << (ostream&, const Employee&);
  46. };
  47.  
  48. //map_template.h
  49. #include <iostream>
  50. #include <vector>
  51.  
  52. using namespace std;
  53.  
  54. template<class A, class B>
  55. struct Rekord
  56. {
  57.     A nr;
  58.     B wartosc;
  59. };
  60.  
  61. template <class A, class B>
  62. class map_template
  63. {
  64.     vector<Rekord<A, B>> tablica;
  65.     unsigned wielkosc;
  66. public:
  67.     map_template();
  68.     ~map_template() {tablica.clear();}
  69.     void Add(A, B);
  70.     B* Find(A);
  71.     unsigned size() const {return wielkosc;}
  72.     friend ostream& operator << (ostream&, const map_template<A,B>&);
  73. };
  74.  
  75. template<class A, class B>
  76. map_template<A, B>::map_template() : wielkosc(0)
  77. {
  78.     tablica.clear();
  79. }
  80.  
  81. template <class A, class B>
  82. void map_template<A, B>::Add(A a, B b)
  83. {
  84.     Rekord<A, B> nowy;
  85.     nowy.nr = a;
  86.     nowy.wartosc = b;
  87.     tablica.push_back(nowy);
  88.     ++wielkosc;
  89. }
  90.  
  91. template <class A, class B>
  92. B* map_template<A, B>::Find(A szukana)
  93. {
  94.     B* wskaznik = nullptr;
  95.  
  96.     for(unsigned i = 0; i < wielkosc; ++i)
  97.     {
  98.         if(tablica[i].nr == szukana)
  99.         {
  100.             wskaznik = &(tablica[i].wartosc);
  101.             return wskaznik;
  102.         }
  103.     }
  104.     return wskaznik;
  105. }
  106.  
  107. template <class A, class B>
  108. ostream& operator << (ostream& out, const map_template<A,B>& obiekt)
  109. {
  110.     for(unsigned i = 0; i < obiekt.wielkosc; ++i)
  111.     {
  112.         out << obiekt.tablica[i].nr << " " << obiekt.tablica[i].wartosc << endl;
  113.         return out;
  114.     }
  115. }
  116.  
  117. //employee.cpp
  118. #include "employee.h"
  119.  
  120. Employee::Employee(string imie, string stanowisko, unsigned wiek) : Name(imie), Position(stanowisko), Age(wiek) {}
  121. Employee::Employee() : Name(""), Position(""), Age(0) {}
  122.  
  123. ostream& operator << (ostream& out, const Employee& obiekt)
  124. {
  125.     out << "name: " << obiekt.Name << ", position: " << obiekt.Position << ", age: " << obiekt.Age;
  126.     return out;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement