Does anyone know the differance and what should be used when?
If someone has a 'rule of thumb' I would love to hear it.
James
Does anyone know the differance and what should be used when?
If someone has a 'rule of thumb' I would love to hear it.
James
My site to register for all my other websites!
'Clifton Bazaar'
I don't know - but it may have something to do with the way they're compiled, and .h files may be compiled in C syntax and not C++. I use hpp to be on the safe side.
You should know that in C++ you must declare functions before you use them. You must 'declare' all functions before the main() fxn otherwise you will get an error.
//////////////////////////////////////////////////////////////////
int myfxn(int k, int y); //This is considered a 'declaration' since it doesn't have any '{}' blocks and thus does nothing yet.
void main() { //Here's our main fxn that starts our program
...;
}
int myfxn(int k, int y) //Down here is where we 'define' our function and tell it what to do
{
...;
}
//////////////////////////////////////////////////////////////////////////
The Header files (.h) simply hold the declarations, that way you don't have to put them in your .CPP file. Instead you just #include "name.h" them into your .cpp file. The header files hold the declarations for the functions you use in the .cpp file. However, their real value becomes evident when you get into Classes and other advanced C++ topics.
For now, the answer to your question is the header files hold the 'declarations' of the functions or Classes you use so you could put our declaration of myfxn(int k, int y); into a file and give it a .h extension and that is our header file, then #include it above the main() fxn.
.cpp files hold the Definitions of our functions and Classes.
//////////////////////////////////////// Here's our new code//////////
#include "somename.h"
void main() { //Here's our main fxn that starts our program
...;
}
int myfxn(int k, int y) //Down here is where we 'define' our function and tell it what to do
{
...;
}
My Avatar says: "Stay in School"
Rocco is the Boy!
"SHUT YOUR LIPS..."
At the moment I am putting everything into header files, but after reading what you've written I think I might change a few around and see what happens
My site to register for all my other websites!
'Clifton Bazaar'
Just put the class declarations in the header files. Than define the methods of these classes in separate .cpp files. In the main program include the header files but not the .cpp files because the cpp's are compiled along with main as long as you are using a project workspace. I have no idea what to do in Linux, I think you need a make file or some god aweful thing.
Now to ask a question that some people may find is a bit simple:
Because I have been using headers and only a main.cpp I have forgotten how to use a second .cpp file. Can someone please remind me of what I need to do in the .cpp?
James
My site to register for all my other websites!
'Clifton Bazaar'
You include it in the same way as a header file. In the main.cpp file add #include "yourfile.cpp" .
In that .cpp file usually goes the implementation of whatever you've declared in the header, like e.g. the definitions of member functions.
Seron
I did programming work this summer (and over the holidays) on a major software project being designed in C++, and this was their convention:
All class declarations (including declarations of member variables and functions) and global functions were to be located in header files, along with the documentation. Each class in most cases was to have its own individual header file. Each header file then had its own .cc file of the same name, which contained the implementations of all the functions declared in the header file.
Only .h files were #included by other files. The .cc files were compiled individually by the person who created the code and stored as makefiles, which were automatically added to the final executable when included by a .cc file with a main() function. However, different compilers and environments deal with multiple .cc files in different ways.
Thanks for all the comments people, I'm rearranging my program as we all talk
My site to register for all my other websites!
'Clifton Bazaar'