MESSAGE
DATE | 2010-02-09 |
FROM | Ruben Safir
|
SUBJECT | Re: [NYLXS - HANGOUT] C++ Workshop _ Syntax Basics
|
3) Original Code and runtime directives starting with main.
C++ has added a new programming directive called the "using" directive which is used to create namespace. Namespace gives a finer grain control of which symbols your code recognizes in a specified space. Its really important and in many ways was a long time coming to the C family of languages. Most importantly it prevents you from accidently stepping on library symbols or words that you might not have been aware of or that programmers after you might not be aware of. It also allows to define the same symbol in multiple locations of your code without stepping on your own toes.
So todays modern C++ main program files might look something look something like this: #ifndef TOP_H #include #define TOP_H #endif
#ifndef INTARRAY_H #include "intarray.h" #define INTARRAY_H #endif
using namespace std; int main( int argc, const char* argv[] ) { //YOUR PROGRAMMING CODE }
There is a catch to the namespace usage though. It might very well be that your library files, especially if you are creating them yourself, which you will in C++, have the using directive. If so, you will likely depend on them.
Header Files: Header files normally have a .h suffix. file1.h would be an exampe of a header file for C or C++. These are the files that are being included in you #include preprocessor directive. These files are often distributed with a program and you can examine them. They are useful for discovering the definitions of programing objects in libraries are used and often programmers will point them out to you as a form of documentation, which itself is a practice I'm not happy about because many programmers mistake them as a sunstitute for real documentation.
Library Files: After researching this, it has occured to me that there is an abiguity about the structure of C and C++ Programming files. Professional programs generally have header files that are described above, but don't have a proper name for the coding files that associate with the headers and which produce object binary files and static or linked libraries. For a beginner this is all confusing and the lack of proper nomenclature makes this all the more harder to learn. I little bit of compiler theory is needed to understand the files structure and binary construction of your program. For now, I just want to point out that programming objects defined in your header file for use in your programming has to have source code to produce the actually machine code that is represented by the symbols in your header file. Those library source files will not have the main function. But the compiler can be asked to create what is called object files, which are partially processed C binary code for later inclusion in your program. When we look closer at the gcc compiler we will examine these object files and learn why they are so important.
What is important to say, however, is that in C++, because of its object orientation and its emphasis on creating Application Programming Interfaces (API), most of the C++ coding you will do is taking place in these library C++ source files (which I will refere to as Library Code from here on out).
There are two kinds of Library code files that you will work with, that which you create, and that which you borrow from your system for inclusion in your programs.
User defined:
User defined library files define the code to create working programming objects that are normally declared in your matching header files. These programming source code files look just like your main programming file except they don't have the main function. Your top most main programming file is dependent on these library code files. The code they produce has to be linked into your program by your compiler.
Standard C++ or Packaged third party: These are the standard libraries, either in source or in object files, that define standard language needs and are usually found somewhere in /lib or /usr/lib on your system.
Standard C++ File Creation:
All our C++ programs has to be created in with a standard text editor. The code that the compiler works on, also known as translation units for the compiler at straight ASCII text. You can NOT use a word processor. My prefered text editor is VIM or GVIM, which is a derivitive of VI. VI is the standard text editor on Unix like systems and there are many tutorials for it around the internet. Other editors include EMACS, and then there are C++ working environments like Anjuta, which I strongly discourage. I discourage the Programming Integrated Programming enviroments because with GNU and Unix like systems, your OS is your integrated enviroment, and I believe one should learn to use the standard tools that are on your GNU/Linux system.
A standard C++ file needs to have at least one function defined. We will look at functions (also called methods, more closely later, but a new programmer should get use to looking at them from the start, since everything in C++ is encapsulated in a function called main. Functions are defined by following structure
"return type" "function name (the symbol)" ( Arguement list) {
Statements that end in semi-colon;
}
Functions do not that semi-colons after the closing curly brace.
The main function looks like this
int main(int argc, char * argv[]){ return 0; }
A realistic C++ main program file, including prepocessor directives would look as follows
#include using namespace std;
int main(int argc, char * argv[]){
return 0; }
The curly braces forms a block in which the coder can add really as many instructions as they choose to. These blocks of statements are seen in many C++ syntax structures including functions, if statements, for loops and other structures. While the above is a minimal C++ source file structure, generally most of the heavey lifting of your code takes place outside of main in user defined functions which you create, as well as objects. A more realistic first program skeletan might well look something like this:
#include using namespace std;
void oxygen(){ cout << "oxygen()\n";} void hydrogen(){ cout << "hydrogen()\n";} void helium(){ cout << "helium()\n";} void neon(){ cout << "neon()\n";}
int main(){ oxygen(); hydrogen(); helium(); neon();
return 0; }
Here we can see the declaring and defining for 4 user defined functions that are outside of our program, and get instantated only when called. the for functions are called read, sort, compact and write.
And notice that we are using the standard namespace called std.
|
|