Advertisement
MeehoweCK

Untitled

Dec 7th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 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.     return 0;
  31. };
  32.  
  33. // employee.h
  34. #include <iostream>
  35.  
  36. using namespace std;
  37.  
  38. class Employee
  39. {
  40. public:
  41.     string Name;
  42.     string Position;
  43.     unsigned Age;
  44.     Employee(string, string, unsigned);
  45.     Employee();
  46.     friend ostream& operator << (ostream&, const Employee&);
  47. };
  48.  
  49. // map_template.h
  50. #include <iostream>
  51. #include <vector>
  52.  
  53. using namespace std;
  54.  
  55. template<class A, class B>
  56. struct Rekord
  57. {
  58.     A nr;
  59.     B wartosc;
  60. };
  61.  
  62. template <class A, class B>
  63. class map_template
  64. {
  65.     vector<Rekord<A, B>> tablica;
  66.     unsigned wielkosc;
  67. public:
  68.     map_template();
  69.     ~map_template() {tablica.clear();}
  70.     void Add(A, B);
  71.     B* Find(A);
  72.     unsigned size() const {return wielkosc;}
  73.     template<class C, class D>
  74.         friend ostream& operator << (ostream&, const map_template<C, D>&);
  75. };
  76.  
  77. template<class A, class B>
  78. map_template<A, B>::map_template() : wielkosc(0)
  79. {
  80.     tablica.clear();
  81. }
  82.  
  83. template <class A, class B>
  84. void map_template<A, B>::Add(A a, B b)
  85. {
  86.     Rekord<A, B> nowy;
  87.     nowy.nr = a;
  88.     nowy.wartosc = b;
  89.     tablica.push_back(nowy);
  90.     ++wielkosc;
  91. }
  92.  
  93. template <class A, class B>
  94. B* map_template<A, B>::Find(A szukana)
  95. {
  96.     B* wskaznik = nullptr;
  97.  
  98.     for(unsigned i = 0; i < wielkosc; ++i)
  99.     {
  100.         if(tablica[i].nr == szukana)
  101.         {
  102.             wskaznik = &(tablica[i].wartosc);
  103.             return wskaznik;
  104.         }
  105.     }
  106.     return wskaznik;
  107. }
  108.  
  109. template <class C, class D>
  110. ostream& operator << (ostream& out, const map_template<C, D>& obiekt)
  111. {
  112.     for(unsigned i = 0; i < obiekt.wielkosc; ++i)
  113.     {
  114.         out << obiekt.tablica[i].nr << " " << obiekt.tablica[i].wartosc << endl;
  115.     }
  116.     return out;
  117. }
  118.  
  119. // employee.cpp
  120. #include "employee.h"
  121.  
  122. Employee::Employee(string imie, string stanowisko, unsigned wiek) : Name(imie), Position(stanowisko), Age(wiek) {}
  123. Employee::Employee() : Name(""), Position(""), Age(0) {}
  124.  
  125. ostream& operator << (ostream& out, const Employee& obiekt)
  126. {
  127.     out << "name: " << obiekt.Name << ", position: " << obiekt.Position << ", age: " << obiekt.Age;
  128.     return out;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement