\documentstyle[cprog]{simslide}
\tallslides
\uppercorners{Data Structures in C++}{\thepage}
\lowercorners{Language Fundamentals}{Chapter 1}
\begin{document}

\slide{}

{\Huge \bf
\begin{center}
$\;$ \\ $\;$ \\
Data Structures \\
in C++ \\ $\;$ \\
Chapter 1 \\ $\;$ \\
Tim Budd \\ $\;$ \\
Oregon State University \\
Corvallis, Oregon \\
USA \\
\end{center}
}

\slide{Language Features}

The purpose of this chapter is to {\em quickly} review all those
features of C++ that you should {\em already} have encountered and be
familiar with.

Even if you have learned programming in another language (Pascal, for example)
you should be able to quickly get up to speed with the features described
here.

\slide{Comments}

There are two forms of comments in C++

\begin{cprog}

	// from slashes to end of line

	/* 
		comments that span
		multiple lines
	*/

\end{cprog}

Comments should be used extensively for documentation.

\slide{Constants}

There are various types of constants:

\begin{itemize}
\item
integer -- 1, 12, $-37$
\item
octal integers -- 014
\item
hexadecimal integers 0XFF 0XC
\item
floating point  -- 3.14159 2.7e14
\item
character -- 'a'  '\verb+\+n' 
\item
string -- "abc"
\end{itemize}

Suffixes can be applied to integer constants (U for unsigned, L for long)

Several other special backslash characters

\slide{Variables, Types, Values and Declarations}

A {\em variable} is a named location that can hold {\em values} of 
a certain {\em type}.

Variables are created using a {\em declaration statement}, which 
also describes the associated type.

\begin{cprog}

	int a, b, c // declare three integer variables

\end{cprog}

Declarations can be combined with initialization:

\begin{cprog}

	double pi = 3.1415926;

\end{cprog}

\slide{Fundamental Data Types}

The fundamental data types:

\begin{itemize}
\item
integer -- {\tt int}
\item
floating point -- {\tt double}, {\tt float}
\item
character -- {\tt char}
\end{itemize}

Modifiers that can be used with funamental types

\begin{itemize}
\item
{\tt signed, unsigned} -- positive and negative, or positive only
\item
{\tt short, long} -- (possibly) shorter or longer than standard
\end{itemize}

\slide{More Data Types}

Boolean ({\tt bool}) variables are true/false.

Enumerated values are defined by providing an explicit range

\begin{cprog}

	enum months {January, February, March, April, May, June, July,
		August, September, October, November, December};


	months workingMonth, vacationMonth;
	months summerMonth = August;

\end{cprog}

\slide{Variables and Assignment}

Variables are modified by assignment statments, which assign an expression to
a variable.

\begin{cprog}

	double f, c;	// Fahrenheit and Celsius temperature

	c = 43;
	f = (c * 9.0) / 5.0 + 32;

\end{cprog}

Binary operators can be combined with assignment:

\begin{cprog}

	i += 5;

\end{cprog}\noindent
has the same meaning as the statement:
\begin{cprog}

	i = i + 5;

\end{cprog}

Other short-hand notations:
\begin{cprog}

	i++

\end{cprog}\noindent
has the effect of incrementing the variable {\tt i} by one.

\slide{Lots of Operators}

\begin{center}
\begin{tabular}{|l|c|}
\hline
\multicolumn{2}{|c|}{Unary Operators} \\
\hline
increment, decrement & i$++$, $++$i, i$--$, $--$i \\
negation & $-$ i \\
bit-wise inverse & $\sim$ i \\
\hline
\multicolumn{2}{|c|}{Arithmetic Operations} \\
\hline
addition, subtraction & a$+$b a$-$b \\
multiplication, division & a$*$b a$/$b \\
remainder after division & a \% b \\
\hline
\multicolumn{2}{|c|}{Shift Operations (also stream I/O)} \\
\hline
left shift (also stream output) & a $<<$ b \\
right shift (also stream input) & a $>>$ b \\
\hline
\multicolumn{2}{|c|}{Relational Operations} \\
\hline
less than, less than or equal & $<$ $<=$ \\
equal, not equal & $==$ $!=$ \\
greater than, greater than or equal & $>$ $>=$ \\
\hline
\multicolumn{2}{|c|}{Logical Operations} \\
\hline
and & x \&\& y \\
or & x $\mid\mid y$ \\
logical negation & $!$ i \\
\hline
\multicolumn{2}{|c|}{Miscellaneous Operations}\\
\hline
function call & f(a,b,c) \\
conditional expression & c ? a : b \\ 
\hline
\end{tabular}
\end{center}

\slide{Stream I/O}

The left and right shift operators are given different meanings when used
with {\em stream} values.  The most common stream is associated with
``console input and output''.

\begin{cprog}

	cout << "the Fahrenheit equivalent of " << c << 
		" is " << f << "\n";

	cin >> c; // get a new value of c
	cout << "the Fahrenheit equivalent of " << c << 
		" is " << f << "\n";

\end{cprog}

Operator $>>$ works by side effect, changing the right hand expression.
Result can be converted into a boolean, to test if input was successful.

\begin{cprog}

	int sum = 0;
	int value;
	while (cin >> value) {
		sum += value;
		}
	cout << "sum is " << sum << "\n";

\end{cprog}

\slide{Pointers}

A pointer is a variable that maintains the address of another location in 
memory.

\setlength{\unitlength}{5mm}
\begin{center}
\begin{picture}(12,3)
\put(0,1){\framebox(3,1){}}
\put(5,0){\framebox(7,1){}}
\put(2.5,1.5){\vector(3,-1){3}}
\end{picture}
\end{center}

Pointers can be used in the following ways:

\begin{itemize}
\item
Pointers can be subscripted, (works best if they point to an array, but
isn't checked).
\item
Pointers can be dereferenced, using the $*$ operator.
\item
Can combine dereference and field access, using $->$ operator.
\item
Can use addition, {\tt p$+$i} is address of {\tt p[i]}.
\end{itemize}

\slide{Conditional Statements}

Normal sequential control can be modified using a conditional statment:

\begin{cprog}

	month aMonth;
		...
	if ((aMonth >= June) && (aMonth <= August))
		isSummer = true;
	else
		isSummer = false;

\end{cprog}

Else part is optional.

\slide{Switch Statements}

Switch statements can select one of many alternatives:

\begin{cprog}

	switch (aMonth) {
		case January: 
			highTemp = 20; 
			lowTemp = 0;
			break;
		case February:
			highTemp = 30;
			lowTemp = 10;
			break;
		...
		case July:
			highTemp = 120;
			lowTemp = 50;
			break;
		default:
			highTemp = 60;
			lowTemp = 20;
	};

\end{cprog}

\slide{Loops}

Loops are used to execute statements repeatedly until a condition is
satisfied.

\begin{cprog}

	c = 0;
	while (c <= 100) {
		cout << "Celsius " << c << " is Fahrenheit " <<
			((9.0 * c) / 5.0 + 32) << "\n";
		c += 10;
		}

\end{cprog}

\slide{For statements}

For statements combine in one statement initialization, termination test,
and update.

\begin{cprog}

	for (c = 0; c <= 100; c += 10) {
		cout << "Celsius " << c << " is Fahrenheit " <<
			((9.0 * c) / 5.0 + 32) << "\n";
		}

\end{cprog}

Declaration of new variables can be combined with loop.

\begin{cprog}

	for (int i = 0; i < 12; i++) {
		cout << "i: " << i << " i squared " << i*i << "\n";
		}

\end{cprog}

\slide{Array}

An array is a fixed sized collection of similarly-typed values.
Array elements are accessed using subscripts, range is zero to one
less than array size.

\begin{cprog}

		// declare an array of twelve integer values
	int Temperatures[12];
		// now assign all values
	Temperatures[0] = 0;
	Temperatures[1] = 10;
	Temperatures[2] = Temperatures[1] + 15;
	...

\end{cprog}

Arrays can be initialized:

\begin{cprog}

	string MonthNames[12] = {"January", "February", 
		"March", "April", "may", "June", 
		"July", "August", "September", "October", 
		"November", "December" };

\end{cprog}

\slide{Multidimensional Arrays}

Arrays of more than one dimension can be created by giving the extent
along each axis.

\begin{cprog}

	double matrix[10][20];

\end{cprog}

Creates a double precision array of ten rows and twenty columns.

Elements accessed by giving subscript for each dimension:

\begin{cprog}

	matrix [i][j] = matrix [i-1][j+1] + 1;

\end{cprog}

\slide{Arrays and Pointers}

Close relationship between arrays and pointers.

Array name is in fact treated just like a pointer.

Pointers can be subscripted, as if they were arrays (even if they aren't!)

{\tt MonthNames + 3} is legal, means address of {\tt MonthNames[3]}.

\slide{Arrays as Arguments}

When used as an argument, size need not be specified:

\begin{cprog}

int arraySum (int values[ ], int n)
	// compute sum of array values[0] .. values[n-1]
{
	int result = 0;
	for (int i = 0; i < n; i++) {
		result += values[i]
		}
	return result;
}

\end{cprog}

\slide{Structures}

A structure is a collection of fields, which need not have the same type.

\begin{cprog}

	struct person {
		string name;
		int age;
		enum {male, female} sex;
		};

\end{cprog}

Fields are accessed using dot notation.

\begin{cprog}

person employee;
employee.name = "sam smith";
employee.age++;
if (employee.sex == male)
	...

\end{cprog}

We actually won't use structures, will use more general mechanism called
{\em class} (Described in chapter 2).

\slide{Functions}

Functions encapsulate a set of actions, so that later we can refer to 
the sequence of actions by name alone:

\begin{cprog}

	int Fahrenheit(int cTemp)
	{
		return (cTemp * 9.0) / 5.0 + 32;
	}

\end{cprog}

Parts:
\begin{itemize}
\item
Header -- with return type, name, and arguments
\item
Body -- with statements to execute.  Can have {\em return} statement to
end execution.
\end{itemize}

Return type can be {\tt void} -- no value.

A function {\em prototype} is a declaration but not a definition, just gives
name, arguments and return type.

\begin{cprog}

		// prototype for Fahrenheit -- definition occurs later
	int Fahrenheit (int);

\end{cprog}

\slide{Local Variables}

Variables within a function come into existance when the function is
entered, disappear when the function exits.   Execute in stack-like
fashion.  Assume function A calls function B which calls function C --
can imagine variables as follows:

\setlength{\unitlength}{2mm}
\begin{center}
\begin{picture}(30,15)
\put(0,0){\framebox(30,5){local variables for A}}
\put(0,5){\framebox(30,5){local variables for B}}
\put(0,10){\framebox(30,5){local variables for C}}
\end{picture}
\end{center}

Will eventually encounter {\em recursive} functions, functions that can
call themselves.  Imagine B is recursive, and has called itself once
before calling C, can envision the following:

\setlength{\unitlength}{2mm}
\begin{center}
\begin{picture}(30,20)
\put(0,0){\framebox(30,5){local variables for A}}
\put(0,5){\framebox(30,5){local variables for B}}
\put(0,10){\framebox(30,5){local variables for B}}
\put(0,15){\framebox(30,5){local variables for C}}
\end{picture}
\end{center}

\slide{The Main Event}

A program must always include a procedure named {\tt main}, which is the
starting point for execution.

\begin{cprog}

# include <iostream>

void main() {
	// program to write table of squares 
	cout << "Table of Squares\n";

	for (int i = 0; i < 12; i++) {
		cout << "i: " << i << " i squared " << i*i << "\n";
		}
}

\end{cprog}

\slide{Include Files}

Many data structures require one to define an include file before they
can be processed.

\begin{center}
\begin{tabular}{| l | l |}
\hline
{\em purpose} & {\em name} \\
\hline
stream input/output & iostream \\
math functions & math.h \\
complex numbers & complex \\
Boolean values & bool.h \\
generic algorithms & algorithm \\
\hline
\end{tabular}
\end{center}

\end{document}
