简体   繁体   中英

Include files in c++?

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.)

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.

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 ie, 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 xh 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM