title: How to display an array of objects derived from a base class, each with it's own different operator overload function?
body: <p>So i have a base class, and 3 derived classes, and another class template which as a parameter is given the base class, class template has an array of the 3 different type of objects.  I have overloaded the &lt;&lt; operator for all 3 derived classes and the base class, but when i try to display all the array in the console, the base class operator overload function is used, how do i make it in such a way that when i iterate through the array of objects and display each one, it's different for every type of object?</p>
  <p>base class representation</p>
  <pre><code>#pragma once
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  class Sushi
  {
  private:
      static int count;
  protected:
      int id;
      int cost;
      std::string fish_type;
  public:
      Sushi();
      Sushi(int cost, std::string fish_type);
      ~Sushi();
  
      int get_id();
      int get_cost();
      std::string get_ftype();
  
      void set_cost(int cost);
      void set_ftype(std::string fish);
      friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Sushi&amp; sushi);
  };
  </code></pre>
  <p>base class operator</p>
  <pre><code>std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Sushi&amp; sushi)
  {
      os &lt;&lt; sushi.id &lt;&lt; &quot; &quot; &lt;&lt; sushi.cost &lt;&lt; &quot; &quot; &lt;&lt; sushi.fish_type &lt;&lt; std::endl;
      return os;
  }
  </code></pre>
  <p>class template representation</p>
  <pre><code>#pragma once
  #include &quot;Sushi.h&quot;
  template &lt;class Sushi&gt;
  class SushiRepository
  {
      Sushi *repo;
      int capacity;
      int top;
  
  public:
      //default constructor
      SushiRepository();
      
      //constructor
      SushiRepository(int size);
  
      //adds an element to top of the stack
      void push(Sushi s);
  
      //Pops an element from the top of the stack and returns it
      Sushi pop();
  
      //Returns the last element from the top of the stack
      Sushi peek();
  
      //returns the size of the repo
      int size();
  
      //checks if repo is empty
      bool isEmpty();
  
      //checks if repo is full
      bool isFull();
  
      //remove by id
      void removeByID(int id);
  
      //display all
      void displayAll();
  
      ~SushiRepository();
  };
  </code></pre>
  <p>class template display function</p>
  <pre><code>template &lt;class Sushi&gt;
  inline void SushiRepository&lt;Sushi&gt;::displayAll()
  {
      if (!isEmpty()) {
          for (int i = 0; i &lt; size(); i++)
          {
              std::cout &lt;&lt; repo[i] &lt;&lt; std::endl;
          }
      }
      else {
          std::cout &lt;&lt; &quot;Repo is empty&quot;;
      }
  }
  
  template&lt;class Sushi&gt;
  SushiRepository&lt;Sushi&gt;::~SushiRepository()
  {
      delete[] repo;
  }
  </code></pre>
  <p>derived class representation</p>
  <pre><code>#pragma once
  #include &quot;Sushi.h&quot;
  #include &lt;ostream&gt;
  class Sashimi : public Sushi
  {
      std::string bed;
  
  public:
      Sashimi();
      Sashimi(std::string bed);
      ~Sashimi();
  
      std::string get_bed();
      void set_bed(std::string b);
      friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, const Sashimi &amp;sh);
  };
  </code></pre>
  <p>one of the derived classes operator overload function</p>
  <pre><code>std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Sashimi&amp; sh)
  {
      os &lt;&lt; &quot;ID:&quot; &lt;&lt; sh.id &lt;&lt; &quot; Sashimi of &quot; &lt;&lt; sh.fish_type &lt;&lt; &quot; on a bed of: &quot; &lt;&lt; sh.bed &lt;&lt; &quot; cost: &quot; &lt;&lt; sh.cost &lt;&lt; std::endl;
      return os;
  }
  </code></pre>