title: C++ Inheritance in Separate Files Using #include and Inclusion Guards
body: <p>I am new to Stack Overflow and am teaching myself C++, but am still quite a beginner. After completing a nice chunk of the book I am using (which may be considered out dated and/or not a great book) I decided to re-enforce some concepts by trying them on my own, referencing the book only if needed, but I appear to be stuck. The concepts I am trying to tackle are inheritance, polymorphism, abstract data types (ADT), and separating the code for my classes into header files (.h) and C++ file (.cpp). Sorry in advance for the wall of text, I just want to be clear and specific where I need to be.</p>
  
  <p>So, my goal is to create simple shape classes that inherit from one another where applicable. I have four classes: myPoly, myRectangle, myTriangle, and mySquare. myPoly, if I understood this concept correctly, should be an ADT since one of the methods is a pure virtual function (area method), since creating a myPoly object isn't something I would want a user of my classes to do. myRectangle and myTriangle both derive from myPoly and in turn mySquare derives from myRectangle. I've also included my test program where I planned on testing my classes. I am using Code::Blocks 10.05 and keep getting the following error when I build my test.cpp program: </p>
  
  <pre><code>undefined reference to 'myPoly::myPoly()'
  </code></pre>
  
  <p>I get 42 similar errors all for the methods of the myPoly class. This happens when I try to build the .cpp files for myRectangle and myTriangle too. With the research I tried to do on the problems I been running into with this little project I feel like something is wrong with my inclusion guards or my #include statements, and something isn't getting included properly or is getting included too many times. At first I was providing the .cpp file for myPoly to myRectangle and myTriangle, but read in a couple of places that including the .h file for myPoly is more efficient and some how automatically include its .cpp. If anyone can provide some insight on that, it would be greatly appreciated. I also remember something about how using quotes in your inclusion statements is different than using the angle brackets. Below are all nine files that I have made for my little project. Most of the comments are little notes or reminders to me. </p>
  
  <h1>myPoly.h</h1>
  
  <pre><code>//Practice with inheritance, polymorphism, and Abstract Data Types
  //header file for Polygon class
  
  #ifndef MYPOLY_H
  #define MYPOLY_H
  
  class myPoly
  {
      public:
          //constructor
          //const reference pass because the values w and h don't change and reference avoid the time it takes to copy large
          //  objects by value (if there were any)
          myPoly();
          myPoly(const float &amp; w, const float &amp; h);
  
          //destructor
          virtual ~myPoly();
  
          //accessors
          float getWidth();
          float getHeight();
          void setWidth(const float &amp; w);
          void setHeight(const float &amp; h);
  
          virtual float area() = 0;
  
      private:
          float width, height;
  };
  
  #endif
  </code></pre>
  
  <h1>myPoly.cpp</h1>
  
  <pre><code>//Practice with inheritance, polymorphism, and Abstract Data Types
  //implementation file for myPoly class
  
  #include "myPoly.h"
  
  //constructor
  myPoly::myPoly()
  {
      setWidth(10);
      setHeight(10);
  }
  
  myPoly::myPoly(const float &amp; w, const float &amp; h)
  {
      setWidth(w);
      setHeight(h);
  }
  
  //destructor
  myPoly::~myPoly() {}
  
  //accessors
  float myPoly::getWidth() {return width;}
  float myPoly::getHeight() {return height;}
  
  void myPoly::setHeight(const float &amp; w) {width = w;}
  void myPoly::setWidth(const float &amp; h) {height = h;}
  
  //pure virtual functions have no implementation
  //area() is handled in the header file
  </code></pre>
  
  <h1>myRectangle.h</h1>
  
  <pre><code>//Practice with inheritance, polymorphism, and Abstract Data Types
  //declaration file for myRectangle class
  
  #ifndef MYRECTANGLE_H
  #define MYRECTANGLE_H
  
  #include "myPoly.h"
  
  class myRectangle : public myPoly
  {
      public:
          //constructor
          myRectangle();
          myRectangle(const float &amp; w, const float &amp; h);
  
          //destructor
          ~myRectangle();
  
          //this doesn't need to be virtual since the derived class doesn't override this method
          float area();
  };
  
  #endif
  </code></pre>
  
  <h1>myRectangle.cpp</h1>
  
  <pre><code>//Practice with inheritance, polymorphism, and Abstract Data Types
  //implementaion file for the myRectangle class
  
  //get a vauge compiler/linker error if you have virtual methods that aren't implemented (even if it ends up being just
  //  a 'stub' method, aka empty, like the destructor)
  
  #include "myRectangle.h"
  
  myRectangle::myRectangle()
  {
      setWidth(10);
      setHeight(10);
  }
  
  myRectangle::myRectangle(const float &amp; w, const float &amp; h)
  {
      setWidth(w);
      setHeight(h);
  }
  
  myRectangle::~myRectangle()
  {
  }
  
  float myRectangle::area()
  {
      return getWidth() * getHeight();
  }
  </code></pre>
  
  <h1>myTriangle.h</h1>
  
  <pre><code>//Practice with inheritance, polymorphism, and Abstract Data Types
  //declaration file for myTriangle class
  
  #ifndef MYTRIANGLE_H
  #define MYTRIANGLE_H
  
  #include "myPoly.h"
  
  //imagine the triangle is a right triangle with a width and a height
  //  |\
  //  | \
  //  |  \
  //  |___\
  
  class myTriangle : public myPoly
  {
      public:
          //constructors
          myTriangle();
          myTriangle(const float &amp; w, const float &amp; h);
  
          //destructor
          ~myTriangle();
  
          //since nothing derives from this class it doesn't need to be virtual and in turn neither does the destructor
          float area();
  };
  
  #endif
  </code></pre>
  
  <h1>myTriangle.cpp</h1>
  
  <pre><code>//Practice with inheritance, polymorphism, and Abstract Data Types
  //implementation file for myTriangle class
  
  #include "myTriangle.h"
  
  myTriangle::myTriangle()
  {
      setWidth(10);
      setHeight(10);
  }
  
  myTriangle::myTriangle(const float &amp; w, const float &amp; h)
  {
      setWidth(w);
      setHeight(h);
  }
  
  myTriangle::~myTriangle()
  {
  }
  
  float myTriangle::area()
  {
      return getWidth() * getHeight() / 2;
  }
  </code></pre>
  
  <h1>mySquare.h</h1>
  
  <pre><code>//Practice with inheritance, polymorphism, and Abstract Data Types
  //declaration file for mySquare class
  
  #ifndef MYSQUARE_H
  #define MYSQUARE_H
  
  #include "myRectangle.cpp"
  
  class mySquare : public myRectangle
  {
      public:
          //constructors
          mySquare();
          //explicity call the myRectangle constructor within this implementation to pass w as width and height
          mySquare(const float w);
  
          //destructor
          ~mySquare();
  };
  
  #endif
  </code></pre>
  
  <h1>mySquare.cpp</h1>
  
  <pre><code>//Practice with inheritance, polymorphism, and Abstract Data Types
  //implementation file for mySquare class
  
  #include "mySquare.h"
  
  mySquare::mySquare()
  {
      setWidth(10);
      setHeight(10);
  }
  
  mySquare::mySquare(const float w)
  {
      myRectangle::myRectangle(w, w);
  }
  
  mySquare::~mySquare()
  {
  }
  </code></pre>
  
  <h1>test.cpp</h1>
  
  <pre><code>//Practice with inheritance, polymorphism, and Abstract Data Types
  //main class that uses my shape classes and experiments with inheritance, polymorphism, and ADTs
  
  #include "myRectangle.cpp"
  //#include "mySquare.cpp"
  #include "myTriangle.cpp"
  
  #include &lt;iostream&gt;
  
  int main()
  {
      myPoly * shape = new myRectangle(20,20);
  
      return 0;
  }
  </code></pre>
  
  <p>I am very curious as to why I am getting these errors or why something I did may not be considered good/best practice, as opposed to just receiving a line of code to make my errors go away.</p>