title: How to write a class library in c++ like the one we write for functions
body: <p>Suppose we need to write a library of functions which we intend to use in our programs, then we could write it by the following way.</p>
  
  <p>In a .h file we declare the function (mylibrary could be any file name we wish)
  Assuming sum is the function we wish to have in our library</p>
  
  <pre><code>int sum(int x, int y);
  </code></pre>
  
  <p>Then we will have a .cpp file which will define the function as follows : </p>
  
  <pre><code>#include "mylibrary.h"
  
  int sum(int x, int y){ return x+y; }
  </code></pre>
  
  <p>Now we we wish to use this function in our programs, say myprog.cpp, we could use like this :</p>
  
  <pre><code>#include
  #include "mylibrary.h"
  
  int main()
  {
  cout&lt;&lt;sum(10,20)&lt;&lt;endl;
  return 0;
  }
  </code></pre>
  
  <p>My question is that can we do something similar for classes as we did for functions, i.e. </p>
  
  <p>Can we declare classes in a .h file like :</p>
  
  <pre><code>class X;
  class Y;
  </code></pre>
  
  <p>Then define the class in .cpp like :</p>
  
  <pre><code>#include"myclasses.h"
  class X
  {
  public:
  int m;
  };
  
  class Y
  {
  public:
  int n;
  };
  </code></pre>
  
  <p>and then use these classes in our program, say myprog.cpp, like this :</p>
  
  <pre><code>#include"myclasses.h"
  
  int main()
  {
  class X myX;
  myX.m = 0;
  return 0;
  }
  </code></pre>
  
  <p>I tried this and got Error aggregate 'X myX' has incomplete type and cannot be defined.</p>
  
  <p>If I put entire definition in myclasses.h file, then it runs fine without errors.</p>