\documentclass[onecolumn,letterpaper,11pt]{article}

%===================================================================
% Include common packages, environments, counters etc
\input{common_latex/common_latex_packages}
\input{common_latex/common_latex_lab_format}
\input{common_latex/common_latex_definitions}
%===================================================================
\setcounter{tocdepth}{2}
%=====================================================

\begin{document}
\wikidoc{file=Lab.ClassIntroCPP}
\wikidoc{auth=mikerb@mit.edu}


%===================================================================
% Build the Cover Page in an environment style easy for wiki parsing
\begin{labcover}
\labtitle{Lab 2 - Introduction to C++}
\vspace{0.1in}
\labcourse{2.680 Unmanned Marine Vehicle Autonomy, Sensing and Communications}
\vspace{0.2in}
%\labpng{0.55}{figures/sim_cluster.png}
\labpng{0.3}{figures/cpp_icon.png}
\vspace{0.3in}
\labdate{February, 2022}
\vspace{0.1in}
\labauth{Michael Benjamin, mikerb@mit.edu}
\labauth{Henrik Schmidt, henrik@mit.edu}
\labauth{Department of Mechanical Engineering}
\labauth{MIT, Cambridge MA 02139}
\vspace{0.2in}
\labtoc{small}
\end{labcover}

%===================================================================
% Add a blank page so first body page is on the right when doubleside
\blankpage

%===================================================================
% Begin the body of the text
%======================================================================
\section{Overview and Objectives}
%======================================================================

This lab will introduce C, and C++ to new users. It assumes nothing
regarding C++ background, but does assume some familiarity with basic
programming concepts in other programming languages such as for-loops,
variable scope, conditional execution (if-then-else) for example.

\begin{labinfo}
%\labinfoheader{Topics:}
\begin{packed_itemize}[2]
\item Structure of a Program
\item Command Line Arguments
\item Variables and Data Types
\item Control Structures (if-else, while-loop, for-loop)
\item Simple Classes
\item Derived Classes
\end{packed_itemize}
\end{labinfo}

%======================================================================
\subsection{More C++ Resources}

We will only just touch the basics today. A couple further resources
are worth mentioning for following up this lab with your own
self-guided C++ exploration.

\noindent
Text book:

\begin{packed_itemize}
\item The text book I recommend is {\em Practical C++ Programming},
  Steve Oualline, O'Reilly Publishers, 2003.
\item This book is available on Amazon with used copies typically
  under ten dollars. We have a couple books to loan available first-come
  first server.
\end{packed_itemize}

\noindent
Web sites:

\begin{packed_itemize}
\item The \urlx{www.cplusplus.com} web site has a lot of resources. In
  particular there are a number of good tutorials:
  \urlx{http://www.cplusplus.com/doc/tutorial}
\item The standard template library has a number of useful tools
  ranging from basic tools that we'll use right out of the gate, like
  strings. Intermediate tools like vectors, maps, and sets. The
  following is great resource: \urlx{http://www.cplusplus.com/reference/stl}
\end{packed_itemize}


%======================================================================
\subsection{What you will need to get started}

You will need a machine that has:

\begin{packed_itemize}
\item A text editor such as Emacs, vi or some ability to edit text files.
\item The C++ compiler installed. In this lab, we assume the compiler is 
  the executable \cmd{g++}, typically found at \cmd{/usr/bin/g++}. You may
  want to check that this is in your shell's path.
\end{packed_itemize}

%======================================================================
\section{Structure of a Program}
%======================================================================

In this exercise we will follow the example in Listing \ref{lst_block_one}
below, which can also be found at the URL listed at the top of the
block. The web site is very good at explaining each component. I
recommend reading through the entire page explaining this example.

\pskip
\noindent
Your goals in this part are:

\begin{enumerate}
\item Open an editor and enter the code block in Listing \ref{lst_block_one}, minus the line numbers. \newline
Save the code in a file named \var{hello.cpp}.
 
\item Read through the explanation on the web site. (If you are new to C/C++, please 
do take the time to read this through.)

\item Build the program into an executable. \newline
On the command line do: \cmd{g++ hello.cpp} \newline
This should generate a file called \var{a.out} 

\item Verify that it runs by running it. \newline
On the command line do: \cmd{./a.out}

\item NOTE: For those still getting familiar with the command-line and
  shell paths, the \var{"./"} in the above invocation is important. It
  tells your shell where to look for the executable \var{a.out}. Normally
  the shell looks only in the set of directories in its {\em path}. By 
  default, the present working directory is not in the shell's path. The 
  notation \var{"./"} is shorthand for the present working directory. The 
  notation \var{"../"} refers to the directory above the present directory.
  Try running \var{"ls -a"} and you will see that both \var{"./"} and 
  \var{"../"} are listed. 

\item Rather than building the cryptic \var{a.out} executable, try
  building it to have a more meaningful name: \cmd{g++ -o hello
    hello.cpp}. Then you should be able to run the program with \cmd{./hello}

\end{enumerate}


\begin{listingx}{Example Code}
\label{lst_block_one}
\footnotesize
\begin{Verbatim}[frame=single]
  0  // Code example from: http://www.cplusplus.com/doc/tutorial/program_structure/
  1  // my first program in C++
  2  #include <iostream>
  3  
  4  using namespace std;
  5  
  6  int main ()
  7  {
  8    cout << "Hello World!" << endl;
  9    return 0;
 10  }
\end{Verbatim}
\end{listingx}

%=======================================================================
\section{Command Line Arguments}
%=======================================================================

In this exercise we will expand on the first example by adding the
ability to handle command line arguments in our program. (This section
does not correspond to a section on the cplusplus.com tutorial site.)
A {\em command line argument} is an argument passed to the program on
the command line. For example, it would be nice if our Hello World!
program had the ability to accept an alternative message on the
command line. For example:

\vspace{0.1in}
\begin{consoleverb}
 $ ./hello Greetings!
\end{consoleverb}
\vspace{0.1in}

In the \func{main()} function, there is the option of having two
arguments as shown in Listing \ref{block_two}.  The first argument is
an integer, \var{argc}, indicating the number of command line
arguments.  The program itself is always the first argument, so the
smallest this number could be is one. The second argument, \var{argv},
holds the actual values of all the command line arguments. (We'll punt
for now on the notation \var{char**}, but in short it can be thought
of as a pointer to an array of strings.) The first argument is
\var{argv[0]}, the second argument, if it exists, is \var{argv[1]} and
so on.

\pskip

\noindent
Your goals in this part are:

\begin{enumerate}
\item Open an editor and enter the code block in Listing
  \ref{block_two}. \newline Save the code in a file named
  \var{hello\_arg.cpp}.
 
\item Build the program into an executable. \newline On the command
  line do: \cmd{g++ -o hello\_arg hello\_arg.cpp} \newline This should
  generate an executable file called \var{hello\_arg} in the same 
  directory.

\item TIP: when you invoke the command \cmd{ls} in your terminal to
  see if the \var{hello\_arg} executable is there, try doing \cmd{ls
    -F} instead. This will append an asterisk to all executables
  making it easier to visually find what you're looking for. In OS X,
  try \cmd{ls -FG} to add some color.  If you like this idea, consider
  making it the default for your shell, by putting \var{alias ls='ls
    -FG'} in your \var{.bashrc} file.  If you are using the
  \var{.bashrc} file from MIT 2.680, this alias may already be there.


\item Verify that it runs by running it. \newline On the command line
  do: \cmd{./hello\_arg}.

\item Modify the program such that instead of the original output, it 
  instead outputs a custom greeting. Your program should respond as
follows:
\end{enumerate}

\begin{consoleverb}
 $ ./hello_arg Greetings!
 $ Greetings!
\end{consoleverb}

%+++++++++++++++++++++++++++++++++++
\vspace{0.1in}
\begin{listingx}{Example Code - Adding command line arguments}
\label{block_two}

\footnotesize
\begin{Verbatim}[frame=single]
  1  // Handling command line arguments
  2  
  3  #include <iostream>
  4  using namespace std;
  5  
  6  int main (int argc, char **argv)
  7  {
  8    cout << "Total number of arguments: " << argc << endl;
  9    cout << "The first argument is: " << argv[0] << endl;
 10    return 0;
 11  }
\end{Verbatim}
\normalsize
\end{listingx}
%+++++++++++++++++++++++++++++++++++

%=======================================================================
\section{Variables and Data Types}
%=======================================================================
\label{var_types}

In this exercise we will follow the example in Listing
\ref{lst_block_three} below, which can also be found at the URL listed at
the top of the block. The web site discusses C++ variable names,
fundamental variable types, and variable declarations. Later on we
will be creating our own "types" in the form of C++ classes, but a
good understanding of the C/C++ fundamental types is important. {\em
  Take the time to read through this web page.}

\pskip
\noindent
Your goals in this part are:

\begin{packed_enumerate}[3]
\item Open an editor and enter the code block in Listing
  \ref{lst_block_three}. \newline Save the code in a file named
  \var{var\_types.cpp}.
 
\item Build the program into an executable. \newline On the command
  line do: \cmd{g++ -o var\_types var\_types.cpp} \newline This should
  generate an executable file called \var{var\_types} in the same
  directory.

\item Verify that it runs by running it. \newline On the command line
  do: \cmd{./var\_types}

\item Modify the program such that instead of assigning and processing
  the results as done in lines 12-16, we instead grab two variables
  from the command line, add them, and output the result. 

  Save your modified program in a file with a different name such as: 
  \var{var\_types\_cmd.cpp}

  To do this you will need to do two things. First you will need to
  include a library, for converting ASCII strings to numbers, near the
  top of the program, \var{\#include <cstdlib>}. Next you will need to
  invoke the \func{atoi()} function to convert the string to an
  integer.  This snippet shows the general idea:

\vspace{0.15in}
\footnotesize
\begin{verbatim}
  #include <cstdlib> 
  ....
  int a;
  a = atoi(argv[1]);
\end{verbatim}
\normalsize
\vspace{0.15in}

\item Verify that your program works by running on the command line:
  
\vspace{0.15in}
\begin{consoleverb}
 $ ./var_types_cmd  12 44
 56
\end{consoleverb}
\vspace{0.15in}

\item Repeat the above two steps, but in this case the goal is to handle
  floating point numbers. You will need to declare your variables 
  as the type \var{float}, and invoke the \func{atof} function instead. 

\item TIP: You can read more about the \func{atof} and \func{atoi}
  functions by typing \cmd{man atoi} on the command line.

\end{packed_enumerate}

%+++++++++++++++++++++++++++++++++++
\vspace{0.1in}
\begin{listingx}{Example Code - Variables and Data Types}
\label{lst_block_three}

\scriptsize
\begin{Verbatim}[frame=single]
  0  // Code example from: http://www.cplusplus.com/doc/tutorial/variables/
  1  // operating with variables
  2   
  3  #include <iostream>
  4  using namespace std;
  5  
  6  int main ()
  7  {
  8    // declaring variables:
  9    int a, b;
 10    int result;
 11  
 12    // process:
 13    a = 5;
 14    b = 2;
 15    a = a + 1;
 16    result = a - b;
 17  
 18    // print out the result:
 19    cout << result << endl;
 20  
 21    // terminate the program:
 22    return 0;
 23  }
\end{Verbatim}
\normalsize
\end{listingx}

%=======================================================================
\section{Control Structures}
%=======================================================================

In this exercise we will begin using the three most common control
structures found in C++, the if-then-else, while-loop, and for-loop
constructs. The idea, if not the C++ syntax, should be familiar to
those with experience in other programming languages. In this section
we will be following the topic described at the cplusplus.com tutorial
page:

\vspace{0.1in}
\noindent
\urlx{http://www.cplusplus.com/doc/tutorial/control/}
\vspace{0.1in}


\noindent
This lab exercise will have three sub-parts, for each of the control
structures, if-else, while-loop, and for-loop.

%=======================================================================
\subsection{The if and else Structures}

In the first part, your goals are:

\begin{packed_enumerate}[4]
\item Read through the first part of the web tutorial on the {\em
  conditional structure}, namely the if and else structure.
 
\item We're going to make a modified copy of the program you created
  in Section \ref{var_types} in Step 4, the program
  \var{var\_types\_cmd.cpp}. Start by making a copy of this file and
  renaming it to \var{add\_nums.cpp}

\vspace{0.1in}
\begin{consoleverb}
 $ cp var_types_cmd.cpp add_nums.cpp
\end{consoleverb}
\vspace{0.1in}

\item In the original version of this program the two numbers being
  added were taken from the command line, \var{argv[1]}, and
  \var{argv[2]}. If these arguments were not provided, this could
  result in a segmentation fault (program crash) since \var{argv[2]}
  doesn't exist unless provided.  In this step, use the condition
  structure to check for the number of arguments. If the wrong number
  of arguments is provided, output a warning message, otherwise handle
  the addition and output as normal.

\item Verify that your program works by running on the command
  line. It should look something like:
  
\vspace{0.1in}
\begin{consoleverb}
 $ ./add_nums 3 8
 11
 $ ./add_nums 38
 please provide exactly two arguments.
\end{consoleverb}
\vspace{0.1in}

\end{packed_enumerate}


%========================================================================
\subsection{The while-loop Structure}

In the second part, your goals are:

\begin{packed_enumerate}[4]

\item Read through the second part of the web tutorial on the {\em Iteration 
structures (loops)}, namely the while loop structure.
 
\item Open an editor and enter the code block in Listing
  \ref{block_while}. (cut-paste from the web site) \newline Save the
    code in a file named \var{countdown.cpp}.
 
\item Build the program into an executable. \newline On the command
  line do: \cmd{g++ -o countdown countdown.cpp} \newline This should
  generate a executable file called \var{countdown} in the same
  directory.

\item Verify that it runs by running it. \newline On the command line
  do: \cmd{./countdown}.

\item Modify the program such that instead of accepting a single
  number from the user, it repeatedly accepts a number, checks whether
  it is even or odd, outputs the result, and quits when the first even
  number is provided.

  Save your modified program in a file with a different name such as:
  \var{even\_odd.cpp}

  HINT: You may want to use the C++ modulus operator, e.g.,
  \var{(7\%2)=1}, \var{(17\%3)=2}.

\item Verify that your program works by running on the command
  line. It should look something like:
  
\vspace{0.15in}
\begin{consoleverb}
 $ ./even_odd
 Enter a number: 3
 The number 3 is odd
 Enter a number: 19
 The number 19 is odd
 Enter a number: 2
 The number 2 is even. Goodbye!
\end{consoleverb}
\vspace{0.15in}

\end{packed_enumerate}

%+++++++++++++++++++++++++++++++++++
\vspace{0.1in}
\begin{listingx}{Example Code - The while loop structure}
\label{block_while}

\scriptsize
\begin{Verbatim}[frame=single]
  0  // Code example from: http://www.cplusplus.com/doc/tutorial/control/
  1  // custom count-down using while
  2    
  3  #include <iostream>
  4  using namespace std;
  5    
  6  int main ()
  7  {
  8    int n;
  9    cout << "Enter the starting number > ";
 10    cin >> n;
 11  
 12    while (n>0) {
 13      cout << n << ", ";
 14      --n;
 15    }
 16    
 17    cout << "FIRE!\n";
 18    return 0;
 19  }
\end{Verbatim}
\normalsize
\end{listingx}

%==================================================================
\subsection{The for-loop Structure}

In the third part, your goals are:

\begin{enumerate}

\item Read through the second part of the web tutorial on the {\em Iteration 
structures (loops)}, namely the for-loop structure.
 
\item Open an editor and enter the code block in Listing
  \ref{block_forloop}. (cut-paste from the web site) \newline Save the
    code in a file named \var{for\_countdown.cpp}.
 
\item Build the program into an executable. \newline On the command
  line do: \cmd{g++ -o for\_countdown for\_countdown.cpp} \newline
  This should generate a executable file called \var{for\_countdown}
  in the same directory.

\item Verify that it runs by running it. \newline On the command line
  do: \cmd{./for\_countdown}.

\item Modify the program such that it accepts two integers from the
  command line from the user, and outputs all multiplication
  combinations between the two numbers ranging from $[0, N]$ for each
  number. Hint: Try nesting one for loop inside the other.

  Save your modified program in a file with a different name such as:
  \var{forloop\_mult.cpp}

\item Verify that your program works by running on the command
  line. It should look something like:

\vspace{0.1in}  
\begin{consoleverb}
 $ ./forloop_mult 10 10
 0 0 0 0 0 0 0 0 0 0 0 
 0 1 2 3 4 5 6 7 8 9 10 
 0 2 4 6 8 10 12 14 16 18 20 
 0 3 6 9 12 15 18 21 24 27 30 
 0 4 8 12 16 20 24 28 32 36 40 
 0 5 10 15 20 25 30 35 40 45 50 
 0 6 12 18 24 30 36 42 48 54 60 
 0 7 14 21 28 35 42 49 56 63 70 
 0 8 16 24 32 40 48 56 64 72 80 
 0 9 18 27 36 45 54 63 72 81 90 
 0 10 20 30 40 50 60 70 80 90 100 
\end{consoleverb}

\end{enumerate}


%+++++++++++++++++++++++++++++++++++
\vspace{0.1in}
\begin{listingx}{Example Code - The for-loop structure}
\label{block_forloop}

\scriptsize
\begin{Verbatim}[frame=single]
  0  // Code example from: http://www.cplusplus.com/doc/tutorial/control/
  1  // countdown using a for loop
  2  #include <iostream>
  3  using namespace std;
  4  int main ()
  5  {
  6    for (int n=10; n>0; n--) {
  7      cout << n << ", ";
  8    }
  9    cout << "FIRE!\n";
 10    return 0;
 11  }
\end{Verbatim}
\normalsize
\end{listingx}

%=======================================================================
\section{Simple Classes}
%=======================================================================

In this exercise we will begin using C++ classes. Everything prior to
this section in today's lab is really in the domain of the C
programming language. The use of {\em classes}, or more precisely the
{\em objects} which are {\em instances} of classes, is what makes C++
an "object-oriented programming language".  In this section we will be
following the topic described at the \var{cplusplus.com} tutorial page:

\vspace{0.15in}
\noindent
\urlx{http://www.cplusplus.com/doc/tutorial/classes/}
\vspace{0.15in}

\pskip
\noindent
This lab exercise will have two sub-parts, (a) building and
modifying the example class from the web site, (b) implementing your
class and program in separate files.

%========================================================================
\subsection{A simple class example (from the web site)}

\pskip
\noindent
In the first part, your goals are:

\begin{packed_enumerate}[4]

\item Read through the first part of the web tutorial on Classes(I) at \newline 
  \urlx{http://www.cplusplus.com/doc/tutorial/classes/}
 
\item Open an editor and enter the code block in Listing
  \ref{simple_rect}. \newline Save the code in a file named
  \var{simple\_rect.cpp}.

\item Build the program ainto an executable:
 
\begin{consoleverb}
 g++ -o simple_rect simple_rect.cpp
\end{consoleverb}

\item The above should generate a executable file called
  \var{simple\_rect} in the same directory. Verify that it runs by
  running it:

\begin{consoleverb}
 ./simple_rect
\end{consoleverb}

\item Prepare to make a modified version of this program by copying the
  file \vvar{simple\_rect.cpp} to \vvar{my\_rect.cpp}

\item Modify the program in following three ways: (1) In addition the
  \vvar{x}, and \vvar{y} member variables, add a member variable of
  type \vvar{string} indicating the rectangle color. You will need to
  include the string library, \vvar{\#include <string>}. (2) In addition
  to the \vvar{set\_values} member function, add another member function
  for setting the rectangle color. It will take a single (string) argument.
  (3) Add yet another member function called \vvar{print()} taking no
  arguments, but writes the area of the rectangle and color to the terminal
  using \vvar{cout}.

\item Modify your \func{main()} function to give the rectangle a color, 
  and use the \func{print()} function instead of the original call to
  \vvar{cout} to convey the rectangle properties.

\item Build your program: \cmd{g++ -o my\_rect my\_rect.cpp}.  Verify
  that your program works by running on the command line. It should
  look something like:
  

\begin{consoleverb}
 $ ./my_rect
 area: 12
 color: yellow
\end{consoleverb}

\end{packed_enumerate}


\vspace{0.1in}
\begin{listingx}{Example Code - A simple class example}
\label{simple_rect}

\scriptsize
\begin{Verbatim}[frame=single]
  1  // Code example from: http://www.cplusplus.com/doc/tutorial/classes/
  2  // classes example
  3  #include <iostream>
  4  using namespace std;
  5   
  6  class CRectangle {
  7      int x, y;
  8    public:
  9      void set_values (int,int);
 10      int area () {return (x*y);}
 11  };
 12   
 13  void CRectangle::set_values (int a, int b) {
 14    x = a;
 15    y = b;
 16  }
 17   
 18  int main () {
 19    CRectangle rect;
 20    rect.set_values (3,4);
 21    cout << "area: " << rect.area() << endl;
 22    return 0;
 23  }
\end{Verbatim}
\end{listingx}


%========================================================================
\subsection{Building your implementation over distinct files}

One of the nice things about C++ is that objects (classes), once
defined and implemented, may be used in many programs. In the example
implemented above, instances of the \vvar{CRectangle} class could only
be used in that particular program. By splitting out the class
definition and class implementation into separate files, objects of
that class may be used multiple programs.


\pskip
\noindent
In this next part, your goals are:

\begin{packed_enumerate}[4]

\item Open an editor and enter the code blocks in Listing
  \ref{dist_simple_rect} into three distinct files named as indicated
  on the first line in each block.

\item Build the program into an executable. \newline On the command
  line do: \cmd{g++ -o distributed\_rect CRectangle.cpp
    distributed\_rect\_main.cpp} \newline This should generate a executable
  file called \var{distributed\_rect} in the same directory.

\item Verify that it runs by running it. \newline On the command line
  do: \cmd{./distributed\_rect}.

\end{packed_enumerate}

%+++++++++++++++++++++++++++++++++++
\vspace{0.1in}
\begin{listingx}{Example Code}
\label{dist_simple_rect}

\scriptsize
\begin{Verbatim}[frame=single]
  1  // distributed_rect_main.cpp
  2  
  3  #include <iostream>
  4  #include "CRectangle.h"
  5  using namespace std;
  6   
  7  int main () {
  8    CRectangle rect;
  9    rect.set_values (3,4);
 10    cout << "area: " << rect.area() << endl;
 11    return 0;
 12  }
\end{Verbatim}
\normalsize
\end{listingx}

\pskip
%+++++++++++++++++++++++++++++++++++
\begin{listingx}{Example Code}
\scriptsize
\begin{Verbatim}[frame=single]
 1  // CRectangle.h
 2  
 3  class CRectangle {
 4      int x, y;
 5    public:
 6      void set_values (int,int);
 7      int area () {return (x*y);};
 8  };
\end{Verbatim}
\normalsize
\end{listingx}

\pskip
\begin{listingx}{Example Code}
\scriptsize
\begin{Verbatim}[frame=single]
 1  // CRectangle.cpp
 2 
 3  #include "CRectangle.h"
 4  using namespace std;
 5   
 6  void CRectangle::set_values (int a, int b) {
 7    x = a;
 8    y = b;
 9  }
\end{Verbatim}
\normalsize
\end{listingx}

%========================================================================
\subsection{Constructors and Destructors}

We won't have time to work through examples on different ways to implement
your class' constructor(s) or destructor(s). Minimally you need to understand
that they are functions that are called automatically when an instance of 
a class is created (constructor) and when it is destroyed or goes out of scope
(destructor). 

\pskip
\noindent
Be sure to at least read the explanation on this topic in the latter part of the
web page:

\vspace{0.15in}
\noindent
\urlxx{http://www.cplusplus.com/doc/tutorial/classes}

%=======================================================================
\section{Derived Classes}
%=======================================================================

An important feature of C++ over C is the ability to define classes in
a hierarchical structure. Subclasses of a class may be defined that
inherit (or override) properties of its parent class. This has
enormous utility with respect to the goal of "code re-use". Class
inheritance is what enables MOOS applications and IvP Helm behaviors to
be written from a general MOOSApp and IvPBehavior superclass.

\pskip

In this exercise we will explore the use of class inheritance with a
simple example. Readers should regard this as a launching point to
explore further the details of C++. If you've gotten this far and
through this exercise in today's lab, you have a good chance to
understand the autonomy specific code used in this course. However, we
recommend you keep going with your C++ exploration in this course. It
is an essential, and very empowering language in marine robotics.


\pskip
\noindent
In this section we will be
following the topic described at the cplusplus.com tutorial page:

\vspace{0.1in}
\urlxx{http://www.cplusplus.com/doc/tutorial/inheritance/}
\vspace{0.1in}


\pskip
\noindent
In the exercise, your goals are:

\begin{packed_enumerate}[4]

\item Read through the first part of the web tutorial on Friendship and
  Inheritance at \newline 
  \urlx{http://www.cplusplus.com/doc/tutorial/classes/} \newline
  While the notion of class friendship is useful, it is not our focus 
  here. I recommend skimming this material for now.
 
\item Open an editor and enter the code block in Listing
  \ref{derived}. \newline Save the code in a file named
  \vvar{derived.cpp}.

\item Build the program into an executable. On the command line do:

\begin{consoleverb}
 $ g++ -o derived derived.cpp
\end{consoleverb}

\item The above should generate a executable file called \var{derived}
  in the same directory.  Verify that it runs by running it.

\begin{consoleverb}
 $ ./derived
\end{consoleverb}

\item Prepare to make a modified version of this program by copying the
  file \var{derived.cpp} to \var{diamond.cpp}

\item Modify the program by adding a new derived class called \var{CDiamond}, 
  and implement its \func{area()} function accordingly. Add the use of the 
  CDiamond class to the \func{main()} function.

\item Build your program: 

\begin{consoleverb}
 $ g++ -o diamond diamond.cpp
\end{consoleverb}

\item Verify that your program works by running on the command
  line. It should look something like:
  
\vspace{0.1in}
\begin{consoleverb}
 $ ./diamond
 20
 10
 5
\end{consoleverb}
\end{packed_enumerate}

%+++++++++++++++++++++++++++++++++++
\vspace{0.1in}
\begin{listingx}{Example Code}
\label{derived}

\scriptsize
\begin{Verbatim}[frame=single]
  0  // Code example from: http://www.cplusplus.com/doc/tutorial/inheritance/
  1  // derived classes
  2  #include <iostream>
  3  using namespace std;
  4  
  5  class CPolygon {
  6    protected:
  7      int width, height;
  8    public:
  9      void set_values (int a, int b)
 10        { width=a; height=b;}
 11    };
 12  
 13  class CRectangle: public CPolygon {
 14    public:
 15      int area ()
 16        { return (width * height); }
 17    };
 18  
 19  class CTriangle: public CPolygon {
 20    public:
 21      int area ()
 22        { return (width * height / 2); }
 23    };
 24    
 25  int main () {
 26    CRectangle rect;
 27    CTriangle trgl;
 28    rect.set_values (4,5);
 29    trgl.set_values (4,5);
 30    cout << rect.area() << endl;
 31    cout << trgl.area() << endl;
 32    return 0;
 33  }
\end{Verbatim}
\normalsize
\end{listingx}

%=======================================================================
\subsection{Polymorphism}

If you have time still in this lab, explore the issue of polymorphism
discussed at:

\vspace{0.1in}
\noindent
\urlx{http://www.cplusplus.com/doc/tutorial/polymorphism/}

\pskip
\noindent
In particular, focus on the issue of virtual members and abstract base
classes.  Try to re-work the Polygon example above by defining
\func{area()} as a virtual function as discussed on the web site. See
if you can learn the difference between a \var{"virtual function"} and
a \var{"pure virtual function"}. Should the \func{area()} function for
the \var{CPolygon} class above be \var{"virtual function"} or a
\var{"pure virtual function"}? Why? If the former, what would be a
reasonable implementation?


\end{document}



