# C++ beginner Question: Where to put INCLUDE files?



## rankbeginner (Jun 24, 2004)

Greetings ... and many thanks in advance. I am a student trying to learn C++ via distance learning, without many resources except books and this forum.

I am using the Microsoft Visual C++.net compiler. And I'm working with an old but good-for-beginners book: The Apprentice C++ Programmer. The book has a disk with some files I want to use: they are INCLUDE files that the authors have written: named "CDPLayer.h" and "CDisc.h". 

The authors call these flles PDTs -- Programmer Defined Types -- and I'm not quite sure what that means. 

I would like to use these two files along with some source code that the authors give. Any ideas about what directory to place these files? ... 
And also, suppose, 4 light years from now, I can write my own INCLUDE file: where would that be placed?

Thanks again ... RB


----------



## itlu (Feb 23, 2004)

PDT MEANS PROGRAMMER WRITES THOSE HELP FILES SO THAT HE CAN USE THOSE AGAIN ANDF AGAIN. THAT IS , JUST A TYPE OF MACRO,BUT HERE U CAN STORE THOSE TYPES IN LIBRARY AND THEN INCLUDE THEM IN PROGRAM NEEDED.

U CAN INCLUDE THOSE FILES WITH SIMPLY

#include<CDPLayer.h>
#include<CDisc.h>


----------



## itlu (Feb 23, 2004)

TO CREATE HELP FILE HERE IS WHAT ONE HAS TO DO

#ifndef __REQUIRED_FILENAME_H_
#define __REQUIRED_FILENAME_H
#endif

---------
SAVE IT AS FILENAME.H

AND THEN YOU CAN USE IT ANY PROGRAM AFTER COMPILING SUCESSFULLY.


----------



## dmneoblade (Apr 15, 2004)

capslock off man... and you are wrong.
first of all, whenever you have a program where you want to use things in those include files, copy them to the folder that your project is in, and use the command

```
#include "filename"
```
before any functions.

and, to make your own headers (VERY USEFUL), you make something like:

```
//This file is myheader.h

//This is so the compiler does not get stuck in an infinate loop...
#ifndef MYHEADER_H
#define MYHEADER_H
   //Source code goes here.
   //But only certain things...
   //Like global variables,
   //Or function prototypes.
   //You will eventually learn about these things.
   //Now that we are done with what we want in here,
   //we must tell the compiler that we are done
#endif
```
EDIT: oops, forgot this part:

save that file with the rest of the source code for that program.
then, whenever you need something from myheader.h, put this line of code in the top of the file

```
#include "myheader.h"
```
That should make things a bit clearer.


----------



## itlu (Feb 23, 2004)

THX dmneoblade


For pointing 2 my silly mistake. 
Always Thank full to one like u who don't hesistate 2 point out others mistake back 2 them


----------



## dmneoblade (Apr 15, 2004)

Ah, no problem. I just don't like to form bad habits in newbies. I remember some of my first programs.... UGLY! I threw #include statements inside the main() function...


----------



## rankbeginner (Jun 24, 2004)

Thanks -- this helps a lot. Now I can use the examples in this older C++ book, with lots of files that the authors have created. 
--RB


----------



## coderitr (Oct 12, 2003)

Another important tip IMO from a seasoned developer: never write code when you're drunk.  You'll thank me later if you heed this advice.


----------



## dmneoblade (Apr 15, 2004)

Another important tip:
strange code is the result of coding at 2 AM, with less than 5 hours sleep the previous night. However, such code usually works wonders.


----------



## anshul (Sep 3, 2004)

plz give a sample code of writing own header files in C/C++


----------



## Shadow2531 (Apr 30, 2001)

dmneoblade already gave a perfect example above, but here's another example.

example.cpp

```
#include <iostream>
#include "example.h"

using namespace std;

int main() {
    print_hello();
}
```
example.h

```
#ifndef EXAMPLE_H
#define EXAMPLE_H

#include <iostream>

void print_hello() {
    std::cout << "Hello" << std::endl;
}

#endif
```
I left it super simple. Obviously, it's not necessary to use a separate header file for just that and not necessarily recommended, but it shows exactly what you need to know.

Another example and probably better than my first example would be

example.cpp

```
#include <iostream>
#include "function.h"

using namespace std;


int main() {
    print_hello();
}
```
function.cpp


```
#include <iostream>

using namespace std;

void print_hello() {
    cout << "Hello" << endl;
}
```
function.h 

```
#ifndef FUNCTION_H
#define FUNCTION_H

void print_hello();

#endif
```


----------

