0

I was wondering how to include files in C++... I was looking at a lot of different things and could not figure it out. I just want to include another function or method or w/e you call it.

I know you need like #include "filename.cpp"; but I don't know how to use stuff from filename.cpp or .h or w/e you want to call it.

Thanks, I know java fine btw if you can explain it though that. (It does not seem to be the same as java at all.)

Zeveso
  • 1,274
  • 3
  • 21
  • 41
  • In general, we include header files. Not the source files. – Mahesh Feb 22 '11 at 02:49
  • @Mahesh then how do you include header files and use them??? – Zeveso Feb 22 '11 at 02:50
  • You just `#include "filename.h"` or `#include ` (the search rules for finding the file are different). `filename.h` will declare (but not define) functions and external variables. You use them just as if they had been declared right there. usually there's documentation somewhere about what's declared in the file you're including. (The actual definitions are in separate compilation units that get combined with your code later.) – Ted Hopp Feb 22 '11 at 02:54
  • 2
    Almost nothing in C++ is "the same as Java." If you are trying to learn C++, it would behoove you to get [a good introductory book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – James McNellis Feb 22 '11 at 02:55

4 Answers4

2

Include files are used to describe interfaces or classes that are to be used by other elements of your application. In essence, the description of your class is included in the header (.h) file. The implementation of the class is contained in the source (.cpp) file. For example:

Your header file looks something like this:

// MyClass.h
class MyClass {
public:
    MyClass();
    void Method1();
    ...
};

Your implementation file looks something like this:

// MyClass.cpp
MyClass::MyClass() {
    // implementation details
}

void MyClass::Method1() {
    // implementation details
}

Now anyone needing to use MyClass would include MyClass.h in the following manner:

#include "MyClass.h"

Hope this helps.

Lou
  • 1,955
  • 14
  • 16
2

Usually, in headers goes the declarations and in source files goes the implementation. Though there is a exception for this in case of templates. Remember that only source files get compiled. So, what ever symbols present in the source file must be known to the compiler. Now taking an example -

foo.h

struct foo
{
    int number ;
    foo( int x );
};

foo.cpp

Now, there is a method that is unimplemented in struct foo i.e., constructor. If you need to implement that in a seperate source file, you need to #include "foo.h". With out it, if you just go on implementing it's methods, compiler doesn't know what is "foo".

#include "foo.h"

foo::foo( int x )
{
     number = x; // This can be achieved through initializer lists too.
}

Before even the compiler stage, preprocessor copies the contents of foo.h to foo.cpp. After preprocessing, the original foo.cpp turns to be -

struct foo
{
    int number ;
    foo( int x );
};

foo::foo( int x )
{
     number = x; // This can be achieved through initializer lists too.
}

Now, every method/symbol that is being defined/used in the source file is known to the compiler to which it belogs to. And in general, the corresponding source file of x.h will be x.cpp. Also know that, every source file passes these stages to give a final executable. ( PreProcessor -> Compiler -> Linker ). Hope it helps to an extent.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
1

The convention is to include only header (*.h or *.hpp) files. These files are generally used to define types and macros, etc.

How you use it really depends on what your apps is doing--you've been rather vague. For example, if you define a class, it is common to declare the class and it's members in the header file. The actual code for the methods can be placed in a *.cpp file.

As long as you #include the header file, the compiler knows what syntax is needed by those methods.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • lets say I have main.cpp and I have a file that does an AES encryption of files and is called AES.h... how do I use AES.h in main.cpp? – Zeveso Feb 22 '11 at 02:53
  • @user516664: To use the header file, you #include it. However, how you call/use the classes, etc. defined in the header file is a completely separate question from using header files. Instead, it's a question about calling C++ code/classes/data structures, and that question is meaningless without knowing the implementation details of the code in question. – Jonathan Wood Feb 22 '11 at 02:56
-1

Including a file in c++ differs between the standard library file and programmer-defined files.

For the standard library we write :

#include <FileName>

while for programmer-defined files we write :

#include "FileDestination\FileName.extension"

notice that there are no semicolons.

if the stuff you want to use are within a namespace, you should either mention the namespace or use it, to use a namespace write :

using namespace NAMESPACE_NAME;

to mention the namespace without using it is easy, like writing :

std::cout << "Hello, World!\n";

Hope that this is useful!, and so sorry for my bad english.

Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
  • 2
    vs. "foo.h" is not about standard library vs. user-created files. It's about whether the current directory is searched (as in "") or not (as in <>). Also, `using` directives should not be used in header files, only in implementation (.cpp) files. – John Zwinck Feb 22 '11 at 03:05