2015-04-15 63 views
-4

我的代碼由一個類動物和兩個繼承動物特徵的子類組成 - 兩棲動物和魚類。該代碼編譯,但deconstructors的oorder從底部殺死他們到頂部,但我想作爲顯示功能順序suggests- 這裏他們被殺害從上到下是我的代碼:爲什麼我的觀點不能在物種類中指向它的異構類

#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; 
} 
+3

你似乎編碼彷彿'Fish'和'Amphibian'是Species'的'子類。你的代碼說他們不是。也許英語單詞「物種」的性質使得它不清楚它是一個*容器的東西*還是特定的東西* –

回答

0

下面的代碼提示你看到Species爲更多的東西一般比Animal

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

在這種情況下,您應確保繼承宣佈,通過改變類定義:

class Animal : Species { 
... 
}; 

有些話還是:

  • 爲所有物種都有一個名字,無論是植物還是動物,你可以考慮移動成員Animal::name_speciesSpecies類;
  • 類別Species的名稱是複數形式的事實,您有一個向量作爲成員,並且在爲成員調用相應操作時顯示或刪除此成員的每個元素都傾向於暗示物種是Animals的集合。現在還不清楚你如何向你的向量添加元素。
  • 與其在聚合中使用原始指針,不如使用shared_ptr。然後你不需要自己刪除aniamls。
0

沒有從類Species繼承,所以轉換不可能使用分配技術。

有在Species沒有方法轉換AnimalSpecies,所以沒有運氣。編譯器如何將Animal轉換爲Species

看起來像Species容器Animal s。您需要提供一種方法,將Animal添加到Species中的容器或公開該容器。

你想是這樣的:

Species s; 
s.animals.push_back(&kermit); 
相關問題