title: C++ Headers related to Classes
body: <p>I've been reading different articles and tutorials on Header files.
  I understand that Headers serve for the purpose of keeping the "interface" from the implementation. ( and other things like some compile optimization )</p>
  
  <p>What I still don't get, and really can't wrap my mind around it, is do you always use headers?
  I know you can write blocks of code within a header file itself. But that is where I get lost.</p>
  
  <p>When I look at a video tutorial, people just define functions with their body within the Header file. Then a different article only defines the functions ( I guess that is the idea of an interface).</p>
  
  <p>Just for now I'm making a simple class named Color.
  implementation:</p>
  
  <pre><code>/* 
  * File:   Color.cpp
  * Author: Sidar
  * 
  * Created on 26 december 2011, 16:02
  */
  
  #include &lt;stdio.h&gt;
  
  #include "Color.h"
  
  Color::Color() {
  
   reset();
  }
  
  Color::Color(const Color&amp; orig) {
  
   a = orig.a;
   r = orig.r;
   g = orig.g;
   b = orig.b;
  }
  
  void Color::reset()
  {
      a = 0;
      r = 0;
      g = 0;
      b = 0;
  }
  
   Color::Color(unsigned int r, unsigned int g, unsigned int b, unsigned int a)
   {
     this-&gt;r = r;
     this-&gt;g = g;
     this-&gt;b = b;
     this-&gt;a = a;
   }
  
  Color::~Color() {
     r = 0;
     g = 0;
     b = 0;
   }
  
   //getters____________________________
   unsigned int Color::getRed() const
   {
     return r;
   }
  
   unsigned int Color::getBlue() const
   {
     return b;
   }
  
   unsigned int Color::getGreen() const
   {
      return g;
   }
  
   unsigned int Color::getAlpha() const
   {
     return a;
   }
  
   //setters____________________________
  
   void Color::setRed(unsigned int r)
   {
     if(r &gt; 255)r = 255;
     if(r &lt; 0)r = 0;
  
     this-&gt;r = r;
  }
  
  
  void Color::setGreen(unsigned int g)
  {
    if(g &gt; 255)g = 255;
    if(g &lt; 0)g = 0;
  
    this-&gt;g = g;
  }
  
   void Color::setBlue(unsigned int b)
  {
     if(b &gt; 255)b = 255;
    if(b &lt; 0)b = 0;
  
    this-&gt;b = b;
  }
  
  void Color::setAlpha(unsigned int a)
  {
   if(a &gt; 255)a = 255;
   if(a &lt; 0)a = 0;
  
   this-&gt;a = a;
   }
  
   unsigned int Color::color()
   {
     return (int)a &lt;&lt; 24 | (int)r &lt;&lt; 16 | (int)g &lt;&lt; 8 | (int)b &lt;&lt; 0;
    }
  </code></pre>
  
  <p>and here the header</p>
  
  <pre><code>/* 
   * File:   Color.h
   * Author: Sidar
   *
   * Created on 26 december 2011, 16:02
   */
  
   #ifndef COLOR_H
  #define COLOR_H
  #include &lt;string&gt;
  
  class Color {
  public:
  
      Color();
      Color(const Color&amp; orig);
      Color(unsigned int r,unsigned int g,unsigned int b, unsigned int a);
  
      virtual ~Color();
      //____________________
      void setRed(unsigned int r);
      unsigned int getRed()const;
      //____________________  
      void setBlue(unsigned int b);
      unsigned int getBlue()const;
      //____________________
      void setGreen(unsigned int g);
      unsigned int getGreen()const;
      //____________________
      void setAlpha(unsigned int a);
      unsigned int getAlpha()const;
      //____________________
      unsigned int color();
  
     void reset();
  
  private:
  
      unsigned int r;
      unsigned int b;
      unsigned int g;
      unsigned int a;
  
  
  };
  
  #endif  /* COLOR_H */
  </code></pre>
  
  <p>This code does work, I'm not getting any errors. But is this the general idea of headers and cpp files?
  And my second question:
  I read a lot that when using Templates it's easier to just implement the code within the Header I understand this(to prevent many implementations for something that is suppose to be so generic). But are there any other situations as well?</p>