Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.cpp
- #include <iostream>
- using namespace std;
- #include "employee.h" //defines class Employee
- #include "map_template.h" //defines template map_template<Key,Value>
- int main(void)
- {
- typedef unsigned int ID; //Identification number of Employee
- map_template<ID,Employee> Database; //Database of employees
- Database.Add(761028073,Employee("Jan Kowalski","salesman",28)); //Add first employee: name: Jan Kowalski, position: salseman, age: 28,
- Database.Add(510212881,Employee("Adam Nowak","storekeeper",54)); //Add second employee: name: Adam Nowak, position: storekeeper, age: 54
- Database.Add(730505129,Employee("Anna Zaradna","secretary",32)); //Add third employee: name: Anna Zaradna, position: secretary, age: 32
- cout << Database << endl; //Print databese
- map_template<ID,Employee> NewDatabase = Database; //Make a copy of database
- Employee* pE;
- pE = NewDatabase.Find(510212881); //Find employee using its ID
- pE->Position = "salesman"; //Modify the position of employee
- pE = NewDatabase.Find(761028073); //Find employee using its ID
- pE->Age = 29; //Modify the age of employee
- Database = NewDatabase; //Update original database
- cout << Database << endl; //Print original databese
- return 0;
- };
- // employee.h
- #include <iostream>
- using namespace std;
- class Employee
- {
- public:
- string Name;
- string Position;
- unsigned Age;
- Employee(string, string, unsigned);
- Employee();
- friend ostream& operator << (ostream&, const Employee&);
- };
- // map_template.h
- #include <iostream>
- #include <vector>
- using namespace std;
- template<class A, class B>
- struct Rekord
- {
- A nr;
- B wartosc;
- };
- template <class A, class B>
- class map_template
- {
- vector<Rekord<A, B>> tablica;
- unsigned wielkosc;
- public:
- map_template();
- ~map_template() {tablica.clear();}
- void Add(A, B);
- B* Find(A);
- unsigned size() const {return wielkosc;}
- template<class C, class D>
- friend ostream& operator << (ostream&, const map_template<C, D>&);
- };
- template<class A, class B>
- map_template<A, B>::map_template() : wielkosc(0)
- {
- tablica.clear();
- }
- template <class A, class B>
- void map_template<A, B>::Add(A a, B b)
- {
- Rekord<A, B> nowy;
- nowy.nr = a;
- nowy.wartosc = b;
- tablica.push_back(nowy);
- ++wielkosc;
- }
- template <class A, class B>
- B* map_template<A, B>::Find(A szukana)
- {
- B* wskaznik = nullptr;
- for(unsigned i = 0; i < wielkosc; ++i)
- {
- if(tablica[i].nr == szukana)
- {
- wskaznik = &(tablica[i].wartosc);
- return wskaznik;
- }
- }
- return wskaznik;
- }
- template <class C, class D>
- ostream& operator << (ostream& out, const map_template<C, D>& obiekt)
- {
- for(unsigned i = 0; i < obiekt.wielkosc; ++i)
- {
- out << obiekt.tablica[i].nr << " " << obiekt.tablica[i].wartosc << endl;
- }
- return out;
- }
- // employee.cpp
- #include "employee.h"
- Employee::Employee(string imie, string stanowisko, unsigned wiek) : Name(imie), Position(stanowisko), Age(wiek) {}
- Employee::Employee() : Name(""), Position(""), Age(0) {}
- ostream& operator << (ostream& out, const Employee& obiekt)
- {
- out << "name: " << obiekt.Name << ", position: " << obiekt.Position << ", age: " << obiekt.Age;
- return out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement