MESSAGE
DATE | 2010-02-08 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] C++ Workshop _ Syntax Basics
|
C++ Syntax
Before exploring how we actually represent data in our C++ programs, I want to introduce a formal discussion of basic C++ syntax, which, like data types, doesn't get enough attention in most standardized texts.
All programming languages require syntax rules in order for the compilers, to parse and create working machine code. These syntax rules require basic understanding of core componenets. These components include files, structure, statements, data and operators.
Starting from the top, first you have files, and files usually have naming rules. C++ inherits from C nearly all the file structures, and actually requires a greater knowledge in more detail and at an earlier level of expertise. C++, because of its object oriented design depends heavely on library creation. In fact, even for beginners, most of your work happens on the library level.
All C programs inherit from the Unix enviroment, which co-developed with C, the need for an initiation of the main function definition. All C programs start with main, and main will then absorb and use all other parts of the systems libraries and programming to produce your completed program. Main is located in your upper most programming file.
Standard Programming Files: A standard programming file is when the top most programming will take place. In the C language, most of your code, espeically as a beginner takes place in this file. Most commonly these files have a suffix of either .cc oro .C. file.C for example is a standard C++ File name.
A standard programming file will have several components:
1) Include Prepocessor Directives - These import header files and define the definitions of the symbols which your not spontaneously creating, that your program will use.
And include directive might look like this:
#include
Which tells the compiler to load up the defintions of all the functions and objects defined in the iostream library.
Standard C++ libraries are included using the angle blacket notions as above. They are search for by your compiler in a set of standard locations which are defined by your compiler and programming enviroment (something I wouldn't mind understanding better on modern Linux and GNU enviroments).
If you use the syntax
#include "myheader"
with double quotes, the compiler will look for these headers in the local directory.
C libraries are accessable in C++ and can either have a standard C language notion
#include #include ***Note the .h suffix being included*** or use the C++ version
#include #include
2) Macro and other Prepocessor Compiler Directives - Help set up conditions in which libraries and header files are brought into your program to help prevent duplication and to create different versions of a program as might be needed for differing archetecture or conditions.
The list of Preprocessor Directives are as follows: #define #endif #ifdef #ifndef #include (as discussed above)
A Macro directive might look like this:
#ifndef HEAD #define HEAD #include #include
#endif
Development of skills using these directives, which is a language in a language, is one of the skills that advanced C and C++ coders have that seperate them from amatures.
This Macro is telling the compiler to include the libraries and symbols for iostream and string from the core C++ library if and ONLY IF, the symbol HEAD, in the compiler instructions, haven't been already defined.
There are also constants that your program has which the compiler adds to your code which include
__cplusplus __DATE__ __FILE__ __LINE__ __STDC__ __TIME__
__DATE__ and __TIME__ are the date and time the program is compiled.
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 PROGRAMMIGN 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.
|
|