I am fairly new to programming for computers. I have noticed that most of the C++ tutorials don't touch on the area of header files a whole lot. Does anyone know where a good header file tutorial is located?
I am fairly new to programming for computers. I have noticed that most of the C++ tutorials don't touch on the area of header files a whole lot. Does anyone know where a good header file tutorial is located?
C++ headers
But I don't really understand your questions, mind elaborating a bit?
Then, don't worry about header files or use them.I am fairly new to programming for computers.
Well I'm not really "new" to programming. I've been programming on TI calculators for about 5 years. By new I mean I've only been programming for computers for about a year, which isn't that long to me. I feel I have a fairly good understanding of the basics of C/C++. Sometimes I will look through the include files for Dev-Cpp and I find word and commands that are foreign to me, such as extern, the conditional statements that are preceded by the #, and all of the underscores. I was wondering if there was a tutorial that explains the process of making header files and libraries in detail. I am starting college and taking a programming class 1st semester and I would like to learn as much as possible before I start class as I'm taking and advanced calculus and would like to relieve some pressure.
Thanks for the help fellow C/C++ programmers.
I think this is a reasonable question. I often wonder what is actually happening when you include header files. I know that it is a means to access functions within the header file but it would be nice to get a more in depth of how the compiler acts upon the
#include<iostream>
statement
also i noticed if you get into the VC98 folder in MSVC++ and look in the include folder there
you see all the different header files.
Can i learn anything by clicking on the various header files or not?
If i can then how about a tutorial on the subject .
Thanks
Mark S.
I tried to make some guide about header files...
Maybe i'm not very good at english but try to understand something...
Quick guide to header files :
I'll give you one example :
This is your header file (.h).
Contents :
And you have main file (.cpp), where is the main function.Code:// file : myheader.h void foo() { // do nothing }
Contents :
myheader.h and main.cpp are seperate files.Code:// file : main.cpp #include "myheader.h" int main() { // call the foo function foo(); return 0; }
Lines that begin with '#' are preprocessor directives.
Preprocessor reads the code before compiler.
When it sees line that begins with '#', it executes the command on the right side from the '#' sign.
In our case this is #include "myheader.h".
'#include' tells preprocessor to copy whole contents of some file (myheader.h for us) to the file where the '#include' statement is used.
So, when preprocessor does its job, we have one big file that looks like this :
I hope this explained you some things about preprocessor and header files.Code:// combined file that preprocessor makes for compiler. void foo() { } int main() { foo(); return 0; }
If I made mistake somewhere, feel free to insult me.
You can download sample project here > Download
It should work on any newer compiler.
Code:.J?+. ?`+. .+1P .++. ,+` 4++. .+zq\ .:.?i .!`? .yz+. ?zdd ..:.^`J. ,!..?...Kyz+ .+dXM ..!p^.N.+ ,,^.a..`.#XO+. .zwWM ^`.Mc`JMhJ. .:JF`JM..^.#WOz` jwpMr`..NF.JMMM,! .JMMF.dJ..`JNWrc 0Wgb `!B:.MMMMM,: ,.MMMF.j$.` NHSZ` TWMp`.+;`MMMMMM.: ;.MMMMM.;+``JHW= 7Mh,``JMMMM"` .TMMMMb``.H#"`
Ok, let's have at it.I feel I have a fairly good understanding of the basics of C/C++.
Defining a variable as extern implies that it's defined somewhere outside the current scope. The extern keyword says to look outside the current block for the definition of the variable. If the variable isn't contained inside a block, then the search for its definition expands to the other files in your program.Sometimes I will look through the include files for Dev-Cpp and I find word and commands that are foreign to me, such as extern,
Those are called "preprocessor directives". The preprocessor's job is to prepare your code for the compile phase, whereupon the compiler will process your code into machine instructions. Learning all about the preprocessor directives is like learning another whole language. An example of a preprocessor directive is #define:...the conditional statements that are preceded by the #
#define pi 3.141
That tells the preprocessor to substitute the characters "pi" with "3.141" in any lines below the current line when the preprocessor encounters "pi" in your program(except inside comments or in string literals surrounded by double quote marks). You can also use #undefine, which says to stop making those substitutions for the lines of code below the line containing #undefine. If you do this:
#define pi
then "pi" will be replaced by nothing, i.e. it will be removed from the lines of code.
Another very common preprocessor directive is #include. That tells the preprocessor to substitute the contents of the specified file in place of the #include statement. With header files, you may also see the ubiquitous:
That hints at the fact that there is something more going on when you use #define. When you write:Code:#if !defined MY_HEADER_CONTAINING_MY_STUFF_123456789_H #define MY_HEADER_CONTAINING_MY_STUFF_123456789_H //C++ code here #endif
#define pi 3.141
pi becomes a "token", and when the token is found in your program it is replaced by 3.141. In other words, pi is some kind of entity that can be #defined or #undefined. That's important because in the line:
#if !defined MY_HEADER_CONTAINING_MY_STUFF_123456789_H
you are telling the preprocessor that if the token MY_HEADER_CONTAINING_MY_STUFF_123456789_H hasn't been #defined, then go ahead and continue looking at the next lines between #if and #endif. The next line is:
#define MY_HEADER_CONTAINING_MY_STUFF_123456789_H
That says to replace the token MY_HEADER_CONTAINING_MY_STUFF_123456789_H with nothing, i.e remove it from the rest of the lines of you program. You wouldn't expect to find that token anywhere in the lines of your program--all you want to do is #define the token. Once you #define the token, then the if statement will fail the next time you try to include the header file. The effect is that the lines of C++ code between #ifndef and #endif will not be included more than once in a file. Header files contain declarations, and if you include the header file more than once, you will include the declaration of a variable in your file more than once, which is an error. For instance, if you do this in a simple "hello world" program:
int num = 3;
int num = 3;
the compiler will say:
NO WAY! REDEFINITION OF THE VARIABLE NUM! YOU LOSE! COMPILATION FAILED!!! HAHAHAHA!! GO BACK TO VISUAL BASIC CHUMP!!
Other preprocessor directives:
#error -- terminates the preprocessing and displays the specified message. For instance,
#pragma -- an implementation defined directive, ignored if not recognizedCode:#ifndef myFile #error "Something went wrong with myFile" #endif
There are also standard macros defined by the preprocessor, e.g.
__LINE__ : the line number of the current line as an int
__FILE__ : the name of the source file as a string literal
etc.
I'm not sure what you mean. Underscores can be part of names. I think by convention leading underscores followed by a capital letter are used in C++ libraries, so you don't want to use variable names like that in your programs.and all of the underscores
Why might you want to put your declarations in .h files and your definitions in .cpp files? See this recent thread:
http://cboard.cprogramming.com/showthread.php?t=68178
Remember when you include a .h file into another file, the include statement is replaced by the .h file, so any legal C++ you could type at the location of the #include statement can be contained in the .h file.
1) Name your file with a .h extension.I was wondering if there was a tutorial that explains the process of making header files
2) Put some inclusion guards in the file:
3) Put some C++ between the inclusion guards.Code:#ifndef SOME_UNIQUE_NAME_H #def SOME_UNIQUE_NAME_H #endif
4) Include the .h file wherever you want those lines of code to appear.
Dynamic link libraries(dll's)?? I will defer because I don't know how.tutorial that explains the process of making libraries
(...the above was garnered from "Ivor Horton's Beginning C++", chap 10: Program Files and the Preprocessor)
Last edited by 7stud; 08-04-2005 at 12:52 AM.