Advertisement Jump to content
Sign in to follow this  
willthiswork89

creating header files

This topic is 4900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ive googled this 100 times and cannot find anywhere with a good tutorial on it, it was in my book but it was very bad discription, i want to just be able to include a simple class in a header class Critter { public: Critter(int hunger = 9): mhunger(hunger); void sayhi(); private: int mhunger; } i want that in a header file and be able to call it in main.cpp...in my book it says i need the header then i need another file to declare what sayhi equals then i can call it...can someone help me?

Share this post


Link to post
Share on other sites
Advertisement
In the .cpp file you would have the body of the member function sayhi(). So you would #include the header file in the .cpp file. If your book includes a complete simple example, I would try rewriting in from memory. I found that when I'm not copying code, I learn it much better. I make a lot more mistakes, but fixing errors is a good (though slow) learning tool. Good luck.

Share this post


Link to post
Share on other sites
critter.h the header file for your class

class Critter
{
public:
Critter(int hunger = 9): mhunger(hunger);
void sayhi();
private:
int mhunger;
}


critter.cpp the implementation file for your class

#include "critter.h"

void Critter::sayhi() //implementation for your sayhi() method
{
//do whatever you wanna do here eg:
cout << "Hi!" << endl;
}


main.cpp

#include "critter.h"

//globals
Critter myCritter; //create an instance of your class
int main()
{
myCritter.sayhi(); //call the sayhi method

return 0;
}


hope that helped... cheers

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!