Witam,
Dostałem kolejne zadanie do zrobienia. Jest to moje pierwsze spotkanie z template więc nie bijcie! :D
Mamy podane main() i do niego mamy dorobić potrzebne klasy tak żeby wszystko działało. Część zadania już działa.
Komentarze dodałem po polsku ale kod będę pisał po angielsku. Po prostu do tłumaczenia profesorowi tak będzie lepiej.
Potrzebuję jeszcze dopisać kod do rzeczy, które zakomentowałem:
*cout - operator << do ostream
*DataBase = NewDatabase - tu jak rozumiem operator= ?
Kod maptemplate.cpp:
#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 maptemplate.h:
#include <iostream>
#include <vector>
using namespace std;
//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;
}
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);
}; //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;
}
A zatem pytania :D
- Jaki nagłówek powinien być do operatora <<. Z tego co widziałem w google to w niektórych przypkach nagłówek był dosyć długi albo nie widziałem zależności więc potrzebuję wytłumaczenia..
Co w tym powinienem dopisać / zmienić?
friend ostream & operator<< (ostream & s, tutaj coś na wejscie);
// będzie trzeba odczytać map_template<ID,Employee> Database
Czy nad tą metodą powinno być też template<class Key, class T> ?
Jak zrobię cout, to zajmę się robieniem NewDatabase tak żeby już widzieć jak DataBase wygląda.
Jak ktoś ma jakieś porady, albo pomysły to proszę o aktywność! :D Będę nagradzał lajkami :) Konstruktywny hejt też zlajkuję :D