title: Using std::ostream as argument to a print function
body: <p>I have always used <code>cout</code> to print the statement but now I want learn printing by <code>passing the stream</code>, something like  <code>void print(std::ostream&amp;) const;</code> my current print function looks like</p>
  
  <pre><code>template &lt;class T&gt;
  
  void Mystack&lt;T&gt;::print()
  {
      for (int i = 0; i &lt;= top; i++)
      {
          std::cout &lt;&lt; input[i] &lt;&lt; " ";
      }
  }
  </code></pre>
  
  <p>I have 2 questions:</p>
  
  <ol>
  <li>What is the benefit of switching from normal print function that I implemented above to the print function using <code>ostream</code>.</li>
  <li>How can I implement <code>ostream</code> in my function. I tried to understand <code>ostream</code> from internet source but could not understand. Please help. </li>
  </ol>
  
  <p>Below is the complete running code:</p>
  
  <pre><code>//*************STACK CODE***************//
  
  //VERY GOOD EXAMPLE TO UNDERSTAND RULE OF THREE FOR BEGINEERS http://www.drdobbs.com/c-made-easier-the-rule-of-three/184401400
  //RULE OF THREE : Video : https://www.youtube.com/watch?v=F-7Rpt2D-zo
  //Thumb Rule : Whenever we have class which has members pointing to heap space we should implement Rule of three.
  //Concepts : Shallow Copy and Deep Copy
  
  #include &lt;iostream&gt;
  template &lt;class T&gt;
  class Mystack
  {
  private:
      T *input;
      int top;
      int capacity;
  public:
      Mystack();
      ~Mystack();
      void push(T const&amp; x);
      void pop();
      T&amp; topElement() const;
      bool isEmpty() const;
      void print();
  };
  template &lt;class T&gt;
  Mystack&lt;T&gt;::Mystack()
  {
      top = -1;
      capacity = 5;
      input = new T[capacity];
  }
  template &lt;class T&gt;
  Mystack&lt;T&gt;::~Mystack() //Since we are using destructor explictly we need to apply Rule of 3
  {
      delete [] input;
  }
  template &lt;class T&gt;
  void Mystack&lt;T&gt;::push(T const&amp; x)  //Passing x by Const Reference // Valus of x cannot be changed now in the function!
  {
      if (top + 1 == capacity)
      {
          T *vec = new T[capacity * 2];
          for (int i = 0; i &lt;= top; i++)
          {
              vec[i] = input[i];
          }
          delete []input; // Avoiding Memory Leak.
          input = vec;
          capacity *= capacity;
      }
      top++;
      std::cout &lt;&lt; x;
      std::cout &lt;&lt; &amp;x;
      input[top] = x;
  }
  template &lt;class T&gt;
  void Mystack&lt;T&gt;::pop()
  {
      if (isEmpty())
      {
          throw std::out_of_range("Stack Underflow");
      }
      else
      {
          std::cout &lt;&lt; "The popped element is" &lt;&lt; input[top--];
  
      }
  }
  template &lt;class T&gt;
  bool Mystack&lt;T&gt;::isEmpty() const
  {
      if (top == -1)
      {
          std::cout &lt;&lt; "Is Empty" &lt;&lt; std::endl;
          return true;
      }
      else
      {
          std::cout &lt;&lt; "Not Empty" &lt;&lt; std::endl;
          return false;
      }
  }
  template &lt;class T&gt;
  T&amp; Mystack&lt;T&gt;::topElement() const
  {
      if (top == -1)
      {
          throw std::out_of_range("No Element to Display");
      }
      else
      {
          std::cout &lt;&lt; "The top element is : " &lt;&lt; input[top];
          return input[top];
      }
  }
  template &lt;class T&gt;
  void Mystack&lt;T&gt;::print()
  {
      for (int i = 0; i &lt;= top; i++)
      {
          std::cout &lt;&lt; input[i] &lt;&lt; " ";
      }
  }
  int main()
  {
      Mystack&lt;int&gt; s1;
      Mystack&lt;float&gt; s2;
      Mystack&lt;char&gt; s3;
      int choice;
      int int_elem;
      float float_elem;
      char char_elem;
      std::cout &lt;&lt; "Enter the type of stack" &lt;&lt; std::endl;
      std::cout &lt;&lt; "1. int ";
      std::cout &lt;&lt; "2. float ";
      std::cout &lt;&lt; "3. Char"&lt;&lt; std::endl;
      std::cin &gt;&gt; choice;
      if (choice == 1)
      {
          int  ch = 1;
          while (ch &gt; 0)
          {
              std::cout &lt;&lt; "\n1. Push ";
              std::cout &lt;&lt; "2. Top ";
              std::cout &lt;&lt; "3. IsEmpty ";
              std::cout &lt;&lt; "4. Pop ";
              std::cout &lt;&lt; "5. Exit ";
              std::cout &lt;&lt; "6. Print"&lt;&lt;std::endl;
              std::cout &lt;&lt; "Enter the choice" &lt;&lt; std::endl;
              std::cin &gt;&gt; ch;
              switch (ch)
              {
              case 1:
                  std::cout &lt;&lt; "Enter the number to be pushed" &lt;&lt; std::endl;
                  std::cin &gt;&gt; int_elem;
                  s1.push(int_elem);
                  break;
              case 2:
                  std::cout &lt;&lt; "Get the TOP Element" &lt;&lt; std::endl;
                  try
                  {
                      s1.topElement();
                  }
                  catch (std::out_of_range &amp;oor)
                  {
                      std::cerr &lt;&lt; "Out of Range error:" &lt;&lt; oor.what() &lt;&lt; std::endl;
                  }
                  break;
              case 3:
                  std::cout &lt;&lt; "Check Empty" &lt;&lt; std::endl;
                  s1.isEmpty();
                  break;
              case 4:
                  std::cout &lt;&lt; "POP the element" &lt;&lt; std::endl;
                  try
                  {
                      s1.pop();
                  }
                  catch (const std::out_of_range &amp;oor)
                  {
                      std::cerr &lt;&lt; "Out of Range error: " &lt;&lt; oor.what() &lt;&lt; '\n';
                  }
                  break;
              case 5:
                  exit(0);
              case 6:
                  s1.print();
                  break;
              default:
                  std::cout &lt;&lt; "Enter a valid input";
                  break;
              }
          }
      }
      else if (choice == 2)
      {
          int  ch = 1;
          while (ch &gt; 0)
          {
              std::cout &lt;&lt; "\n1. PUSH" &lt;&lt; std::endl;
              std::cout &lt;&lt; "2. TOP" &lt;&lt; std::endl;
              std::cout &lt;&lt; "3. IsEmpty" &lt;&lt; std::endl;
              std::cout &lt;&lt; "4. POP" &lt;&lt; std::endl;
              std::cout &lt;&lt; "5. EXIT" &lt;&lt; std::endl;
              std::cout &lt;&lt; "6. Print" &lt;&lt; std::endl;
              std::cout &lt;&lt; "Enter the choice" &lt;&lt; std::endl;
              std::cin &gt;&gt; ch;
              switch (ch)
              {
              case 1:
                  std::cout &lt;&lt; "Enter the number to be pushed" &lt;&lt; std::endl;
                  std::cin &gt;&gt; float_elem;
                  s2.push(float_elem);
                  break;
              case 2:
                  std::cout &lt;&lt; "Get the TOP Element" &lt;&lt; std::endl;
                  try
                  {
                      s2.topElement();
                  }
                  catch (std::out_of_range &amp;oor)
                  {
                      std::cerr &lt;&lt; "Out of Range error:" &lt;&lt; oor.what() &lt;&lt; std::endl;
                  }
                  break;
              case 3:
                  std::cout &lt;&lt; "Check Empty" &lt;&lt; std::endl;
                  s2.isEmpty();
                  break;
              case 4:
                  std::cout &lt;&lt; "POP the element" &lt;&lt; std::endl;
                  try
                  {
                      s2.pop();
                  }
                  catch (const std::out_of_range &amp;oor)
                  {
                      std::cerr &lt;&lt; "Out of Range error: " &lt;&lt; oor.what() &lt;&lt; '\n';
                  }
                  break;
              case 5:
                  exit(0);
              case 6:
                  s2.print();
                  break;
              default:
                  std::cout &lt;&lt; "Enter a valid input";
                  break;
              }
          }
      }
      else if (choice == 3)
      {
          int  ch = 1;
          while (ch &gt; 0)
          {
              std::cout &lt;&lt; "\n1. PUSH" &lt;&lt; std::endl;
              std::cout &lt;&lt; "2. TOP" &lt;&lt; std::endl;
              std::cout &lt;&lt; "3. IsEmpty" &lt;&lt; std::endl;
              std::cout &lt;&lt; "4. POP" &lt;&lt; std::endl;
              std::cout &lt;&lt; "5. EXIT" &lt;&lt; std::endl;
              std::cout &lt;&lt; "6. Print" &lt;&lt; std::endl;
              std::cout &lt;&lt; "Enter the choice" &lt;&lt; std::endl;
              std::cin &gt;&gt; ch;
              switch (ch)
              {
              case 1:
                  std::cout &lt;&lt; "Enter the number to be pushed" &lt;&lt; std::endl;
                  std::cin &gt;&gt; char_elem;
                  s3.push(char_elem);
                  break;
              case 2:
                  std::cout &lt;&lt; "Get the TOP Element" &lt;&lt; std::endl;
                  try
                  {
                      s3.topElement();
                  }
                  catch (std::out_of_range &amp;oor)
                  {
                      std::cerr &lt;&lt; "Out of Range error:" &lt;&lt; oor.what() &lt;&lt; std::endl;
                  }
                  break;
              case 3:
                  std::cout &lt;&lt; "Check Empty" &lt;&lt; std::endl;
                  s3.isEmpty();
                  break;
              case 4:
                  std::cout &lt;&lt; "POP the element" &lt;&lt; std::endl;
                  try
                  {
                      s3.pop();
                  }
                  catch (const std::out_of_range &amp;oor)
                  {
                      std::cerr &lt;&lt; "Out of Range error: " &lt;&lt; oor.what() &lt;&lt; '\n';
                  }
                  break;
              case 5:
                  exit(0);
              case 6:
                  s3.print();
                  break;
              default:
                  std::cout &lt;&lt; "Enter a valid input";
                  break;
              }
          }
      }
      else
          std::cout &lt;&lt; "Invalid Choice";
      std::cin.get();
  }
  </code></pre>