View Single Post
Old Jul 12th, 2008, 12:07 AM   #3
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,120
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: creating header files

Let's say your Piece class has one public constructor, and four methods to move it (left, right, up, and down). Your header file might look something like this:
C++ Syntax (Toggle Plain Text)
  1. // piece.h
  2. public class piece
  3. {
  4. private:
  5. static int MAX_X;
  6. static int MAX_Y;
  7. int xPosition, yPosition;
  8. public:
  9. Piece(void);
  10. void moveLeft(void);
  11. void moveRight(void);
  12. void moveUp(void);
  13. void moveDown(void);
  14. };
Your .cpp file might be like so:
C++ Syntax (Toggle Plain Text)
  1. // piece.cpp
  2. #include "piece.h"
  3. int Piece :: MAX_X = 20;
  4. int Piece :: MAX_Y = 20;
  5.  
  6. Piece :: Piece(void)
  7. {
  8. xPosition = MAX_X / 2;
  9. yPosition = MAX_Y / 2;
  10. // other initialization code
  11. }
  12.  
  13. void Piece :: moveLeft(void)
  14. {
  15. --xPosition;
  16. if(xPosition < 0)
  17. xPosition = 0;
  18. }
  19.  
  20. void Piece :: moveRight(void)
  21. {
  22. ++xPosition;
  23. if(xPosition > MAX_X)
  24. xPosition = MAX_X;
  25. }
  26.  
  27. void Piece :: moveUp(void)
  28. {
  29. --yPosition;
  30. if(yPosition < 0)
  31. yPosition = 0;
  32. }
  33.  
  34. void Piece :: moveDown(void)
  35. {
  36. ++yPosition;
  37. if(yPosition < 0)
  38. yPosition = MAX_Y;
  39. }
Basically, declarations go in the header file, and definitions go in the code file. Stuff like macros and inline functions go in the header, but (aside from inline functions) you should have no actual code in there.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote