4 Replies - 54871 Views - Last Post: 05 June 2010 - 01:50 PM Rate Topic: -----

#1 logBase2   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 27-December 08

Call function from CPP file

Posted 27 December 2008 - 02:41 PM

Hi,
I'm quite new to C++ but am picking it up quite quickly as I'm already familiar with C#. However, I'm confused with header files and CPP files. I have a CPP file which contains a function. How do I call this function from my main program? Do I *have* to create a header file? The example program that comes with this CPP file has a working win32 example, but it also includes other libraries and DLLs as it carries out various tasks. I've noticed none of these have a header file for the file but still call the function. How is this possible? I'm using Visual Studio 08.
Thanks for any help.

Is This A Good Question/Topic? 1

#4 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5615
  • View blog
  • Posts: 14,693
  • Joined: 18-April 07

Re: Call function from CPP file

Posted 27 December 2008 - 04:31 PM

*
POPULAR

Header files are used to define class declarations or give function prototypes. Prototypes can be considered "hints" to the compiler to let it know what functions are coming up later in the code.

CPP files are source code files that typically create the function implementation that was hinted at in the header file. So if I hinted at a function be called "void test();" the CPP typically would then define test() and the code inside that function.

Because of this header files typically define the outline of what is to come and CPP files actually create the flesh.

Now in order for a CPP file to know about the implementation in another CPP file you typically have to define a prototype in a header file and include the header file in the first CPP file. This again lets the compiler know that in one of the source files there is going to be a definition that was defined in the header file.

Assume we have a main.cpp file like this...

#include <iostream>
using namespace std;


int main() {
    test();
    return 0;
}



I can't simply add an #include "otherfile.cpp" and expect to see it. At least not in the VS 2008 way. We have to define a prototype to it in a header and then include the header. So I create a functions.h file like so...

void test();



Notice we have just told the compiler that we are going to have a test function which takes no parameters and is void of a return type.

Now in a second CPP file we define the body of this function...

otherfile.cpp

void test() {
	int b = 100;
}



Here we have the body of the function we hinted at in the functions.h file. Now if we go back to our original file and add in the include file for the functions.h file the main.cpp file will see the code of the otherfile.cpp only because it knows there is going to be a function called "test()" and it will be defined in a source code file.

#include <iostream>

// Include the header which hinted at code in the otherfile.cpp
#include "functions.h"
using namespace std;

int main() {
    test();
    return 0;
}



So what does this all mean? It means that we could have 100 cpp files with various functions and then put all our "hints" in the functions.h file and it will see them all from main.cpp.

One last note I want to make is that you will see programs that define the class declaration in the .h file and put its implementation code in a cpp file of the same name. Like myclass.h and myclass.cpp. You will also see it where they move the functions of the class into the h file (but after its declaration). As long as it sees the declaration first it will know to look for its functions.

In conclusion yes you have to include .h files if you want to separate the implementation across multiple cpp files. You could also put the implementation straight into the header file (even though that is not very good design). Either way header files will be needed in a multiple file project which is using functions from one another.

:)

This post has been edited by Martyr2: 27 December 2008 - 04:32 PM

Was This Post Helpful? 6
  • +
  • -

#5 logBase2   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 27-December 08

Re: Call function from CPP file

Posted 28 December 2008 - 11:01 AM

Best. Answer. Ever. lol :D Thanks Martyr2, that was very helpful and can actually kick start my project!
Was This Post Helpful? 0
  • +
  • -

#6 Guest_pubis*


Reputation:

Re: Call function from CPP file

Posted 05 June 2010 - 01:17 PM

Martyr2, you are my hero!
Was This Post Helpful? 0

#7 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Call function from CPP file

Posted 05 June 2010 - 01:50 PM

You would have better PM'ed him instead of reviving such old topic.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1