Witam,
Mam program gdzie pierwsza klasa charakteryzuje zwierze, druga zaś opisuje listę tych zwierząt i ma wykonywać różne operacje na nich. Kod :
#include <iostream>
#include <list>
#include <iterator>
using namespace std;
class Zwierze
{
protected:
string imie;
string gatunek;
string data_ur;
public :
Zwierze(string im = "", string gat = "", string ur = "") : imie(im), gatunek(gat), data_ur(ur) {}
bool operator ==(const Zwierze &a);
friend ostream &operator<<(ostream &o, Zwierze &obj);
};
class Zoo : public Zwierze
{
list<Zwierze> zoo;
public:
Zoo(string im = "", string gat = "", string ur = "") : Zwierze(im, gat, ur) {}
bool add(const Zwierze &z);
void find(const char *txt, list<Zwierze> &result);
bool remove(const char* im, const char* gat);
void pokaz();
};
bool Zwierze::operator==(const Zwierze &a)
{
if(imie != a.imie) return false;
if (gatunek != a.gatunek) return false;
return true;
}
bool Zoo::add(const Zwierze &z)
{
list<Zwierze>::iterator it;
for(it = zoo.begin(); it != zoo.end(); it ++)
{
if ((*it).data_ur == z.data_ur) return false;
}
zoo.push_back(z);
return true;
}
void Zoo::find(const char *txt, list<Zwierze> &result)
{
}
void Zoo::pokaz()
{
list<Zwierze>::iterator it;
for(it = zoo.begin(); it != zoo.end(); it ++)
cout << (*it) << " ";
}
ostream &operator<<(ostream &o, Zwierze &obj)
{
o << "Imie : " << obj.imie << " " << "Gatunek : " << obj.gatunek << " " << "Data ur : " << obj.data_ur << endl;
return o;
}
int main()
{
Zwierze a("Kubus", "Mis");
Zoo z;
z.add(a);
z.pokaz();
return 0;
}
Problem w funkcji add, nie wiem dlaczego wyrzuca błąd w czasie kompilacji, że 'data_ur' jest protected i nie mam do niej dostępu skoro odziedziczyłem klase 'Zwierze'. Jeśli wykonuje to w funkcji operatorowej == to wtedy jest jak należy. Może ktoś podać metodę jak należy to poprawnie zapisać? Dziękuję z góry:)