Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 11th, 2008, 8:38 PM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 345
Rep Power: 4 cwl157 is on a distinguished road
creating header files

So i am making a c++ program with classes. I want to know how to use header files. I have some functions and some variables that are public. I know a head file has the class and the function declarations in it. I keep getting errors that the type is not right in the .h file. The class is called piece it has 4 public variables and 1 constructor. I say this:

class Piece;

to define the class and then i need to define the constructor too right? But i can't figure out how to do that and do i have to delare the variables. I couldn't find a good example of one on the internet so could someone give me a good example of a c++ header file for a class so i can see how i am supposed to write the declarations in it? Thanks.
cwl157 is offline   Reply With Quote
Old Jul 11th, 2008, 9:07 PM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 598
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: creating header files

There are lots of tutorials on the net. See these google links (click here)
__________________
<<Freelance Programmer>> << Hobby Site>>
Ancient Dragon is offline   Reply With Quote
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
Old Jul 12th, 2008, 2:35 PM   #4
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 345
Rep Power: 4 cwl157 is on a distinguished road
Re: creating header files

thanks lectricpharaoh thats exactly what i was looking for
cwl157 is offline   Reply With Quote
Old Jul 15th, 2008, 2:25 AM   #5
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 292
Rep Power: 2 Jabo is on a distinguished road
Re: creating header files

Quote:
Originally Posted by xmo View Post
nonsense
WOW what a bunch of gibberish
Nice try xmo; I don't know about anybody else, but I won't be following any of your links. No telling what could happen.

Not to mention 2 things about your post:
1. you tromped all over somebody else's thread
2. this has nothing to do with programming
Jabo is offline   Reply With Quote
Old Jul 15th, 2008, 6:55 AM   #6
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 598
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: creating header files

Quote:
Originally Posted by Jabo View Post
WOW what a bunch of gibberish
Nice try xmo; I don't know about anybody else, but I won't be following any of your links. No telling what could happen.

Not to mention 2 things about your post:
1. you tromped all over somebody else's thread
2. this has nothing to do with programming
I deleted it -- spam.
__________________
<<Freelance Programmer>> << Hobby Site>>
Ancient Dragon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating new files on the server mleonid PHP 2 Aug 15th, 2006 3:03 PM
question about header files and prototyping functions linuxpimp20 C 13 Sep 7th, 2005 9:28 AM
Header files Klarre C++ 13 Jun 15th, 2005 12:25 PM
Including header files some1 C++ 8 Apr 28th, 2005 9:59 AM
Including your home-made header files Siphon C++ 3 Feb 5th, 2005 9:26 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:32 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC