Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Lesson 9



Defines
Defines are a fantastic help in logical programming, they replace non meaning full numbers, values into human understandable english. Basically they can be similar to constants, as they are placed as global, unless you dont want it to be ofcourse, but that is the usual. Also there are special types of definitions which can be usefull, we will only be using 3 types which are:
#define A_NAME A_VALUE		//Every instance of A_NAME will be replaced with A_VALUE
#ifndef A_NAME			//If A_NAME has not already been defined
#endif				//End the if definition
Now, the use of the first one #define is to define a string or name as a value or chunk of code, our use of this comes as we can define such things as ASCII key codes, so we could define the ESC key as 27 and the ENTER key as 13 like this:
#define ESC 27
#define ENTER 13
and then in our program we just have to write something like while(Key!=ESC).. as that is much more meaningfull and easier to write than 27 all the time, and makes then easy to remember. Defines do not use any memory, the compiler when you compile your program goes through all your code and when it sees one of the key words you have defined, like ESC it will replace that with the value that you have defined it as.
Example Program using #defines
#include <iostream.h>
#include <conio.h>

#define ESC 27
#define HELLO "Hello world, hope you are learning lots"
#define MY_STRING "This is a very long string" << endl \	
		  << "And its still going on the next line..";	

		//the special slash after the endl above tells it im still defining it
		//on to the next line

void main()
{
	char Key;

	cout << HELLO << endl;
	Key = getch();
	while(Key!=ESC)
	{
		cout << MY_STRING << endl;
		Key = getch();
	}
}

As you can see just the standard #define can have many uses and hold any values, try to implement this to improve clarity in your code, and soon it will be understandable by almost anyone.

Using multiple code files for your project
This technique is used by all the big programmers, as it splits up your project into sections so you can easily have more than one person working on the same project / program. First I want to go over a bit of programming structure and logic, programming is all about logic, therefore clarity and effeciency should always be of top priority no matter what the size of your program. Some basic rules to stick by: try to use as little global variables as possible if any, have only 1 entry and one exit point in each function which means no gotos and only one return at the end of your function, keep all variables declared at the top of each function, name each variable / function as meaningfull as possible, use consistant naming standards, constants and define names should be capitals with spaces as underscores only, put all related functions together like in the same source file, put all structure and function definitions in .h (header) files which are named the SAME as the .cpp file only difference being the extension.

Now with those standards in mind, especially the last one I will go on to how you use your own libraries.
All c or c++ functions should go into an external text/ascii file other than the main, with the extension .c or .cpp respectively. So all functions will go in their, but not their prodotypes, structure definitions and required libraries. All these go into the .h file. So every time you make a .cpp file, make a .h file with the same name in which you put all the function prodotypes, structure definitions and required libraries into, and make sure you name it the same as the .cpp file! Every .c or .cpp file should have its respective .h file, even main will now have its .h as in that you should put the main include libraries, definitions and any structures used in main.

Now you should end up with the files main.cpp, main.h, functions.cpp and functions.h obviously functions would be named something more appropreate in your case depending on what group of functions you have put in their, and also you may very well have more than one other .cpp file, you can have 100 if you want. Now the fun starts, in every .c or .cpp file you have, at the top of that you must put a link to its respective header file and any other function files it may use. So in your file called functions.cpp you would put #include "functions.h" at the top of it, and if you have more than one external .cpp file which contains functions inside them you wish to use, then link its respective header file, like say you had another functions file which was named menus.cpp along with its respective menus.h, and you needed to call or use one of its functions / global variables within one of your functions in functions.cpp, then you simply link its header file like this #include "Menus.h" and you now can call any function within menus.cpp from functions.cpp.

The last thing to do when using external files is to make sure you do not get dublicates of the same thing, as when using this technique, the compiler actually makes each .cpp file into a .obj file then it links all the .obj together to make your final .exe . So this is where the #ifndef and #endif comes in handy, the first stands for if not already defined and the second stands for end if which in this syntax is end definition. Now we place the #ifndef at the top of each .h file we have only, not any of our .cpp files. The #ifndef is only a statement which if true will allow code below it until it finds an #endif else it will simply ignore it all until it finds the #endif statement. So right below the #ifndef we place a #define statement which will define our header file to a common name which is same throughout program compilation, usually it is good practise to use this syntax for the header definition, HEADERFILENAME_H as it avoids conflicts. So when we do all this together, it comes out like this:

#ifndef MAIN_H
#define MAIN_H
Header code .......
#endif

This says if MAIN_H or this header file has not already been defined by another .cpp file, then define it as MAIN_H and that will make the compiler go through it all and compile it, it will also look at the related .cpp file for each definition of each thing like function or structure and compile that.

If you have done this right, your new projects should look something like this:

//Main.cpp

#include "Main.h"
#include "Menus.h"

void main()
{
	CallMenu();		//Call function from within Menus.cpp
}

...............................

//Main.h

#ifndef MAIN_H			//If main.h has not already been defined 
#define MAIN_H			//by another file, define it so it gets compiled

#include <conio.h>
#include <iostream.h>

#define SOMETHING "Something"

struct sdefinition
{
	int num;
	char key;
};

#endif				//End definition
...............................

//Menus.cpp

#include "Menus.h"
#include "Main.h"

MyStruct Crap;			//Use structure definition from main.h

void CallMenu()			//This is prodotyped in Menus.h
{
	cout << "Call menu...";
}

...............................

//Menus.h

				//Attempt to define all the functions prototyped heree
#ifndef MENUS_H			//Check if Menus.h has already been pre-compiled
#define MENUS_H

#include <string.h>

void CallMenu();

#endif				//end header file definition

......................
Well if you get that, its not that hard really, your program will be much more readable, structured and look impressive. Also when you get really advanced, you will know that the compiler doesnt have to recompile unchanged bits of code, so makes compilation of big projects much faster. It generates .obj files for each .cpp file you have so they can be converted into .dll files and used on other projects. Basically the main use of this is to make code far more readable and clear, and allow for multiple users to code on the same project, just different functions and have it all brought together easily.

I belive a quiz is not really neccesary for this tutorial, so ill just give you an actuall multiple .cpp file project if have made - Click here


Back Home