Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Hey Importing functions in C++?
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Geminias




PostPosted: Tue Nov 08, 2005 7:09 pm   Post subject: Hey Importing functions in C++?

i mine as well be asking this question specifically to wtd lol. wtd, how do you import a function you've created already so you don't have to keep writing the same functions (or copy pasting them) into new source codes.

for instance in turing there's a length function built in. I'd like to make a file a cache for all the functions i find useful and thus be able to easily transport them around into new sources.

am i thinking about this the wrong way? is there something better than doing this?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Nov 08, 2005 7:15 pm   Post subject: (No subject)

First off... what are you trying to find the length of? Smile

Next, the mechanism you're looking for are header files.

There's a simple way to do this, and a correct way.

The simple way is to put everything in a header file that might look like:

c++:
#include <iostream>

void foo()
{
   std::cout << "hello" << std::endl;
}


And you might call this "foo.h".

In the same directory you'd create "main.cpp".

c++:
#include "foo.h"

int main()
{
   foo();

   return 0;
}
wtd




PostPosted: Tue Nov 08, 2005 7:22 pm   Post subject: (No subject)

Now, the right way acknowledges that header files should not contain executable code. So, we separate "foo.h" into...

foo.h:

c++:
void foo();


foo.cpp:

c++:
#include <iostream>
#include "foo.h"

void foo()
{
   std::cout << "hello" << std::endl;
}


Then main.cpp looks exactly the same as it did before, but when we compile it, we provide both source files.

Instead of:

code:
g++ main.cpp -o program


We compile with:

code:
g++ main.cpp foo.cpp -o program
Geminias




PostPosted: Tue Nov 08, 2005 7:33 pm   Post subject: (No subject)

ahhhhhhh, i read about header files and made one with for a class, but since i barely understand the concept of classes i didn't think header files were good for functions.

anyway. danke sir.
Geminias




PostPosted: Tue Nov 08, 2005 7:37 pm   Post subject: (No subject)

oh and to answer your question i'm not trying to find the length of anything at the moment. i was just using length as an example function. i've already made functions to calculate factors of numbers, calculate length of words, make all lower case letters upper case, and so on... and i was looking for a way to implement them the correct way for a programming competition question.
wtd




PostPosted: Tue Nov 08, 2005 7:51 pm   Post subject: (No subject)

Ah.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: