CIS 181 Intro to Comp Sci
Class 18, April 14, 2005

IMPORTANT DATES:

Second exam: April 21?? -- covers chapters 4-6.

Third project due: Tuesday, April 26th.


SAMPLE EXAM

An old exam available in old-test2.ps. A PDF version of the same file can be found at old-test2.pdf.
Some solutions to programming portions of the exam can be found in the directory: $CLASSHOME/exams/old-t2-solutions/.


ASSIGNMENT:

Read: D&D, finish Chapter 6.

Exercises due Tuesday, April 19th: p. 467, 6.12 -- rectangle class.  You can use the following driver to test your class. The driver program can be found here.

// Driver program to test rectangle class
// problem 6.12 in D&D

#include <iostream.h>
#include "rectangle.h"

main ()
{
float l, w;
char again;
rectangle r, rand1;

do
{
cout << "Input length and width of rectangle: ";
cin >> l >> w;
r.setrectangle(l, w);
rand1.setlength(l-1);
rand1.setwidth(w-1);

cout << endl;
cout << "The rectangle r:" << endl;
r.printlw();
r.printrec();
cout << "The perimeter of r is: " << r.perimeter() << endl;
cout << "The area of r is: " << r.area() << endl;

 
cout << endl;
cout << "The rectangle rand1:" << endl;
rand1.printlw();
rand1.printrec();
cout << "The perimeter of rand1 is: " << rand1.perimeter() << endl;
cout << "The area of rand1 is: " << rand1.area() << endl;
cout << endl;

cout << endl;
cout << "Setting rectangle rand1 to be the same as original rectangle r\
"
<< endl;
rand1 = r;
cout << "The rectangle rand1 is now:" << endl;
rand1.printlw();
rand1.printrec();
cout << "The perimeter of rand1 is: " << rand1.perimeter() << endl;
cout << "The area of rand1 is: " << rand1.area() << endl;
cout << endl;

cout << "Would you like to try again? (y/n) ";
cin >> again;
} while (again == 'y');

return 0; }



TODAY'S TOPICS
  1. More introduction to C++ classes
    See the file $CLASSHOME/examples/part-rec.cc and the files in the directory
  2. More on C++ classes
    Two important ideas:
    1. classes can contain "methods" in the form of member functions as well as data.
    2. classes have "public" and "private" parts that allow us to build shells around the implementation and provide access to the class through a (hopefully) nicely designed user interface.
  3. Another example: the Time class
    See the files in the directory $CLASSHOME/deitel-files/examples/ch06/fig6_10
  4. A case study: exercise 6.7 creating a Rational number class.
    See the files: $CLASSHOME/examples/rational-main.cc, $CLASSHOME/examples/rational.h, $CLASSHOME/examples/rational.cc
  5. In introducing classes, we are beginning an important transition from "programming in the small" to "programming in the large." So far, we have introduced programming language constructs that are useful for writing small programs (although they have been used to write some very large programs). The features (or their equivalents) that we have discussed before introducing classes are found in most common programming languages. C++ is an (imperfect) attempt to provide a programming language that provides good tools for writing, maintaining, and enhancing large programs. That is, programs (actually software systems) that are hundreds, or even thousands, of pages long and take many years to complete.

    We are also moving from procedure or function oriented programming (i.e., what functions should our program provide? --- for example, sorting automobile data) to object oriented programming (i.e., what objects should our programs implement? --- for example, bank accounts). For the latter, we use data to represent the actual objects but also include the functions, or methods, that are natural for such objects like making deposits, withdrawing money, printing the information, etc. This change in point of view is subtle, but powerful. The power of object oriented thinking becomes apparent when very large programs are used over long periods of time.

  6. Some points about classes:

    -- Keep data members private, separating interface and implementation

    -- use a header file for the class declaration (the header to be included by any client who wants to use the class) and a source file for function definitions (the source file to be compiled with client program, or its object code to be linked in with client ).

    -- provide set functions to set private data. These functions can ensure that data is always in a valid state.

    -- provide get functions to retrieve private data. Thus, implementation can change and not affect client programs.

    -- constructor functions can be given default values.

    -- multiple constructors can exist (with different argument types) thus further shielding client from implementation.

    -- my preference: in class declaration always use class member access specifiers (i.e., do not rely on default private, make it explicit!). Put public: member items first and include good documentation in the header file. Client programmers need to know about the public members, and have access to the header file (and perhaps nothing else) to understand your class.