# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by Tim Budd <budd@ada> on Thu Jun 24 12:55:35 1993
#
# This archive contains:
#	asgn1.tex	asgn2.tex	asgn3.tex	asgn4.tex	
#	asgn5.tex	asgn6.tex	asgn7.tex	
#

LANG=""; export LANG
PATH=/bin:/usr/bin:$PATH; export PATH

echo x - asgn1.tex
cat >asgn1.tex <<'@EOF'
% written by Tim Budd, Oregon State University
% Public domain, may be used as a basis for programming assignments

\documentstyle[cprog]{article}
\begin{document}
\section*{Course in OOP \\ Programming Assignment 1}

The purpose of this first assignment is simply to familiarize the student
with the use of the Turbo C++ Integrated Development Environment running 
under Microsoft Windows.  This assignment will not be graded.   Those 
students who have previous experience using Turbo C++ can move immediately
to assignment number 2.

\section*{Turbo C++ Tutorial}

\begin{enumerate}
\item
Start the Turbo C++ IDE.

The Turbo C++ Integrated Development Enivornment, or IDE, is a Windowss
application that provides facilities for writing, editing, compiling, linking,
running and debugging C++ programs.

[ details of how to get to the icon for Borland C++ for Windows must be
provided here ].

\item
Open a new project.

A project file is a special file used by Turbo C++ to store all the information
needed to build a program.  Each program will have its own project file.
Whenever you start working on a new program, you will open a new project file.

\begin{enumerate}
\item
Select the Project menu.
\item
Choose the Open project item.  The Open project file dialog appears on the
screen.
\item
Insert your diskette into the appropriate drive.
\item
Select the right drive by double-clicking the drive name in the 
Directories list box.
\item
Click the mouse in the File Name input box and type hello.  The filename
extension .prj will automatically be appended to the name.
\item
Click the OK button.
\end{enumerate}

\item
Open a new program source file.

The C++ code you write will be stored in a DOS text file.  DOS places
certain restrictions on the format of file names.
\begin{itemize}
\item
A filename can be from 1 to 8 characters long.
\item
A file can have an optional extension of 1 to 3 characters which are separated
from the filename by a period.  The extension typically identifies the
purpose of the file.  Applications often require specific extensions.
\item
A filename or extension cannot contain a space, tab or newline.
\item
A filename or extension cannot contain certain specific characters,
\verb@."/\[]:*<>|+=;,?@.
\end{itemize}

A project can consist of several program files.   Turbo C++ opens an edit
window that provides the standard editing facilities such as cutting, copying,
pasting, etcetera.
\begin{enumerate}
\item
Select the File menu.
\item
Choose the New item.  An edit window titled noname00.cpp appears on the desktop.
\item
Type in the C++ source program shown below.
\begin{cprog}

# include <iostream.h>

void main()
{
	cout << "hello, world\n";
}

\end{cprog}
\end{enumerate}
\item
Save the C++ source program.

After typing the entire program into the edit window, the program must be
saved to a file.
\begin{enumerate}
\item
Select the File menu.
\item
Choose the Save item.  The Save File dialog appears on the screen.
\item
Type hello.  The filename extension .cpp will automatically be appended to
the name.
\item
Click the OK button.
\end{enumerate}

\item
Add the C++ source file to the project.

Each file that you create must be added to the project.
\begin{enumerate}
\item
Activate the Project: hello window by clicking the mouse anywhere inside the
window.
\item
Select the Project menu.
\item
Choose Add item.  The Add to Project List dialog is displayed.
\item
Double-click hello.cpp in the Files list box.  The file hello.cpp is then 
added to the Project: hello window.
\item
Click the Done button.
\end{enumerate}

\item
Compile and link.

All source files must be compiled and then linked along with libaries to 
produce an executable program.  The Make command in Turbo C++ provides this
service.
\begin{enumerate}
\item
Select the Compile menu.
\item
Choose the Make item.  The Compile Status dialog appears, displaying 
various information during compilation and linking.
\item
If the source code was entered correctly, the Status box will display
Status: Sucess.
\item
Click the OK button.   The Message window displays the following messages:

\begin{cprog}

Compiling HELLO.CPP
Linking HELLO.EXE
Linkier Warning: No module definition file specified: using defaults

\end{cprog}
You can ignore the warning.
\end{enumerate}

\item
Run the application

Turbo C++ allows you to run the newly created program without exiting the IDE.
\begin{enumerate}
\item
Select the Run menu.
\item
Choose the Run item.  A window titled (Inactive A: Hello.EXE) is displayed
with the words ``hellpo, world'' in the workspace.
\end{enumerate}

\item
Quit the application.

Even though this simple application does not allow any user interaction,
Windows still consideres it a running application.   The application must
be terminated.

\begin{enumerate}
\item
Select the Control menu box located in the upper left corner of the 
application's window.
\item
Choose the Close item.
\end{enumerate}

\item
Close the project.

When work is finished on a project, the project must be closed.
\begin{enumerate}
\item
Select the Project menu.
\item
Choose the Close project item.
\end{enumerate}

\item
Exit Turbo C++.

When you are finished with Turbo C++, you terminate the IDE application.
\begin{enumerate}
\item
Select the File menu.
\item
Choose the Exit item.
\end{enumerate}
\end{enumerate}

\end{document}

@EOF

chmod 600 asgn1.tex

echo x - asgn2.tex
cat >asgn2.tex <<'@EOF'
% written by Tim Budd, Oregon State University
% Public domain, may be used as a basis for programming assignments

\documentstyle[cprog]{article}
\begin{document}
\section*{Course in OOP \\ Programming Assignment 2}

The first assignment was a simple tutorial designed to failarize you with the
Turbo C++ Integrated Development Environment rnning under Microsoft Windows.
The ``hello, world'' program that you typed in is an example of a simple
conventional C++ program.

Programs written to run under Graphical User Interface environments,
such as Microsoft Windows, X-Windows, or the Macintosh, are constructed very
differently from conventional programs.  The main difference lies in the
fact that in a GUI environment the user has more control over such
things as the order in which certain operations are performed and the
actual appearance of a running program.  Key goals of Graphical User
Interfaces are to:
\begin{itemize}
\item
Provide a common interface that can be shared by all programs in order to
reduce both the time required for programmers to develop new applications
and the time required by users to learn new applications.
\item
provide more flexibility for the user, allowing him or her to utilize the
computer in a more natural way.
\end{itemize}

The way this works is that an application receives a message from the GUI
that some ``event'' has occurred.  Examples of events include the mouse
button has been pressed, a window has been resized, graphics have been written
to a window, and a window has been closed.  Programs are designed around
a loop that recieves a message, responds by performing some operation,
and then waits for another message.

As a result of this increased functionality, programs written to run under
Windows are considerably more complex than conventional programs.  For
example, the program you developed in the first assignment,
which displayed ``hello, world'' on
the screen, was 5 lines long.  an equivalent Windows program is over 60
lines long.  Application framewoks, also called Software Frameworks, 
provide a means of managing this added complexity.  They provide the basic
services needed by an application, such as initializing the user interface
and displaying a window.  The programmer can then specialize the behavior for 
a particular application.

In this assignment you will type in a program that uses the Little 
Application Framework, a simple framework that you will be using throughout 
the term to write some interesting and hopefully useful applications.

\begin{enumerate}
\item
Copy the files laf.h and laf.cpp to your diskette.
\item
Start Turbo C++.
\item
Open a new project named pen.prj.  Make sure the project is opened on
your diskette.
\item
Open a new program source file and enter the program shown below.

\begin{cprog}

//====================================
// Name:
// File:   penAppl.cpp
// Description: A program that uses a pen to draw lines
//   using the Little Application Framework.
//   by clicking the mouse in the window the user draws lines.
//===================================

# include "laf.h"

// Customized Application

class penApplication : public application {
public:
	penApplication(char *, char *, HANDLE, HANDLE);
	void mouseDown(int, int);
	void paint();
};


penApplication::penApplication(char * apName, char * apTitle,
	HANDLE thisInst, HANDLE prevInst)
	: application(apName, apTitle, thisInst, prevInst)
{
	// no action, just pass arguments to superclass constructor
}

	// global variables for pen locations
int oldx = 0;
int oldy = 0
int newx = 0;
int newy = 0;

void penApplication::mouseDown(int x, int y)
{
	// set location to move to
	newx = x;
	newy = y;

	// then update the screen
	update();
}

void penApplication::paint()
{
	// draw a line
	line(oldx, oldy, newx, newy);

	// update old locations
	oldx = newx;
	oldy = newy;
}

// Windows Program Entry Point

int PASCAL WinMain(HANDLE thisInst, HANDLE prevInst, LPSTR, int)
{
	penApplication theApp("PENS", "Little Application Framework",
		thisInst, prevInst);

	return theApp.run();
}

\end{cprog}

\item
Save the C++ source program as penApp.cpp.

\item
Add the source files panApp.cpp and laf.cpp to the project.

\item
Compile, link and Run the application, answering the questions in the
later section of this document.

\item
When you are finished, quit the application, close the project, and exit 
Turbo C++.
\end{enumerate}

\section*{Brief Description of the Program}

The class penApplication is defined as a subclass of application and is
used to customize or specialize the behavior of the basic Framework.
(Don't worry if you really don't understand te concept of a subclass at
this point, you can simply copy the class description as I have presented
it.  Later assignments will be going into the ideas of inheritance and 
subclassing in 
more detail).   This program specializes the default behavior of the
framework in two activities:
\begin{enumerate}
\item
How the application will respond when the mouse button is pressed, and
\item
How the application will respond when Windows requests that the window
should be repainted (redrawn).
\end{enumerate}

When the mouse button is pressed, the location of the mouse press is saved
in a pair of global variables.  A restriction of Microsoft windows is
that graphics activities can only be performed during repaint (or redraw)
events, and thus the mouse down function is restricted to only saving
information.  By executing the function ``update'', however, the mouse
down function forces the scheduling of an update, or painting event.

The questions in the following section are designed to help you understand
what actions are taking place in the framework.

\section*{Questions on the program}

\begin{enumerate}
\item
When you first start the application, what is in the window?
$\vspace{1in}$
\item
When you click the mouse once inside the window, what happens?
$\vspace{1in}$
\item
When you click the mouse once again, what happens?
$\vspace{1in}$
\item
In which function is the command to do the actual drawing of the line 
contained?
$\vspace{1in}$
\item
Edit the program and change the call on ``update'' in the mouse down
function to a call on 
``clearAndUpdate'', then run the program again.  How is the behavior
different?
$\vspace{1in}$
\item
Experiment with changing some of the characteristics of the Pen, by adding
the following command to the mouse down function:
\begin{cprog}

setPen(blue, dashedLine, 2);

\end{cprog}
What happens now when you run the program?
$\vspace{1in}$
\item
You might want to experiment more with changing pen colors, styles, or
widths.  Valid colors are black, blue, green, cyan, red, magenta, brown,
gray, white, brightBlue, rightGreen, brightCyan, brightRed, brightMagenta,
brightYellow, and brightGray.  Valid pen styles are solidLine, dashedLine,
dottedLine, and nullLine.
\end{enumerate}

\end{document}
@EOF

chmod 600 asgn2.tex

echo x - asgn3.tex
cat >asgn3.tex <<'@EOF'
% written by Tim Budd, Oregon State University
% Public domain, may be used as a basis for programming assignments

\documentstyle[cprog]{article}
\begin{document}
\section*{Course in OOP \\ Programming Assignment 3}

In this assignment we will delve further into the concept of classes, 
constructors, and dynamic memory allocation.  Classes were introduced
without explanation in the previous assignment, in this assignment we
will explain the concept in more detail.

\subsection*{Creating the Application Code}

Create a new project called rectangle.prj, and along with the application
framework (the file laf.cpp) include in the project a file named rectApp.cpp
containing the following:

\begin{cprog}

//====================================
// Name:
// File:   rectApp.cpp
// Description: A program that defines classes to draw 
//	rectangles, uses the Little Application Framework
//   by clicking the mouse in the window the user draws rectangles.
//===================================

# include "laf.h"

// Customized Application

class rectangleApplication : public application {
public:
	rectangleApplication(char *, char *, HANDLE, HANDLE);
	void mouseDown(int, int);
	void paint();
};


rectangleApplication::rectangleApplication(char * apName, char * apTitle,
	HANDLE thisInst, HANDLE prevInst)
	: application(apName, apTitle, thisInst, prevInst)
{
	// no action, just pass arguments to superclass constructor
}

//
//	RECTANGLE CLASS DEFINITION GOES HERE
//

void rectangleApplication::mouseDown(int x, int y)
{
	// fill in later
}

void rectangleApplication::paint()
{
	// fill in later
}

// Windows Program Entry Point

int PASCAL WinMain(HANDLE thisInst, HANDLE prevInst, LPSTR, int)
{
	rectangleApplication theApp("PENS", "Little Application Framework",
		thisInst, prevInst);

	return theApp.run();
}

\end{cprog}

\subsection*{Creating a New Class}

The major part of this assignment will be involved in creating a new class
that represents a rectangle.  The major distinction between a class and
a structure (excluding the fact that classes can also use inheritance, which
we will discuss in more detail in the next assignment), is that a class
is a combination of data fields {\em and} behavior.

The data fields used by a rectangle can be defined in a number of different
ways.  We will use four integer values; two representing the x and y
position of the upper-left corner, and two representing the x and y 
position of the lower-right corner.  We might begin our description of a
class for rectangles by defining something that looks very much like
a structure, and contains just these fields:

\begin{cprog}

class rect {
	// data fields
	int upperLeftX;
	int upperLeftY;
	int lowerRightX;
	int lowerRightY;
};

\end{cprog}

Unlike a simple structure in other languages, a class in C++ can also have
functions, which provide behavior for the associated object.
What behaviors should a rectangle exhibit?  In this assignment, we will
have just three functions and a constructor.  
The first function, named moveTo, takes four 
integer arguments, and simply assigns the parameter values to the
various fields.  (The questions at the end of the assignment ask you to
write this function).
The second, named draw, is intended to draw the rectangle on the window.
To do this, it needs to know in {\em which} window to draw the rectangle.
(In the little application framework, of course, there is only one window.
However, in general when using Windows there might be many).  The
draw method therefore takes a pointer to a window, and using pen commands
draws the lines representing the rectangle.  

The following illustrates a portion of this function, you will provide the
remainder in answers to the questions at the end of this assignment.

\begin{cprog}

void rect::draw(window * theWindow)
{
	// first make sure the pen is correct
	theWindow->setPen(blue, solidLine, 1);

	// then draw the lines
	theWindow->line(upperLeftX, upperLeftY, lowerRightX, upperLeftY);
	...	// you put more code here
}

\end{cprog}

Let us explain this function step by step.
\begin{itemize}
\item
First, the function returns no type, that is, it returns a ``void''.
\item
The name of the function is ``the draw function associated with the class
named rectangle''.  This is written as the class name, a pair of colons,
and the function name.  You can think of this as being analogous to 
a persons first name and family name.
\item
The argument is a pointer to a parameter of type window.
\item
Within the function, operations on windows must be qualified by saying
on which window the operations should be executed.  In the previous assignment
we did not need to do this, since the window operations were executed 
directly from a method associated with a window, and the current window
was therefore assumed as the default object on which operations were to be
performed.  In the current case, however, the function is associated
with the class rectangle, not the class window, and thus window operations
must be explicitly named.
\end{itemize}

After moving and drawing, the third operation for rectangles
takes a pair of X, Y coordinates and returns true if
the values are within the bounds of the rectangle, and false if they are not.
Again, you are asked to write this function in the questions at the end of
this assignment.

A constructor is simply a function that is executed as part of the creation
of a new value.  Constructors are distinguished by the fact that the name
of the function is the same as the name of the class in which it appears.
Constructors can take any arguments needed to initialize the class.
In this assignment the constructor will take four integer arguments,
used to initialize the coordinate values.

All of these functions must be declared within the class definition.
We can do this as follows:

\begin{cprog}

class rect {
public:
	// constructor
	rect(int, int, int, int);

	// operations
	void moveTo(int, int, int, int);
	int includes(int, int);
	void draw(window *);

	// data fields
	int upperLeftX;
	int upperLeftY;
	int lowerRightX;
	int lowerRightY;
};

\end{cprog}

Note that the declaration in the class definition does not give names to
the argument values, only their types.

\subsection*{Doing the Assignment}

\begin{enumerate}
\item
Edit the rectangle application file and add the class declaration at
the location specified by the comment.  Following this add the
definitions for the various functions.
\item
The constructor for the class simply initializes the four data fields.
Notice that, unlike other functions, the constructor function does 
not have a return type.
\begin{cprog}

rect::rect(int a, int b, int c, int d)
{
	upperLeftX = a;
	upperLeftY = b;
	lowerRightX = c;
	lowerRightY = d;
}

\end{cprog}
\item
What does the implementation of the function named moveTo in the class
rect look like?  (write it here).
$\vspace{1in}$
\item
Write the complete definition of the function draw.
$\vspace{1in}$
\item
Write the complete definition of the function includes, which should take
a pair of coordinate values as argument and return a value of 1 if the
point is within the bounds of the rectangle, and the value of 0 if the 
argument is not.
Note that coordinate values begin in the upper left corner, and move
down and right.
$\vspace{1in}$
\item
Write the complete implementation of the constructor for rectangles.
$\vspace{1in}$
\item
Although we have now defined a new data-type, our application still does not
{\em do} anything.  We will make our application illustrate the use of
rectangles by making changes to the mouseDown and paint functions.
First, declare a global variable which is a {\em pointer} to a rectangle.
Do this by placing the following declaration in your file following the
code you entered above.
\begin{cprog}

rect * theRectangle = 0;

\end{cprog}
Note that the value of this pointer is initially zero (a null pointer).
\item
The mouse down procedure will do the following.  If the position of the mouse
is within the bounds of the current rectangle, then a new rectangle is
created with upper left corner at the mouse down position, and height and
width of size 50 and 20, respectively.  If the mouse down is not within
the current rectangle, then the mouseDown procedure does nothing.
This is written as follows:

\begin{cprog}

void rectangleApplication::mouseDown(int x, int y)
{
	if (theRectangle != 0) {
		if (theRectangle->includes(x, y)) {
			// remove the old rectangle
			delete theRectangle ;

			// create a new one
			theRectangle = new rect(x, y, x+50, y+20);
			}
		}
	else
		theRectangle = new rect(x, y, x+50, y+20);

	// then update the screen
	update();
}

\end{cprog}
The operator new allocates a new rectangle.  This rectangle is dynamic,
not tied to the execution of the mouse down function (as a local variable
would be).  Thus, it can exist until the next execution of the mouse down
function.

\item
The paint function simply draws the current rectangle.  To do this, it
needs a reference to the current window.  Withing a function associate
with a class (a so-called method), the object currently being manipulated
is always available as the value of the variable named ``this''.  

\begin{cprog}

void rectangleApplication::paint()
{
	if (theRectangle != 0)
		theRectangle->draw(this);
}

\end{cprog}

\item
Explain what happens as we execute the resulting application.
$\vspace{1in}$
\item
What happens if we change the call on update in the mouse down method to
be instead a call on clearAndUpdate?  How is the resulting application
different?
\end{enumerate}

\end{document}
@EOF

chmod 600 asgn3.tex

echo x - asgn4.tex
cat >asgn4.tex <<'@EOF'
% written by Tim Budd, Oregon State University
% Public domain, may be used as a basis for programming assignments

\documentstyle[cprog]{article}
\begin{document}
\section*{Course in OOP \\ Programming Assignment 4}

In this assignment we will further extend our study of classes in C++ with
a discussion of inheritance.  In this case, we will develop a new class
that represents a playing card, and which inherits some of its behavior from
the class rectangle we developed in assignment 3.  Along the way we will
explore the concept of a virtual method.

\subsection*{Creating the Application Code}

Copy the entire contents of the file rectApp.cpp into a file named
cardApp.cpp (you can do this by cutting as pasting).  Create a new project
named card.prj which includes this new file plus the little application
framework.

\subsection*{Creating a New Class Using Inheritance}

In this assignment we are creating a new class named ``card'' that is
a subclass of rectangle.  By a subclass, we mean that the class
card will inherit data fields and behavior from the class rectangle.
This means everything we have already described for rectangles, all the
data fields and the functions, are applicatable immediately and for free in
the class card.

To do this, the class description of card is written as follows:

\begin{cprog}

class card : public rectangle {
public:
	... 
};

\end{cprog}

How is a card different from a rectangle?  Well, in addition to having
a size, a card also has a suit and rank value.  We will use integers
1 to 4 for the suit, and 1 to 13 for the rank.  A card can also be face-up
or face down.  We will use 1 to represent face up, and 0 to represent
face-down.  Thus, there are three new data fields in a card.
A card also has a fixed size.  We will use two constant global variables
named CardWidth and CardHeight to hold these values.
A card also has a fixed size.  We will use two constant global variables
named CardWidth and CardHeight to hold these values.

Just as a rectangle needed a constructor, a card also needs a constructor
to set these data fields.  The constructor for a card requires {\em four}
integer arguments, the first two are the upper corner right corner of
the card, same as the first two arguments for the rectangle.
As cards are always a fixed size (CardWidth by CardHeight units), 
we don't need to
specify the other corner, as we can compute it.
The remaining two integer values in the constructor for the card
represent the suit and rank values (we will
assume cards always beging face-down).  The constructor for the
class card must invoke the constructor for the class rectangle.  This
is performed by following the heading for the class card with a colon,
then a call on the constructor for rectangle, as follows:

\begin{cprog}

card::card(int xv, int yv, int s, int r)
	: rectangle(xv, yv, xv + CardWidth, yv + CardHeight)
{
	// then initialize the card fields
	suit = x;
	rank = r;
	faceUp = 0;
}

\end{cprog}

In addition to the constructor, there will be three other functions associated
with the class card.  The first will be a redefinition of the function
draw, which will draw the face of a card.  The second will be a function
named flip, which takes no arguments and simply changes the card from being
face-up to being face-down, or vice-versa.
The third function overrides the moveTo function in class rectangle, 
replacing the function which took four integer arguments with a new
function that takes only two integer arguments.

For reasons we will explore in the questions, the function draw will 
be declared as ``virtual'' in both the class rectangle and the class card.
Thus, the complete class description for card can be given as follows:

\begin{cprog}

const int CardWidth = 75;
const int CardHeight = 100;

class card : public rectangle {
public:
	// constructor
	card(int, int, int, int);

	// operations
	virtual void draw(window *);
	void flip();
	void moveTo(int, int);

	// data fields
	int suit;
	int rank;
	int faceUp;
};

\end{cprog}

\subsection*{Doing the Assignment}

\begin{enumerate}
\item
You should enter this definition, plus the body of the functions for
the constructor and for flip and draw (the latter to be described shortly), 
in the file cardApp.cpp following
the class declaration and methods you created for rectangles in the
last assignment.
\item
The constructor must pass four arguments to the constructor for the
class rectangle.  The first two are the first two arguments to the
card constructor, while the second are these locations plus CardWidth
and CardHeight.  The remaining two constructor arguments are used to
initialize the suit and rank, and the card is always initially face down.
Write the constructor function.
$\vspace{1in}$
\item
What is the definition of the function flip?
$\vspace{1in}$
\item
Drawing a card is divided into two cases, depending upon whether the card
is face up or face down.  If face down, simply draw a large X between the
corners of the card, using a pen 4 pixels wide in a color of your choice.
(If you want to create some more interesting card back, feel free).
To draw the face-up part of a card, we will simply print text indicating
the value and suit of the card.  This is done using stream operators.
Streams are discussed in most C++ books, but we will simply here provide
you the code without explanation.

\begin{cprog}

void card::draw(window * win)
{
	// draw the outer rectangle
	rectangle::draw(win);

	// then draw the inner contents
	if (faceUp) {
			// first print out the suit
		win->wout << setpos(upperLeftX + 5, upperLeftY + 5);
		if (suit == 1) win->wout << "heart";
		else if (suit == 2) win->wout << "club";
		else if (suit == 3) win->wout << "spade";
		else if (suit == 4) win->wout << "diamond";

			// then print out the rank
		win->wout << setpos(upperLeftX+5, upperLeftY + 20);
		if (rank == 1) win->wout << "ace";
		else if (rank == 10) win->wout << "jack";
		else if (rank == 11) win->wout << "queen";
		else if (rank == 12) win->wout << "king";
		else win->wout << rank;
		}
	else {
		// face down, draw back
		// write your code here






		}
}
\end{cprog}

Finish writing the card drawing function.

\item
Write the implementation of the function moveTo.  This function should 
take two integer arguments, then invoke the function from class rectangle
which required four integer arguments, using our standard size of 50 by 30
for the card boundaries.  See the beginning of the draw function given above
for an illustration of how to specify that the function you wish to invoke
is the function inherited from the class rectangle.
$\vspace{1in}$

\item
Once again we have now completed the definition of our new data type, but
do not have any code that uses these values.
We will redefine the mouseDown and paint methods to illustrate the 
concept of virtual functions.

Following the class definition for rectangles and card, define the following
three global variables:

\begin{cprog}

rectangle a(50, 100, 150, 200);
card b(200, 100, 2, 7);
rectangle * c = new card(300, 100, 3, 5);

\end{cprog}

The paint method will simply ask each of these to draw themselves, as 
follows:

\begin{cprog}

void cardApplication::paint()
{
	a.draw(this);
	b.draw(this);
	c->draw(this);
}

\end{cprog}
Enter this method, then describe the output when you first start the
application.
$\vspace{1in}$.
\item
Are you surprized at the output produced for the third image, the one
produced by the global variable named ``c'' (which, you will recall, was
a pointer to a rectangle).
$\vspace{1in}$.
\item
To test your code for printing the backside of a card, change the
mouse down procedure so that if the mouse is clicked within the confines
of the card indicated by the global variable b, the card will flip.
This can be done as follows:

\begin{cprog}

void cardApplication::mouseDown(int x, int y)
{
	if (b.includes(x, y))
		b.flip();

	clearAndUpdate();
}

\end{cprog}
Test your application to make sure you can now print both the front side
and the backside of a card.
\item
What happens if you now change the mouse down procedure to try to do the
same actions with either the global variables a or c?  Why will they not
compile?
$\vspace{1in}$.
\item
Next, we are going to explore the meaning of the keyword ``virtual''.
Try removing the keyword virtual from the declaration of the draw method
in both the classes rectangle and the class card.  Then start the application.
What changes do you observe? (look carefully!!)
$\vspace{1in}$.
\item
What happens if you insert it into the declaration for the method draw
in the class rectangle, but not in card?
$\vspace{1in}$.
\item
What happens if you insert it into the declaration for the method draw
in the class card, but not in rectangle?
$\vspace{1in}$.

A considerable degree of the power of the use of object-oriented techniques
comes through the use of polymorphism, and in particular polymorphic
variables.  In C++ a polymorphic variable must be either a pointer
or a reference.  A polymorphic variable can hold either the value of
the class it is declared (the static type) or a variable from a subclass
(the dynamic type).  If a virtual function is used in conjunction with
a polymorphic variable, the actual function executed will be determined
by the run-time value (the dynamic type) of the object, not by its declared
type.

\item
You are now in a position to understand better the working of the application
framework, the LAF.  In each of the assignments you have done you have
created a new class, which was a subclass of class application.
The class application contained all the code needed to create a working
Windows application, namely code to move and resize windows, respond to
requests to form icons, and so on.  By subclassing from this class, you
get all of this code for free.  The methods paint and mousedown are declared
as virtual methods in the class application, and modified by your class
description.  Other methods, such as line (the method to draw a line), 
are used directly without modification.

In the main program (WinMain) there is a method that is inherited from
the application class and not overridden by your program.  What is the
name of this function?
\end{enumerate}


\end{document}
@EOF

chmod 600 asgn4.tex

echo x - asgn5.tex
cat >asgn5.tex <<'@EOF'
% written by Tim Budd, Oregon State University
% Public domain, may be used as a basis for programming assignments

\documentstyle[cprog]{article}
\begin{document}
\section*{Course in OOP \\ Programming Assignment 5}

In this assignment we will expand our card class into a deck of cards,
in preparation for the development of a card game in the next assignment.
The card game will be a somewhat simplified version of the solitare
program described in Chapter 10.
The class we will create in this assignment corresponds to a pile of
cards.

\subsection*{Creating the Application}

You can copy the card application you created in the previous assignment into
a new file, named pile.cpp.  This we will use as a basis for the new
application.  Replace all occurrences of the name cardApplication with
the new name pileApplication.  Once again, create a new project called
pile.prj, containing the source file pile.cpp and the little application
framework.

A deck of cards will be represented by a simple linked list.
Each link will contain two pointer fields, one a pointer to a card,
and the second a pointer to the next link.  The latter field can be null
to indicate the end of the linked list.   The class description for
the links is as follows:

\begin{cprog}

class cardLink {
public:
	// constructor
	cardLink (card *, cardLink *);

	// data fields
	card * value;
	cardLink * next;
};

\end{cprog}

A pile of cards will hold as a data file a pointer to a card link,
and a location for the upper left corner of the pile.
Most operations will be performed by executing actions on this set of links.
The following actions are needed for our card game:
\begin{itemize}
\item
Return the topmost card from the pile.  The card is represented by a 
pointer to an instance of class card.  Return a null pointer if there is
no such card.
\item
Add a new card (represented by a card pointer) to the pile, moving the
card if necessary.
\item
Tell whether any card in the pile contains a given point (described by
a pair of integers representing an x and y coordinate).
\item
Print the pile.  This is accomplished by printing the topmost card in the
pile.
\item
Respond when a pile is ``selected''.  the meaning of this operation will
differ depending upon the type of pile.  To accomodate this, the function
will be declared as virtual and the default will be to do nothing.
\end{itemize}

Combining these together yields the following class description:

\begin{cprog}

class cardPile {
public:
	// constructor, arguments are location
	cardPile(int, int);

	// operations
	virtual card * topCard();
	virtual void addCard(card *);
	int includes(int, int);
	virtual void display(window *);
	virtual void select();

	// data fields
	int upperLeftX;
	int upperLeftY;
	cardLink * firstLink;
};

\end{cprog}

\subsection*{Doing the assignment}

\begin{enumerate}
\item
Enter the class descriptions for cardLink and cardPile given above 
into your application program.  These should be placed
following the classes created in the earlier assignments.
\item
The constructor for cardLink should simply take the two argument
values and initialize the data fields.  Write this function.
$\vspace{1in}$
\item
The constructor for cardPile should initialize the upperLeftX and 
upperLeftY values to the arguments, then initialize the cardLink to
a null pointer value.  Write this function.
$\vspace{1in}$
\item
The insertion function must use the {\bf new} operator introduced in
the last assignment to dynamically create a new link field,
then initialize this with the value of the card and the current first
link, setting the newly created first link to the new field, then moving
the card to the location of the pile.
Using the constructor for links, this can all be accomplished in
a pair of statements, written as follows:

\begin{cprog}

void cardPile::addCard(card * newCard)
{
	firstLink = new cardLink(newCard, firstLink)l
	newCard->moveTo(upperLeftX, upperLeftY);
}

\end{cprog}
Enter this into your file.
\item
The remove method must take care of disposing of the link for the element
being returned (but not the card value itself!).  This is accomplished
using a pair of temporary variables as follows:

\begin{cprog}

card * cardPile::topCard()
{
	if (firstLink != 0) {
		cardLink * p = firstLink;
		card * q = p->value;
		firstLink = firstLink->next;
		delete p;
		return q;
		}

	// no cards, return a null pointer
	return 0;
}

\end{cprog}

\item
A temporary variable is also used to loop over all the elements in a list.
For example, the following function can be used to tell if any of 
the cards in a deck contain a specific point:

\begin{cprog}

int cardPile::includes(int x, int y)
{
	for (cardLink * p = firstLink; p != 0; p = p->next)
		if ((p->value)->includes(x, y))
			return 1;

		// not in any card, return 0
	return 0;
}

\end{cprog}

\item
By default, the printing method for class cardPile simply prints
the topmost card.  (We will later subclass from the class card, 
and sometimes change this behavior).  Write this method.
$\vspace{1in}$
\item
By default, the select method will do nothing.  Again, we will 
later subclass from class cardPile, and change this behavior.
Write this method.
$\vspace{1in}$
\item
The only subclass of cardPile we will describe in this assignment will
represent a randomly shuffled deck of cards.  
As cards are inserted, they are randomly placed into the deck.
To do this, we need to maintain a count on the number of cards in the
deck.  This is done by adding an additional field to the class
description:

\begin{cprog}

class deckPil : public cardPile {
public:
	// constructor
	deckPil(int, int);

	// actions
	virtual card * topCard();
	virtual void addCard(card *);

	// data fields
	int count;
};
\end{cprog}

\item
The constructor for class deckPile simply invokes the constructor for class
cardPile, then sets the count field to zero.  Write this constructor.
$\vspace{1in}$
\item
The topCard function merely decreases the count, then returns the result
of the function inherited from class cardPile.  Write the function.
$\vspace{1in}$
\item
The addCard method inserts the card into the deck in a random location.
To do this, we can use the function
rankInt provided in the little application framework.  This function provides
a random integer between two points.  A loop then interates this number 
of times, inserting the card at the resulting position.  This
function can be written as follows:

\begin{cprog}

void deckPile::addCard(card * newCard)
{
	int n = randomInt(0, count);
	if (n == 0)	// add to front
		cardPile::addCard(newCard);
	else {		// add in pile
			// move into location
		newCard->moveTo(upperLeftX, upperLeftY);
			// then find the place to insert
		cardLink * p = firstLink;
		while (n > 0) {
			if (p->next != 0)
				p = p->next;
			n = n - 1;
			}
		// insert after card pointed to by p
		p->next = new cardLink(newCard, p->next);
		}

	// increment count
	count = count + 1;
}

\end{cprog}

\item
To test our new data structure, we will initially place all fifty-two cards
into a deck.  This can be done by introducing a deck as a global variable,
named theDeck, at location 100, 100,  
and a plain pile, named discardPile, at location 100, 200.
In the main program (called WinMain) add the 
following code:

\begin{cprog}
for (int i = 1; i <= 4; i = i + 1)
	for (int j = 1; <= 12; j = j + 1)
		theDeck.add(new card(0, 0, i, j);
\end{cprog}

\item
When the mouse is clicked within the bounds of the deck, the top card
is removed from the deck and inserted into the discard pile.
When the mouse is clicked within the bounds of the discard pile,
the top card is removed, flipped, then reinserted into the same pile.
Write this method.  Make sure you schedule a clear and update after each click.
$\vspace{1in}$
\item
When a paint event occurs simply ask each of the two decks to redraw itself.
Write this function.
$\vspace{1in}$
\end{enumerate}

\end{document}

@EOF

chmod 600 asgn5.tex

echo x - asgn6.tex
cat >asgn6.tex <<'@EOF'
% written by Tim Budd, Oregon State University
% Public domain, may be used as a basis for programming assignments

\documentstyle[cprog]{article}
\begin{document}
\section*{Course in OOP \\ Programming Assignment 6}

In this assignment we will expand the card pile abstraction we developed
in chapter 5 into a real card game, somewhat similar to that described
in chapter 10 of the text.  We will simplify the program somewhat,
by not having buttons in the interface, and by changing the game to
not permit builds to be moved (therely permitting only the topmost card to
be needed on each table pile).

\subsection*{Creating the Application}

Copy the pile application you created in the previous assignment into
a new file, named game.cpp.  This we will use as a basis for the new
application.  Replace all occurrences of the name pileApplication with
the new name gameApplication.  Once again, create a new project called
game.prj, containing the source file game.cpp and the little application
framework.

\subsection*{Doing the Assignment}

\begin{enumerate}
\item
In order to create our game, we need to add one new method to the class
cardPile created in the last assignment.  Add a method canTake, which
takes a pointer to a card as argument, and returns an integer indicating
whether the pile can accept this new card.  The behavior in the class
cardPile is simply to return the value 0.

Write the new class description for class cardPile.
$\vspace{1in}$

\item
Write the implementation for the function canTake in class cardPile.
$\vspace{1in}$

\item
As described in chapter 10, the solitare game consists of four different
types of piles.  One of these, the deck pile, we have already created
in the last assignment (with the exception of the ``select'' method,
which we will shortly describe).  The other three piles are the discard
pile, the suit pile, and the table pile.

\item
Suit piles need only implement a constructor (which does nothing more than
invoke the constructor for the parent class), and override the method
canTake.
Write the class description for this class.
$\vspace{1in}$

\item
Write the constructor for this class.
$\vspace{1in}$

\item
Write the method canTake.  As described in the text, this should return
true if the pile is empty, or if the pile is not empty if the card matches
the suit of the current topmost card, and is rank one greater.
$\vspace{1in}$

\item
The class discardPile needs only to implement a constructor, and to override 
the method select.  Write the class description for this class.
$\vspace{1in}$

\item
The class tablePile needs only to implement a constructor, and to override
the methods canTake and select.  (The extra argument in the constructor,
described in the text, is not needed in our simplfied game).  Write this
class description.
$\vspace{1in}$

\item
Write the implementation of the canTake method for a table pile.
As described in the text,
a table pile can take a card if the pile is empty and the card is a king,
or if the topmost card is face up and the next card is a different color
and the next lower value.
$\vspace{1in}$

\item
The implementation of some of the methods described above need to refer to 
other piles.  To do this we will use global variables for the various
different piles.  The following declarations should be inserted after the
class definitions just described, but before the implementations of the
various methods.

\begin{cprog}

	// global declarations for the various decks
deckPile theDeck(480, 100);
discardPile theDiscard(400, 100);

suitPile suit1(50, 100);
suitPile suit2(130, 100);
suitPile suit3(210, 100);
suitPile suit4(290, 100);

tablePile table1(0, 200);
tablePile table2(80, 200);
tablePile table3(160, 200);
tablePile table4(240, 200);
tablePile table5(320, 200);
tablePile table6(400, 200);
tablePile table7(480, 200);

cardPile * allPiles[13];

\end{cprog}

\item
We can now write the method select for the class deckPile.
When executed, the topmost card is removed, flipped, then inserted into
the discard pile.  If there are no cards in the deck pile then the
function does nothing.
Write this function.
$\vspace{1in}$

\item
The select method for class discardPile examines the topmost card.
If it can be played on a suit pile (which we can discover by asking
each of the suit piles in turn if they can take a card) it is inserted
into the suit pile.  Otherwise, if it can be played on a table pile
it is inserted.  Otherwise it is simply added back into the discard pile.

\item
In response to a select method, the table pile class examines the topmost
card.  If it is face down it is flipped and reinserted into the pile.
If it is face up each of the suit piles is examined in turn, and if the
card can be played on a suit pile it is moved to that location.
If not, it is simply reinserted back into the table pile.

\item
Having implemented all the methods for a our data structures, we now must
write the initialization code for our application, and describe the
effect of mouseDown and paint events.

To initialize the application, place the following in the main procedure.

\begin{cprog}

// create the initial deck
for (int i = 1; i <= 4; i = i + 1)
	for (int j = 1; <= 12; j = j + 1)
		theDeck.add(new card(0, 0, i, j);

// put pointers to all the piles into an array
allPiles[0] = &theDeck;
allPiles[1] = &theDiscard;
allPiles[2] = &suit1;
allPiles[3] = &suit2;
allPiles[4] = &suit3;
allPiles[5] = &suit4;
allPiles[6] = &table1;
allPiles[7] = &table2;
allPiles[8] = &table3;
allPiles[9] = &table4;
allPiles[10] = &table5;
allPiles[11] = &table6;
allPiles[12] = &table7;

// then initialize the table piles
for (i = 1; i <= 7; i++) 
	for (j = 0; j < i; j++) 
		allPiles[i+5]->addCard(theDeck.topCard());

\end{cprog}

\item
To repond to a mouse down, the function simply cycles through all the piles
seeing if any contain the given point.  If so, then the pile is selected.
Using the fact that a pointer value will select the appropriate virtual
function for the type of object it points to (and not the type of object
it is declared as) we can use the array of all piles to simplify this task.

\begin{cprog}

void gameApplicatin::mouseDown(int x, int y)
{
	// see which pile is indicated
	for (int i = 0; i <= 12; i++)
		if (allPiles[i]->includes(x, y))
			allPiles[i]->select();

	// then update the screen
	clearAndUpdate();
}

\end{cprog}

\item
The paint function simply asks each pile to redraw itself.  Write this
method.
$\vspace{1in}$

\item
The resulting game is very hard to win.  In you wish, you might want to
explore some variations.  For example, one could change the select method
in the class deck so that if an empty deck is selected the remaining cards
in the discard pile are remove and reinserted into the deck (thereby 
permitting multiple trips through the discard pile).

\end{enumerate}
\end{document}
@EOF

chmod 600 asgn6.tex

echo x - asgn7.tex
cat >asgn7.tex <<'@EOF'
% written by Tim Budd, Oregon State University
% Public domain, may be used as a basis for programming assignments

\documentstyle[cprog]{article}
\begin{document}
\section*{Course in OOP \\ Programming Assignment 7}

In this assignment we explore one of the more recent additions to the C++
language, the idea of a template.  Templates are described in chapter 18
and in the recent revision for chapter 17.  A template is a parameterized
class, useful for creating container classes, such as vectors or linked lists.
There are no questions on this assignment, which is simply for your 
own education and information.

\subsection*{Creating the Application}

We will simply make changes to the application we created in chapter 6.
Use the same project and files.

\subsection*{Doing the Assignment}

\begin{enumerate}
\item
Replace the class description of cardLink, which is a link specifically tied
to the class card, with a more generic class link, as follows:

\begin{cprog}

template<class T> class link {
public:
	// constructor
	link (T , link *);

	// data fields
	T value;
	link * next;
};

\end{cprog}

\item
Replace the constructor for the class link with a generic constructor,
as follows:

\begin{cprog}

template<class T> link<T>::link(T val, link<T> * n)
{
	value = val;
	next = n;
}

\end{cprog}

\item
A card link is then simply a link with the parameter replaced by the
type ``pointer to card''.  Everywhere the type cardLink is used,
replace with the type 
\begin{cprog}
link<card *>
\end{cprog}

\item
Compile the resulting application, which should run as before.
Only now the class link is a general purpose class that can be carried
from one project to another.

\end{enumerate}
\end{document}
@EOF

chmod 600 asgn7.tex

exit 0