#ifndef Test_h
#define Test_h
#endif

May I know what are the above 3 syntax for? How can I use it?
Is is used only in header file?

It's a guard against including the information in the header file more than once(which can cause a mess with redeclarations etc.).
You'd put the contents of your header between lines 2 and 3. At first pass if this symbol Test_h is not defined, processing goes to line 2 which defines Test_h and then continues with the rest of the header until the endif.
If you include this file again somewhere and that symbol is found to be already defined the preprocessor hits line 1 and skips to line 3 since the ifndef is false.

I can just use this right?

#include <iostream>
using namespace std;

It's almost apples and oranges but not quite. Say you have a class A and you put the declaration for that class into it's own header file,one that you wrote. Say that your class B header file, B.h, includes A's header, A.h. If you then include A.h and B.h in main.cpp you will have 2 copies of A.h being included. Your guard prevents this and it's only included once. It doesn't seem like a complicated situation but with a large project there can be crossovers all over the place.

In the case of the headers that we normally include for library files (like iostream) they have their own guards in place to prevent this.

#ifndef _GLIBCXX_IOSTREAM
#define _GLIBCXX_IOSTREAM 1

#pragma GCC system_header

is in the <iostream> header. I'm not sure if this particular pragma is involved in that whole process or not.

If you work with Visual Studio (and possibly other compilers) they also use the #pragma once directive to accomplish the same task.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.