Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   .H or .cpp? (http://www.programmingforums.org/showthread.php?t=8929)

jayme Mar 18th, 2006 2:35 PM

.H or .cpp?
 
I have a class called Monster, now what I have is an array of 5 monsters, because there are 5 different monsters in my program.

:

Monster array[5];
array[0].SetName("monster1");
array[1].SetName("monster2");
array[2].SetName("monster3");
array[3].SetName("monster4");
array[4].SetName("monster5");

array[0].SetDamage(12);
array[1].SetDamage(15);
array[2].SetDamage(16);
array[3].SetDamage(3);
array[4].SetDamage(22);


I know that is horrible code, mine doesn't actually look like that though. I am just wondering, if you are only using the file to set the data members for the array, should I use a .cpp file or a header? I'm thinking a .cpp file is the way to go, but I am also under the impression you should do "#include "array.cpp"" type declarations, so I would need a header in association to simply refer to that .cpp file.

EDIT: Or, would I just set each value for each array in the Monster.cpp file, which holds all the member functions?

Mocker Mar 18th, 2006 2:42 PM

That would go in a .cpp file since you are calling functions and performing an action. Make some sort of initialization function and put the code there.

DaWei Mar 18th, 2006 3:03 PM

Never #include a .cpp file. The reasons and pros and cons and all have been covered on this forum several times.

nnxion Mar 18th, 2006 5:52 PM

If you want to have some array outside of your file you should use the keyword extern. I'll repeat what DaWei said: Never include a .cpp file, always use header files and add:
:

#ifndef FILENAME_H
#define FILENAME_H

// rest of code

#endif

That will make sure your header only gets included once. When the preprocessor includes some file, it just pastes it in.


All times are GMT -5. The time now is 11:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC