creating header files
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?
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
critter.cpp the implementation file for your class
main.cpp
hope that helped... cheers
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement