Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 18th, 2006, 3:35 PM   #1
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
.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:
Originally Posted by Mohamed Jihad
Durka durka!
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!
jayme is offline   Reply With Quote
Old Mar 18th, 2006, 3:42 PM   #2
Mocker
Hobbyist Programmer
 
Mocker's Avatar
 
Join Date: Oct 2005
Location: Indiana
Posts: 223
Rep Power: 0 Mocker is an unknown quantity at this point
Send a message via AIM to Mocker
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
Mocker is offline   Reply With Quote
Old Mar 18th, 2006, 4:03 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Mar 18th, 2006, 6:52 PM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:42 PM.

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