Getting Started: Create a directory for this lab. You can either download the example programs to your directory from the Web as you go through this (go to the course web page at http://cs.roanoke.edu/Spring2009/CPSC270A to get this web page) OR just copy them all to your directory using cp as follows (assuming you are in your directory for this lab):
cp ~cpsc/public_html/Spring2009/CPSC270A/lab1/*.java .
cp ~cpsc/public_html/Spring2009/CPSC270A/lab1/*.cc .
int main()
or
int main (int argc, char * argv[])
Note that main returns an int. Usually we return 0 but this can be used to return status info to the operating system (0 means all is well!).
#include <iostream>
using namespace std;
cout << "Average delay: " << delay << endl;
The endl represents the new line (the escape sequence \n may also be used).
cin >> num;
The data read in becomes the value of the variable num.
g++ myProg.cc
This creates an executable in a file named a.out. To execute
the program you just type the pathname of the file (you must include
the dot for your current directory):
./a.out
g++ myProg.cc -o runP
g++ -c myProg.cc
This creates a file named myProg.o. Now link (in this case
with the library functions) and create the executable in runP:
g++ myProg.o -o runP
if (n = 0)
...
What would happen in Java if you had this in your program? How would
it be interpreted in C++?
Exercise: See if you were correct by examining the behavior of JavaIf.java and CPlusIf.cc.
const int DOZEN = 12;
This is done inside a function.
#define DOZEN 12
This is done outside of a function (before main or, later when we
use header files, in the header file).
const int MAX_SIZE = 10;
int x[MAX_SIZE];
This declaration allocates space for 10 ints. The space is allocated
at compile-time and cannot be changed while the program is running.
Later we will see a way to allocate space at run-time - dynamic
rather than static allocation. C++ allows the programmer to be
careless with array subscripts.
Exercise: The programs JavaArrays.java and CPlusArrays.cc use arrays.
OBSERVE: Note the formal parameter list for the function does NOT give the size of the array. Its format is as follows:
int sum (int size, int a[])
Exercise:First study the program ArrayFunctions.cc, then compile and run it. Then add a function that doubles every other element in the array starting at the first one. Note that this function will have void return type. In main add a statement that prints the new array (or add a function to print an array then call it) after the current statement that prints the original sum, the call the sum function to print the new sum.
Exercise:
Node myNode;
would declare myNode to be a reference (currently null) to a Node
object. The actual object would be created using the new operator.
In C++, the above declaration would create the actual Node (no
new would be needed).
To declare a reference (pointer) to an object we use *. The following declaration declares myNodePtr as a pointer to a Node:
Node * myNodePtr;
The actual node would be constructed using the new operator (details
later!).
The & operator gives the address of a variable. Some examples:
myNodePtr = &myNode;
int * numPtr;
int num = 5;
cout << &num << " an address!"; //prints the address of num
*numPtr = 13; // *numPtr is the contents of the location pointed
// to by numPtr - * is "dereferencing" numPtr
The program PointerFun.cc illustrates some of the properties of the & ("address of") operator and the * operator (when used to declare and when used to dereference a pointer variable). Study it then compile and run it.
To pass by reference in C++ your parameter must be a reference (a pointer). So, for example if you want to pass a Node object to a function so it can change the object you need to pass a pointer to the object - thus the declaration (signature) of the function would be
returnType functionName (Node * theNode);
or
returnType functionName (Node *);
Note: It is standard practice to just list the type of each parameter
in the declaration; it isn't necessary at that point to have a formal
parameter name.
The call of the function would need to send a pointer to a Node as the actual parameter. Two examples are:
Node * nodePtr;
Node actualNode;
...
... functionName(nodePtr) ....
...
... functionName(&actualNode)
The & operator gives the address of an object so &actualNode is a
pointer to (or reference for) the object actualNode.
Exercise: Write a C++ program that reads in three integers and arranges and prints them in sorted order (ascending). Your program must define and use a function swap that takes two integer parameters and swaps them (so, if a = 20 and b = 5, the call swap(a, b) will result in a = 5 and b = 20).