MESSAGE
DATE | 2010-02-14 |
FROM | Ruben Safir
|
SUBJECT | Re: [NYLXS - HANGOUT] C++ Workshop _ Syntax Basics
|
Data Assignments in C++ Statements:
We've now bootstrapped enough background information in order to examine data assignment and variables in C++ statements, and to compile small programs to explore C++ data better.
C and C++ are know as compiled typed languages. What this means is that the C source code itself does not directly run a program, unlike scripting languages like Perl, Python, Rudy, Bourn Shell Scripting and such. The program has to be compiled, linked with standard library binary libraries, and outputted into a binary file that the machine and operating system can run.
The TYPED aspect of C++ means that all variables have to be defined as some specific data type, one of the data types such as we discussed earlier (such as int, char, short, long, double, float). Functions and Methods are data and have a type. Its a code type. But for the purposes of Syntax they are typed according to the kind of data that they return.
When we create variables in C and C++ they have to be typed. In addition, there is 3 phases in variable creation, which can be either in separate statements of combined into one statement. The three phases are:
Declaration Definition (needed for functions and methods) Initialization
We declare a variable using the name and its type, its symbol and is initializing it, we include an assignment of data. For example:
int i;
declares a variable i which is of type int, and most usually defines it.
float j;
declares a variable j of type float, as it is defined specifically to your architecture and environment. We can then assign an integer to i and a float to j using the following syntax.
#include using namespace std;
int i; float j; //Declarations of variables
int main(int argv, char * argc[]) {
i = 10; j = 5.6;
return 0; }
Now here is the catch. WHEN WE DECLARE A VARIABLE in an ordinary C++ coding file, one that has the function main within it, placing the variable in the global namespace above or outside of the "main" function, then we are defining and declaring the variable and the variable exists globally for your program, from the point that the variable is declare on to the end of the file, and all the other functions and code that you bring into your program from that point forward. However, it is normal for real programming for one to use at least three files for your program. Your main C++ code file, your library source file which is used to create reusable objects in your code base, and then finally, in HEADER files, the .h files that are imported with
#ifndef MYOWNLIB #include "myownlibrary" #define MYOWNLIB #endif
statements.
However, it is a programming error to ***define*** variables within your headers. It can confuse the linker, because then they compiler will make space for all the defined variables in every object that uses the header file in your program, and all those objects may not agree as to their values later on. So you want to declare it, so that everyone knows what the common type and symbol is, but you don't want to define it, so that the linker doesn't come along and fill in all those definitions, possibly incorrectly, and separately if not independently, in all the objects of your complex program. In order to allow for the declaration without the definition of a variable in the header file, you must use the "extern" keyword in header files to create proper global scope of variables.
This is not true with functions or classes, which we will see later. They have implicit "extern"s.
To beat a dead horse, since this is one of the most common areas of programming bugs, understand this:
Where we declare a variable is important and determines where your variable is viewable in your program. Complex programs have so many variables in them that it is critical to try to restrict their access and usage to the smallest subset of need as practical. This is accomplished through two mechanisms, Scope and Namespace. We've seen namespace already with the using directive
using namespace std;
imports into our section of programming all the reserved symbols of the standard namespace. Scope, on the other hand, restricts variable access to specific blocks of our programs, not just the symbols. When we declare our variables outside of main, as we did in the program file1.cc, then the scope is considered to be global and the variables i and j are available throughout our program.
Scope is a bit more complex We've looked at global scope already and we will run up against it time and again. According to Lippman there are three kinds of Scope: local, namespace and class. Scope in C++ is fairly complicated, and not well described in the standard texts. Anywhere you have a block you can declare and define variables, and they are local to that block. This is particularly important in functions. A block is a group of statements which are surrounded by curly braces. Like the unix shell, a block inherits the variables in its scope from the name space it is called from, including blocks that wrap it. So you can have blocks within blocks. If you declare a variable already in play, it creates a new temporary version of that variable. Variables in functions, while they might be declared outside the function, and defined within a function, would be created by the compiler, but don't become active until they are called. Each function creates a stack of runtime processes. The top most process, ignoring threading and forked processes for the moment, needs to run its course and end prior to calling function can continue. Since everything begins with a function called main, a runtime stack might look like this, each platter on the stack having its own unique environment and variables scoped within, excluding global variables which they can affect and see.
function2 | function1-------| | main ---|
In this scenario, when function2 finishes, all of its local variables die and no long exist in runtime, and control returns back to function1. function1 can then call function3, and add new plates to the runtime stack. We will explore this more when we look at functions and methods.
Now lets return to the representation of data in C++ and look at programming examples.
I will now increasingly be showing example code on the NYLXS website because of nice HTML tool in VIM that can show code in syntax color. See http://www.nylxs.com/docs/workshops/?C=M;O=D for coding examples as mentions here.
We can start by building a nice example program, which will have 4 working files, a header file, a main programming file, a library programming file and a make file. There are easier ways of showing these data type examples, but I feel it is important that new C and C++ programmer become comfortable and familiar with multiple file programming projects, especially for C++. Also, I might mention a tip or two with VIM and GVIM, in order to outline more general GNU tool sets to help the C and C++ coder. There are also, no doubt, likewise examples in EMACS and other editors, but I'm not going to mention them, and I really don't want an editor war to break out in the workshop.
|
|