Hi, I am a newbie! I want to create a DevC++ project that will compile a few classes with their header files in the correct order!
Now I would like to understand something. Should all my class declarations be in a .h file and my class definitions be in a .cpp file? example:
class Cat { private: int age; public: Cat(); ~Cat(); }
Now should I include the top class declaration in Cat.h?
how about the functions and methods for Cat class, do they go in my Cat.cpp?
example:
Cat::Cat() { cout<<Cat constructed!"<<endl; }
Cat::~Cat() { cout<<Cat destructed!"<<endl; }
does this code go in my Cat.cpp
do I include "Cat.h" in my Cat.cpp file?
now if i have a few more classes like this, how do i compile them all?
say iw ant to run everything from a main class, which only has my main method
please help.
thank you.
1) Yes you should put
in a file called Cat.h
and
#include "Cat.h" Cat::Cat() { cout<<Cat constructed!"<<endl; }
in a file called Cat.cpp
2) In your main code (where int main resides) add #include "Cat.h"
3) Putting all of you classes in seperate files can be good (re-usable code) and bad (1000's of files).
4) To compile everything just add the *.cpp files (Cat.cpp) to your project.
This should work, as I have had it working for me.
Curtis
Log in to post a comment.
Hi,
I am a newbie!
I want to create a DevC++ project that will compile a few classes with their header files in the correct order!
Now I would like to understand something. Should all my class declarations be in a .h file and my class definitions be in a .cpp file?
example:
class Cat
{
private:
int age;
public:
Cat();
~Cat();
}
Now should I include the top class declaration in Cat.h?
how about the functions and methods for Cat class, do they go in my Cat.cpp?
example:
Cat::Cat()
{
cout<<Cat constructed!"<<endl;
}
Cat::~Cat()
{
cout<<Cat destructed!"<<endl;
}
does this code go in my Cat.cpp
do I include "Cat.h" in my Cat.cpp file?
now if i have a few more classes like this, how do i compile them all?
say iw ant to run everything from a main class, which only has my main method
please help.
thank you.
1) Yes you should put
class Cat
{
private:
int age;
public:
Cat();
~Cat();
}
in a file called Cat.h
and
#include "Cat.h"
Cat::Cat()
{
cout<<Cat constructed!"<<endl;
}
Cat::~Cat()
{
cout<<Cat destructed!"<<endl;
}
in a file called Cat.cpp
2) In your main code (where int main resides) add
#include "Cat.h"
3) Putting all of you classes in seperate files can be good (re-usable code) and bad (1000's of files).
4) To compile everything just add the *.cpp files (Cat.cpp) to your project.
This should work, as I have had it working for me.
Curtis