creating header files

Started by
3 comments, last by SoulSkorpion 18 years, 8 months ago
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?
Advertisement
Read.
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.
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"//globalsCritter myCritter; //create an instance of your classint main(){  myCritter.sayhi(); //call the sayhi method  return 0;}


hope that helped... cheers
-fuchiefck----------------------------------------------------------"Inside the world that you as a programmer or developer create, you are God" - Zerbst & Duvel
Don't forget the semicolon after the class declaration! ;)
-------------"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."- Charles Babbage (1791-1871)

This topic is closed to new replies.

Advertisement