Kod main:
#include "maptemplate.h"
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 = Database.Find(510212881); //Find employee using its ID
pE->Position = "salesman"; //Modify the position of employee
pE = Database.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 database
cout<<"Wszystko dziala"<<endl;
}
kod .h:
#include <iostream>
#include <vector>
using namespace std;
template <class Key, class T> class map_template;
template <class Key, class T> ostream& operator<< (ostream &o,const map_template<Key, T> &map);
template <class Key, class T> //template <klucze, dane pracownikow>
class map_template{
private:
vector<Key> keys; //vector do przechowywania unikalnych kluczy pracowników
vector<T> content; //vector do przechowywania danych pracowników
public:
map_template(){}
void Add(Key key, T t);
T* Find(Key key);
friend ostream& operator<< <>(ostream &out,const map_template<Key, T> &map);
}; //koniec klasy map_template
//Dodanie do bazy (Add)
template<class Key, class T>
void map_template <Key, T>::Add(Key key, T t)
{
keys.push_back(key);
content.push_back(t);
}
//Szukanie w bazie (Find)
template<class Key, class T>
T* map_template<Key, T>::Find(Key key)
{
for(unsigned int i = 0; i < keys.size(); i++)
if (keys[i] == key)
{
return &content.at(i);
}
return nullptr;
}
//Początek klasy Employee bez template
class Employee
{
private:
public:
Employee(string Name, string Position, int Age);
string Name;
int Age;
string Position;
};//koniec klasy employee
//Dodanie pól Name, Age, Position
Employee::Employee(string Name, string Position, int Age)
{
this->Name = Name;
this->Age = Age;
this->Position = Position;
}
//operator <<
template<class Key, class T>
ostream& operator<<(ostream &out,const map_template<Key, T> &map)
{
for(unsigned int i=0;i<map.content.size();i++)
{
out << map.keys[i] <<", " << map.content[i].Name << ", " <<map.content[i].Position << ", " <<map.content[i].Age <<endl;
}
return out;
}
Potrzebuję dorobić kod aktualizujący bazę danych:
map_template<ID,Employee> NewDatabase = Database; //Make a copy of database
// (...)
Database = NewDatabase;