<function declarations>
void main ()
{
<statements>
}
The include statement is used to literally include the contents of
other files into the current file at compile time. The compiler
searches
for files matching the include specification. There are two forms:
#include <iostream.h>
In this case the angle brackets <> indicate that it is a system file,
and
should be sought accordingly with the other system files.
#include "myfile.h"
In this case the quotes indicate it is a user defined file and should
be
searched for in the user's local file space.
The .h specification is a convention to indicate that the file is a
header file (which
is why the includes come at the top of your program file). It
is only a convention.
void main ()
{
cout << "hello world";
}
In this example we have two include files. iostream.h
is a standard include which
is used when input/output operations are needed. In this case
cout is an operation
which sends output to the console. The second file myfile.h
is a user file.
For the purposes of the example let's assume that the contents of myfile.h
are the following:
// Just a collection of comments
// The double slash indicates a comment in c++
At compile time the compiler literally dumps the contents of the two
files into the
corresponding locations in the original code. Leaving something
like:
<contents of iostream.h>
// Just a collection of comments
// The double slash indicates a comment in c++
void main ()
{
cout << "hello world";
}
I haven't shown the contents of iostream.h for brevity's sake.
There are no procedures in C++, instead there is a flexible form of
function calling.
Let's say we want to write a function which computes 5 factorial.
Here is a function
which would do the trick.
int FiveFactorial()
{
int i, fact;
fact = 1;
for (i = 1; i <= 5; i++)
{
fact = fact * i;
}
return fact;
}
This is a silly program, but it demonstrates several key constructs.
The int declaration
before the function name defines the type of information that the function
will return.
Since factorial is an integer, we use the integer type. The value
is actually returned
in the return statement (which causes an exit from the function).
The variables
i and fact are defined locally to the function.
The loop structure here is a for
loop, it has the general form for (initialize; condition; increment)
The program could
just have easily been written:
int FiveFactorial()
{
int i, fact;
fact = 1;
for (i = 5; i >= 1; i--)
{
fact = fact * i;
}
return fact;
}
In the second form the loop counts down from 5, instead of up from 1.
The ++ and -- are
auto-increment and decrement respectively. Notice that in C++
the assignment statement
is = and not := as in Pascal. To check if two things
are equal use ==.
A more general program calculates any factorial (within limits).
int Factorial(int factor)
{
int i, fact;
fact = 1;
for (i = factor; i >= 1; i--)
{
fact = fact * i;
}
return fact;
}
Here we have defined a general function which takes an integer as an
input and
return an integer as an output.
#include <iostream.h>
#define ARRAYSIZE 6
main()
{
int factArray[ARRAYSIZE];
for (int i = 0; i < ARRAYSIZE; i++)
{
factArray[i] = Factorial(i);
}
for (int j = 0; j < ARRAYSIZE; j++)
{
cout << factArray[j] << endl;
}
}
This program introduces several new constructs. The #define
statement is used mostly
to define constants within a program. This way we could have
a brand new program with
a different size, all by simply changing the initial define,
rather than needing to change
every reference to size within the program.
The array is intially defined in the first line of the main routine.
The int denotes that
the elements of the array are integers. The fact that it is an
array is denoted by the
brackets [ ]. The statement tells the compiler how large an array
we need, in
this case 6 integers. Please note that in C++ arrays are 0-based not 1-based as in Pascal.
In the cout statement we string multiple things together by using
multiple
<< operators. The endl denotes that a carriage
return should be output.
Sometimes we don't know ahead of time how big an array we need. C++
provides
the opportunity to allocate the memory dynamically with the new
and delete statements.
#include <iostream.h>
main()
{
int *factArray;
int arraySize;
cout << "Enter the size of the array.";
cin >> arraySize;
factArray = new int [ arraySize ];
for (int i = 0; i < arraySize; i++)
{
factArray[i] = Factorial(i);
}
for (int j = 0; j < arraySize; j++)
{
cout << factArray[j] << endl;
}
delete [ ] factArray;
}
In the first line of main we define factArray as a pointer variable.
What this means is
that its contents are the location of some storage, rather than an
actual value. When
initially defined its value is NULL. The new state
gives it a new value, which is
the location of the storage just allocated. Later, when the delete
statement is
executed, the value is reset to NULL and the storage is reclaimed.
new and delete
are most useful in highly storage intensive applications. Another
new item in this
program is the use of the cin statement to read in data from
the terminal.
#include <iostream.h>
// This one doesn't work
void
SwapWrong( int A, int B )
{
int Tmp = A;
A = B;
B = Tmp;
}
// C style -- using pointers
void
SwapPtr( int *A, int *B)
{
int Tmp = *A;
*A = *B;
*B = Tmp;
}
// C++ style -- using references
void
SwapRef( int & A, int & B )
{
int Tmp = A;
A = B;
B = Tmp;
}
The first function will not work. C++ passes by value to functions,
so it just copies
the values of the integers when the function is invoked. In the
second function, the
values that are copied are pointer values. With the use of these
values, the locations
that they point to can be changed. As the comments say, this
is C coding style. It
can be confusing and is generally best avoided. The third function
uses C++ style,
and is simpler to understand. In this function A and B
are reference variables.
With reference variables C++ automatically keeps track of the pointer
information
for you. As you can see by the code, it is much a much cleaner
style.
You can test the functions with the following program
main()
{
int X = 5;
int Y = 7;
SwapWrong( X, Y );
cout << "X=" << X << " Y="
<< Y << endl;
SwapPtr( &X, &Y );
cout << "X=" << X << " Y="
<< Y << endl;
SwapRef( X, Y );
cout << "X=" << X << " Y="
<< Y << endl;
return 0;
}