Creating a Project in C/C++
Using various filetypes:
.h .cpp

This is a little IDE specific but I'll make it as general as possible.

This is quite specific, but it can be generalised. Basically, when you create a new project, you should already have a .cpp file added to it, since this is where the main loop goes. This file is often called main.cpp

Now, to add a new file, most IDEs will require you to go to File>New. You should then see an option called something like "Header file." If not, then it is most likely File>New>file...

Now, make sure that it is a .h file that you are creating, give it a name, and click next/OK. Follow the wizard until you are returned to the coding view, and you should now be working in a header file (WhateverYouCalledIt.h)

First off, I'm going to add some header guards. This is basically to prevent the file being included more than once, which can cause errors (saying that things have already been declared, and are now being redeclared)

The easiest one is a simple one-line tool. However, it's not used very often.
#pragma once

The more common method is to use #ifndef like so:
cpp
#ifndef MYHEADER_H_INCLUDED
#define MYHEADER_H_INCLUDED
// header content goes here
#endif
This looks more confusing than it is. let's break it down a little.

A header file is most often used to contain variable declarations, function prototypes, and class declarations. In this example, I'm just going to use a few variables, because you may not yet be at a level of writing functions/classes.

Here is a very basic header file:
cpp
#ifndef MYHEADER_H_INCLUDED
#define MYHEADER_H_INCLUDED

int a; // declare an int
double b; // declare a double
char c; // declare a char

// notice that I haven't assigned any values here?
// we do that next, in a cpp file

#endif
Now it's time to make a .cpp file to define everything in that header file. So, go to File>New>File... again. This time, however, instead of selecting .h we're selecting .cpp
Give it the same name as the header file. This will make it easier to manage later on. smile.gif

Let's include our header file, and assign some stuff to our variables:
cpp
#include "myheader.h"

a = 4;
b = 20.2994;
c = 'D';
Pretty simple, huh? That's because we're not really learning any new code. All that we're doing is breaking up some code into manageable files. It seems a little pointless here, but it's much better to have a few files with a few hundred lines each than one big file with thousands of lines. It simply makes it easier to manage!

Now to write the code for our main.cpp
All we need to do is include our header file, and the contents of the cpp file will be included for you.
This is main.cpp:
cpp
#include <iostream> // input/output stream
#include "myheader.h" // use " " instead of < > so the compiler knows that it's a local header file

using namespace std;

int main ()
{
cout << a << endl << b << endl << c;
cin.get ();
return EXIT_SUCCESS;
}

There are no scoping issues, since these variables have simply been dragged into the program at compile time.

Happy coding! smile.gif