| 
      Computer Science 197c - Programming in C++ Winter Session 2001
 Brent Heeringa
 Work Phone: 413.545.3616
 Home Phone: 413.549.9619
 
 
      
      [
      General Information |
      Lectures |
      Office Hours |
      Assignments | 
      References
      ]
      
       
       
      This is a first course in C++ for people who are comfortable
      with Java.  I'm not exactly sure what comfortable
      means, but you probably should be able to tell me about abstract
      and concrete classes, objects, interfaces, inheritance,
      visibility, encapsulation, polymorphism, exceptions, packages
      and some procedural programming constructs like condtionals,
      iteration, and recursion.  I also expect that you are familar
      with the Java environment including the compiler (javac), the
      classpath environment variable (CLASSPATH) and the Java
      virtual machine (java).
       
       I'm not really into traditional exams and for a straight-shot
       programming course it seems fairly innapproriate to require a
       final, so I'd much rather write a lot of code and then talk
       about our experiences in doing it.
       
       
      I really want the first few lectures to be a solid introduction
      to C++, and the final few to be a friendly, involved discussion
      about the language.  There are seven lectures, so that leaves us
      about three lectures to ooze into on both sides.  If the class
      turns out to be one big question and answer session after the
      first few periods, I'd be thrilled.  If it doesn't, that's cool
      too.
        Just so you know, I'm not really into hand-raising.  General
      shouting, interruption, voice-clearing, or other methods of
      attention seem to work best for me.  If you want to raise your
      hand, there's a non-trivial chance I'll call on you.  Just
      realize you don't need to go about things that way.  
       
       
      How about Tuesdays and Thursdays from 16:00 to 17:00 in Room 264
      of the new computer science building.  Send email anytime.
       
       
      
      I'll post assignments here along with due dates,
      corrections, digressions, general comments, sonnets, movie
      reviews, and witty banter.
      
      
      Assignment 0
 
      The best mechanism for learning a new language is to write code
      using that language.  So, it seems reasonable that the first
      exercise should be a straight-forward programming assignment.  
       
      Chapter 14 of Budd [page 215] introduces rational numbers (or
      fractions) in a case study.  Most of the implementation is given
      in bits and pieces throughout the chapter.  I've implemented my
      own Rational number class that closely resembles that given in
      the text but highlights a number of constructs available in the
      C++ programming language.  I've also implemented a Complex
      number class in Java.  This class contains the common algebraic
      operations addition, subtraction, multiplication, and division
      as well as exponentiation, equivalence, and formatting methods.
       
      The assignment is to implement the Complex number class in C++
      using a style similar to that given in the Rational class I've
      written.  By style, I don't mean you must use the same
      conventions for bracket placement but rather, use the natural
      C++ conventions for implementing functions and operations.  For
      example, use the operator overloading capabilities of C++ to
      implement the algebraic operations so that plus appears like
       
      const Complex operator+(const Complex& c1, const Complex& c2) {
         // logic goes here
      }
      instead of
      const Complex Complex::plus(const Complex& c) {
        // logic goes here
      }
      Really, the above two functions are quite different.  They both
      implement a flavor of addition, but the former is not a member
      function, while the latter is. 
      Make sure to overload the output stream, equality, and
      assignment operators.  Also implement a copy constructor and any
      other supporting routines you deem necessary.  Reuse the
      DivideByZeroException class I provided in the Rational
      example.  I realize this is a pretty hefty assignment, but
      because the Rational number class is heavily documented and the
      syntaxes between the Rational and Complex implmentations are
      almost identifical, it should decrease the harshness and be a
      profitable assignment.
       
      You can download all the necessary files standalone, in a zip
      file, or in a gziped tar file below.
       
      
            
      
      
      
 
      Use the MainComplex.cc file to test your code.  Turn in
      your Complex.h and Complex.cc files to me on
      10 January 2001 by 17:00.  This gives us Friday and Wednesday to
      talk about crazy stuff that happens.  Feel free to write me email or drop by the ol'
      office hours.
       
      To compile the Rational number class (and it's necessary
      supporting files), download the files to some directory.  if you
      downloaded a zip file use
       
      % unzip assignment0.zip
      If downloaded a tgz file use
      % gzip -d assignment0.tgz
      % tar -xvf assignment0.tar
      The C++ Rational number class will use all the files except for
      MainComplex.cc.  However, we only need to compile the
      implementation (.cc) files.  So do a
      % g++ -fhandle-exceptions Rational.cc DivideByZeroException.cc Cmpsci197cUtil.cc Main.cc
      This will compile all the files into an executable called
      a.out.  If you want the compiler to dump the executable into a
      different file, you can add
      -o FileName
      to the command.
      The best way to compile C++ is probably using a makefile.  If you download the
      makefile into the same
      directory as the source and type
      % make
      at the command line, this will compile the Rational number class
      into an executable called Main.  This makefile works for the
      version of g++ on oit.  Notice that you explicitly need to tell
      the compiler to use exception handeling.  On newer versions of
      g++ this has been changed to be the default.
           
      Assignment 1
 
      Some of you are still reeling from assignment 0, so here is a (I
      think) a fun assignment to help you understand the role (and not
      the role) of pointers in C++.  Download the pointers.cc file and read the
      directions given in the comments.  There you will find that your
      assignment is to fill in 8 blanks in the code, so that the
      proper output is produced.  The blanks are indicated by literal
      __ and comments enumerating them.  There are 8 blanks.
      Each is worth 5 points.  I will take 1 point off for close
      answers, 2 points off for answers that reflect some sort of
      thought, 3 points off for being in the right hemisphere, and 4
      points off for being wicked wrong.  If you leave it blank or
      comment it out (in which case the file will not compile), you
      will get 5 points off.  Start early.  The assignment is due
      Wednesday, 17 January 2001, at 17:00.  As always, send me mail if you have
      questions or comments.
      Assignment 2 
 
      Okay.  First I apologize for not having this assignment up in a
      timely fashion.  I want you to become familar with templates, so
      I've implemented a very basic linked list class.  It is
      very similar to the one we talked about in class on 17 January
      2001.  I have also implemented the header (.h) file for a
      parameterized Stack class.
       
      Your assignment is to implement the Stack class.  You should use
      the LinkedList _list as your underlying data structure.  In all
      cases but two, the actual implementation of the functions
      specified in the header file are one liners.  For example, for
      the push operation, you should write:
       
      template 
      void Stack::push(T value) {
        _list.add(value);
      }
      
      The LinkedList.h and LinkedList.cc files are documented
      extensively.  For the love of god, read over these files first.
      Understand them before you start writing the Stack class code.
      If you come to me with a question that is answered within the
      comments of the LinkedList implementation I will verbally abuse
      you.  Well, okay, I won't verbally abuse you, but I will tell
      you to shove off and read the comments.  Once you understand the
      LinkedList class, take a look at the Stack.h file.  You must
      implement all the functions defined in the header.  So, you
      should create a Stack.cc file and begin implementing.
      A word of warning.  The g++ compiler on OIT is old enough that
      it doesn't like a small change in the c++ template syntax.  I
      have made code available that runs on OIT and code available
      that runs on newer compilers.  If you are having a problem with
      the operator== function, you may need to alter the function
      prototype according to the comments in the Stack.h
      file.  Hopefully, everyone who uses OIT will just download the
      OIT version of the code and everyone who doesn't use OIT will
      download the other version and everyone will be happy.  If you
      don't use the OIT verion of g++ and the compiler is whining to
      you, then go ahead and try the other version.  The problem lies
      with the friend declaration of the operator== function.  In
      newer c++ versions, the friend definition looks like this: 
      friend bool operator==<>(const Stack& s1, const Stack& s2);
      and in older c++ versions the syntax should read:
      friend bool operator==(const Stack& s1, const Stack& s2);
      I have documented this inside both the LinkedList and Stack
      class, so hopefully you won't run into any problems.
      To compile the files using g++ type:
      
       
      % g++ LinkedList.cc Stack.cc Main.cc
      LinkedList.cc is the low-grade linked list
      implementation, Stack.cc is the stack class you'll
      create and Main.cc is a small program to help you test
      your stack class.
      The assignment is due Monday, 22 January 2001 at 17:00.  If you
      have questions, send me mail, or come to office hours
      on thursday.  I'll also answer questions on friday, so get to an
      early start on this baby.
       
      
            
      
       Assignment 3
 
 
      This assignment will introduce you to inheritance and
      polymorphism.  I have written an abstract Shape class and a
      concrete implementation (subclass) called Circle.  All the code
      is heavily commented and should explain all of the aspects of
      polymorphism and inheritance we haven't talked about yet.
       
      The assignment is to write two new classes:
       
      Rectangle
      Square
       where Rectangle is a conrete subclass of Shape and Square is a
      concrete subclass of Rectangle.  So Rectangle should look a lot
      like circle except it will have two data members (_length and
      _width) and the functions to compute perimeter and area will be
      different.  Square will only require a small amount of
      functionality.  In fact, all you'll have to do is implement a
      constructor that takes one argument for the side length, set
      both the _width and _length vars to it and then overload the
      getDescription() function.
 I plan on spending class on wednesday talking about the
      assignment and definitely stop by my office hours if you have
      questions.
       
      To compile the code on OIT, use:
       
      % g++ Shape.cc Circle.cc Main.cc
      when you are done with you're Rectangle and Square classes, use:
      % g++ Shape.cc Circle.cc Main.cc Rectangle.cc Square.cc
      You'll get a binary executable called a.out
      
            
      
      The assignment is due Friday, 26 January 2001 at 17:00.
 
       
      If you're looking for a good C++ reference, you should pick up
      The C++ Programming Language by the creator of C++, Bjarne Stroustrup.  I
      have a copy and will reserve a copy in the physical sciences
      library.  Bjarne matains a list of
      links to various C++ resources such as faqs, tutorials, and
      references.  Maybe you'll find them useful.
      
       |