jeudi 30 juin 2016

Counting elements in a list with different objects given a property

So i got this two classes and i want to count the number of FlowersGarden objects with the specie rose in my list:

class Garden {
private:
    string owner;
    double lenght, width;
public:
    Garden(string ow, double l, double w) {
        this->ownder = ow;
        this->lenght = l;
        this->width = w;
}

class FlowersGarden: public Garden {
private:
    string species;
public:
    FlowersGarden(string ow, double l, double w, string sp):Garden(ow, l, w) {
        this->species = sp;
}
    string GetSpecie()const {return species;};
};

main.cpp

Graden** list;
list = new Garden* [5];
list[0] = new Garden("asdas", 54, 57);
list[1] = new FlowersGarden("tyyty", 98, 87, "rose");
list[2] = new FlowersGarden("asdasd", 578, 212, "sadas");
list[3] = new Garden("uyiyui", 687, 212); 
int count = 0;
for (int i = 0; i < 4; i++)
    if(dynamic_cast<FlowersGarden*>(list[i]))
        if(list[i]->GetSpecies() == "rose")
           count++;

That's only i can think of solving this problem and i'm getting this error: "class 'Garden' has no member named 'GetSpecies'" and i see why, but i don't know another way.

Aucun commentaire:

Enregistrer un commentaire