CSE 283 Introduction to Object Oriented Design

Barbara Nostrand, Ph.D.

CSE 283 Labs
Welcome to the site for CSE 283 Labs!


Lab 10: C++ Project

Prev | Next | Lab 10


  1. Practice writing programs in C++. 
  2. Gain experience with arrays and vectors in C++. 
  3. Gain experience with files in C++. 
  4. Practice using C++ I/O facilities. 


C++ provides the cassert mechanism for checking preconditions.  This mechanism is contained in the cassert library which must be included in order to use this mechanism.  The assert function is passed a boolean value.  Execution procedes normally if this value is true.  However,  the program is immediately terminated if this value is false.  A diagnostic message is automatically displayed on the console device if the assertion fails. 

Classes in C++

As in Java,  classes in C++ provide a way to group together attributes and functionality within a single "wrapper".  Also,  our naming convention remains pretty much the same with class names begining with a capital letter while the names of attributes and methods (called "functions" in C++) begin with lower case letters.  There are,  however important differences in both syntax and organization between the two languages.  The most important organizational difference is that C++ allows multiple inherittance whereby you can declare a single class which extends multiple parent classes.  The designers of Java eliminated this feature, and Java supports on single inherittance.  The advantage of single inherittance is that there can be no confusion about which attributes and methodsd are being propogated to the class being defined. 

We will now take a look at how to declare classes in C++.  Like Java,  C++ has implements access control features such as public.  Here is an example of a C++ class with public attribute variables

    class Car
      string itsName;
      int itsDoors;
      int itsCylinders;
      int itsEngineSize;
Notice that C++ provides a mechanism for specifying the accessibility of a block of variables while Java attaches accessibility to each variable declaration statement.  However,  just as in Java,  attributes in C++ are accessed by dot notation

Methods in C++

C++ provides special syntax for associating methods (functions) with classes and objects when the methods are being defined.  Here we define a simple print() method for our Car class.  Notice the use of the double colon separating the class name and the name of the method (function) being defined> 

    void Car::print() {
      cout << itsName << '\n'
           << "Doors: " << itsDoors << '\n'
           << "Cylinders: " << itsCyclinders <<  '\n'
           << "Engine size: " << itsEngineSize << endl;

Format Control in C++

Although you can use the prinffprintfscanf,  and fscanf functions inheritted from the C language,  Jerry Schwarz used classes to develop a better I/O system for C++.  Among other things,  Schwarz developed a two new operators:  >> the input operator or extraction operator and << the imput operator.  These are actually macros which expand into stream methods which accept streams as parameters and return them as values while simultaneously interacting with variables and I/O devices.  You can control formatting of the iostreams by inserting format manipulator constants into the stream you wish to control.  For example: 

    int i = 17;                  // decimal integer 17
    cout << showbase             // display base when outputing integers
    << oct << i << endl          // display 17 as an octal value:       21o
    << dec << i << endl          // display 17 as a  decimal value:     17d
    << hex << i << endl;         // display 17 as a  hexadecimal value: 11x

Format ManipulatorDescription
fixedUse fixed-point notation for real values. 
scientificUse scientific notation for real values. 
showpointShow decimal point and trailing zeros for whole real numbers. 
noshowpointHide decimal point and trailing zeros for whole real numbers.
decUse base-10 notation for integer input or output. 
hexUse base-16 (hexidecimal) notation for integer input or output. 
octUse base-8 (octal) notation for integer input or outpu. 
showbaseDisplay integer values indicating their base (e.g.,  0x for hex). 
noshowbaseDisplay integer values without indicating their base. 
showposDisplay + sign for positive values. 
nowhosposDo not display + sign for positive values. 
boolaphaRead or display bool values as true or false
noboolaphaDo not read or display bool values as true or false
uppercaseIn scientific,  use E; in hexadecimal,  use symbols A-F. 
nouppercaseIn scientific,  use e; in hexadecimal,  use symbols a-f. 
flushWrite contents of stream to screen (or file). 
endlInsert newline character into output stream and flush the stream. 
leftLeft justify displayed values;  pad with fill character on the right. 
rightRight justify displayed values;  pad with fill character on the left. 
internalPad with fill characdter between sign or base and value. 
skipwsSkip white space on input. 
noswkipwsDo not skip white space on input. 
setprecision(n)Sets the number of decimal places to n
setw(n)Sets the width of the next field only to be equal to n

I/O Failure in C++

While Java handles I/O errors by throwing exceptions,  C++ tends to handle I/O errors by setting or clearing internal state flags.  There are three commonly used boolean valued accessor methods (functions) for an istream (input stream) in C++ 

MessageReturns True if and only if
cin.good()all is well in the istream
cin.bad()something is wrong with the istream
cinl.fail()the last operation could not be complelted


Implement a version of PigLatin in C++.  PigLatin will prompt the user for the file name of the source file in English and for the name of the destination file to receive the translation.  The default file extention in both cases should be .txt.  Begin by downloading meteorology.cpp and study simple stream-oriented file I/O in C++.  test1.dat is a simple test file containing five temperatures.  Observe that the program uses specialized C++ macros to simplify text-oriented I/O. These macros actually expand to rather ordinary method calls.  While it is possible to construct a structured sequential program in C++,  your C++ program should be object oriented.  Remember that in addition to constructors,  C++ has destructors which are executed when an object is garbage collected

Don't forget to clean up your directory when you are all finished. 

Prev | Next | Lab 10




Lab 0

Lab 1

Lab 2

Lab 3

Lab 4

Lab 5

Lab 6

Lab 7

Lab 8

Lab 9

Lab 10

Lab 11

Lab 13

Last modified: 2007 NOV 12