-4

My code consists of a class animal and 2 sub classes inheriting animal charactistics - amphibian, and fish. The code compiles but the oorder of the deconstructors kills them from bottom to top but i want them to be killed from top to bottom as the display function order suggests- Here is my code:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Animal{
public:
    Animal(string name, string diet, bool queue)
        : name_species(name), regime(diet), queue(queue){
            if(name == ""){cout << "Error, name can't be empty!" << endl; }
            else { cout << "A new animal" << endl; }}
    virtual ~Animal(){ cout << "End of animal" << endl; }
    virtual void display()const;
protected:
    string name_species;
    string regime;
    bool queue;
};

void Animal::display() const{
    cout << "I present the species " << name_species << endl;
}

class Fish : public Animal {
public:
    Fish(string name, unsigned int scales, string diet = "Carnivore", bool  queue = true)
        : Animal(name, diet, queue), scales(scales){ cout << "Add a fish" << endl; }
    ~Fish(){ cout << "Fish species " << name_species << " is turned off. " << endl; }
    void display() const override;
private:
    unsigned int scales;
};
void Fish::display() const {
    Animal::display();
    if(queue == true){cout << "I have a tail "; }
    else { cout << "I don't have a tail "; } cout << "and i follow " << regime <<    endl;
    cout << "I have " << scales << " scales" << endl;
}


class Amphibian : public Animal {
public: Amphibian(string name, string diet, unsigned int claws = 4, bool queue = false)
        : Animal(name, diet, queue), claws(claws){ cout << "Add an amphibian" << endl; }
        void affiche() const;
        ~Amphibian(){ cout << "Amphibian species " << name_species << " is turned off. " << endl; }
private:
        unsigned int claws;
};

void Amphibian::display() const {
    Animal::display();
    if(queue == true){ cout << "I have a tail "; }
    else { cout << "I don't have a tail " ; } cout << "and i follow " << regime << endl;
    cout << "I have " << scales << " scales " << endl;
}

class Species {
public:
    virtual void display()const = 0;
    virtual ~Species();
private:
    vector<Animal*> animals;
};

void Species::display()const{
    for(size_t i(0); i < animals.size(); ++i){
        animals[i]->display(); }
}

Species::~Species(){
    for(size_t i(0); i < animals.size(); ++i){
        delete animals[i];
    }
}

int main()
{
    Amphibian kermit("lizard", "insects");
    Fish nemo("dolphin", 6);
    Fish sala("salamander", 4);

    Animal* animal(&kermit);
    animal->display();

    animal = &nemo;
    animal->display();

    return 0;
}
1
  • 3
    You seem to be coding as if Fish and Amphibian are subclasses of Species. Your code says they are not. Perhaps the nature of the English word "species" makes it unclear whether it's a container of things or a particular thing, Apr 15, 2015 at 19:17

2 Answers 2

0

Nothing inherits from class Species so conversion not possible using assignment technique.

There are no methods in Species to convert an Animal into a Species, so no luck there. Compiler running out of ideas on how to convert Animal to Species.

Looks like Species is a container of Animals. You will need to supply a method that adds an Animal to the container inside Species or make the container public.

You want something like this:

Species s;
s.animals.push_back(&kermit);
0

The following code suggests that you see Species as something more general than Animal:

 Species* animal(&kermit);
 animal->display();
 animal = &nemo;

In this case you shall make sure the inheritance is declared, by changing the class definition to :

class Animal : Species {
...
};

Some remarks nevertheless:

  • as all species have a name, whether vegetal or animal, you could consider to move member Animal::name_species to class Species;
  • the fact that the name of the class Species is in plural, that you have a vector as member, and that every element of this member is displayed or deleted when the corresponding operation is called for the member tends however to suggest that Species is an aggregation of Animals. It is then not clear how you add elements to your vector.
  • Rather than using raw pointers in the aggregation, you could use shared_ptr instead. You then don't need to delete the aniamls yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.