Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Nov 29th, 2005, 2:46 AM   #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 and .CPP files

Lets say i have a header named Experience.h and another one named fight.h, they are both compiled in the same project.dev file, so if i have an integer in the experience.h called int exp;.. would the fight.h "interact" with the experience.h file? heres an example as best i can come up with without having tried it yet:

This is the fight.h:

void fight()
{
     cout << "you killed the monster.";
     if (player_1.health > frog.health){
                         exp = exp + 15;
                         
                         else if (player_1.health < frog.health) {
                              cout << "You died.";
                              }
}

Experience.h:

void experience()
{
     int exp = 0;
     if (exp == 400){
                         cout << "You have gained a level!";
                         }
}

So what is happening is that the exp int is being created in the experience.h, but i need to know if it will also interact with other .h and .cpp files, since it is in the same project. If they do interact, i also would like to know if the int would be carried over to other .cpp's, like.. if int exp gained 20 in the main.cpp, if i printed the int from the experience.h would it have printed 20 aswell or would it be different values? If this doesnt make sense, just diregard it im very tired and will probably understand what im trying to do in the morning.

EDIT: I forgot to add, would i have to create all the variables i want to be used across all the .cpp and headers as a global variable in the main .cpp?
__________________

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 Nov 29th, 2005, 3:01 AM   #2
melbolt
Hobbyist Programmer
 
melbolt's Avatar
 
Join Date: Feb 2005
Location: PA, USA
Posts: 244
Rep Power: 4 melbolt is on a distinguished road
Send a message via AIM to melbolt Send a message via Yahoo to melbolt
I'm not much of a C++ guy but I believe what you've got there is merely a local variable and i think what you are looking for is a global variable.

since they are seperate functions they will not carry over to each other unless you pass them by value, pass them by reference, or make them global.
__________________
I have never let my schooling interfere with my education. -Mark Twain-

Xbox live gamertag: melbolt
melbolt is offline   Reply With Quote
Old Nov 29th, 2005, 3:13 AM   #3
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
Alright, so global would be before the int main() function in the main.cpp right? If so then this thread is done.
__________________

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 Nov 29th, 2005, 4:51 AM   #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
You should not define your functions in header files. Header files are for declarations and maybe some extern variables.
Read some tutorials on it, short one: here.
__________________
"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
Old Nov 29th, 2005, 6:17 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Please listen to nnxion. Header files are designed to convey information to multiple source files (by including them in more than one source file). Consequently, if they have definitions, one will get multiple definitions. Header files are pasted into the source file at the point of inclusion. There is nothing to be gained by having a header used in just one source file, except organizational divisibility. When the compiler sees it, the header and the source will be one thing.

To divide your source, put it in multiple files. Things that need to be known to more than one source file are declared an a header (or more than one header). That header is then included in each source file that needs the declarations. The compiler is converting statements to machine code. It isn't running the program. I needs to know what variables and functions look like, it doesn't need to use them.

At the end of a compilation or set of compilations one will have object files which need to refer to each other, usually. The linker links them together. At that point, things must actually exist somewhere, rather than just be prototyped.
__________________
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 Nov 29th, 2005, 7:08 AM   #6
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
Im not going by this thread anymore, i already found out that what i was trying to do is next to completely useless.. It just means more work than i have to do so im not even going to bother, i found an equally as good way of doing it, that takes not so much effort to figure out either.. its in my project thread if you care to see what it looks like now
__________________

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 Nov 29th, 2005, 7:55 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I hope you take the opportunity to check out the information provided in this "dead" thread after it died without warning. Misperceptions can complicate and ruin your code. The average member may or may not jump around trying to keep up, and may or may not move his or her germane replies about.
__________________
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
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 10:45 PM.

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