![Smile :)](./images/smilies/icon_smile.gif)
this is just a reference 2 help you better under stand some of the more advanced tut's and demo's
![Smile :)](./images/smilies/icon_smile.gif)
1- C++ Structure
(found this at http://homepages.ius.edu/JDRUIN/html_pa ... ucture.htm )
--------------------------------------------------------------------------------
C ++ Structure
--------------------------------------------------------------------------------
C++ is still one of the most powerful languages in use today. This, like anything, has a trade off. C++ can be difficult to learn because it is not very forgiving; especially not to beginners. This page is designed to help beginners by providing examples. We are all teachers and learners of something. Feel free to copy some or all of the functions. Help others when they need help.
This is a reference page for beginners to see sample code. I recommend an instructional book such as Sam's Teach Yourself C++ in 21 days from Liberty Publishing to supplement self study. Files available on this site are currently kept in PDF. Download a free viewer from Adobe if needed:
Download Adobe Acrobat Reader©
--------------------------------------------------------------------------------
Basic Structure of Examples:
A C++ program is usually made up of three files, the header, the driver, and the implementation file. Some of the reasons include:
Reusability of the code is easier if:
only functions are kept in one file (implementation)
only headers are kept in another (header file)
all other code is kept separate in yet another file (driver)
If functions and headers are kept separate, these files can be "included" in any number of projects without ever looking at the code again. This is where the #include statement comes into play. When the #include statement is used to call <iostream.h>, you are just using code some other programmer has already written and compiled, then you are inserting that code into your project to serve some purpose.
Implementation and Header Files can even be compiled in advance (in nearly any language) and then called from any program that wishes to use them.
If a C++ implementation file is compiled in advance, even another programming language could call those functions. VB could call the quick sort of C++ for example. VB cannot normally solve problems recursively but has fantastic user interfaces. Programmers can get the best of both worlds with precompiled, well-filed code.
Code is easier to debug if "like code" is kept separate from "unlike code".
When one programmer wants to reuse another programmer's code, having to cut and paste is inefficient. Keeping functions separate from "main()" makes life easier for everyone involved.
In simple programs, these C++ program files are generally:
Header File: Function prototypes are kept here. A detailed discussion of header files is in the first example (PDF). A prototype is sometimes called a signature.
Implementation File: Functions that are "called" are defined here except "main()". Main is a special function that is called by the operating system.
Driver File: Also called the client file. "main()" lives here. The operating system looks for main() inside this file as soon as execution begins. The header files need to be "included " (#include) in your driver file, just above main().
You can imagine these files to look something like this:
Header.h
I am the Header File:
I store function prototypes. I tell main() what to expect from function that are stored in the implementation file. When main() calls one of these functions, I have already told main() what kind of memory to allocate. I have to be included in the driver file above main(). I am saved in the same folder as the driver and implementation files.
Driver.cpp
I am the Driver File:
Main() lives here. I might look something like this:
#include "the_header.h" // for my own functions
#include <iostream.h> // for cout and cin
void main()
{
call functions in the implementation file. I can do that since Header.h already told me how to use them.;
do stuff;
}
Implementation.cpp
I am the Implementation File:
Any functions called from main are stored here. I only contain functions. If you want to reuse functions, just include me in your next project. You will save lots of time and avoid cutting and pasting.
A note on #include:
#include basically cuts whatever is in your header.h file and paste it in place of the #include statement.
This explains why you get an error on the driver file when you leave a ";" off of the last statement in the header file. The header file is stuck, line by line, in the exact place where the #include statement resides. The error is "transferred" into the area where the #include was.
This also explains why the #include statements must be in very particular locations. #including the header is exactly the same as if you simply wrote the entire header file right there where that #include statement is located.
Why not just write the code and skip the #include stuff? This C++ code is already hard enough to read as it is. Keep the code separate and work on one file at a time. There a plenty of reasons why C++ is hard. Do not make it anymore difficult by stuffing all the code into one huge file.
A header file is necessary. When different people are working on other functions, they need your header to know how your functions work so they can write there code while yours is still in development. Having separate files is a necessity, even if it seems strange now.
When Classes are present, files are broken down even further.
Class Header File: The class is defined here.
Class Implementation File: The functions that belong to the class are defined here. They are called by objects using the "." (dot) operator. The programmer who wrote this code is usually not the one currently using the code. The programmer who wrote the class code is the author of the class and the programmer using the class code is the client. Both people are programmers. The client is using the code of another programmer so that the client does not have to rewrite code already in existence. You are your own client when you write both the class and then write a driver to demonstrate your class.
Client Driver File: "main()" lives here. The programmer that is using the class is accessing the class object through their own program. This programmer is the client. The class was (usually) written by some other programmer long before. The user of the class writes his or her code here in the client driver file. The client uses objects just like they would use and int or a float. The client declares the object then uses the object but the object invokes its own functions. This is the same as a programmer adding a float to another float. The programmer just uses the floats but does not call the add function. C++ knows when two floats have a plus sign between them, it needs to call the add function for two floats.
Client Header File: Function prototypes for the driver (client) are kept here. Class definitions and class headers are NOT kept here.
Client Implementation File: Functions that are called by the client for use in the clients driver file are defined here except "main()". Class functions are NOT kept here.
--------------------------------------------------------------------------------