![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Professional Programmer
|
.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?
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
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.
__________________
#programmingforums relay - http://thegupstudio.com/cgi-bin/pforelay.cgi freelance scripts - http://ryanguthrie.com/index.html |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Never #include a .cpp file. The reasons and pros and cons and all have been covered on this forum several times.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|