Second exam: April 21?? -- covers chapters 4-6.
Third project due: Tuesday, April 26th.
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/.
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;
}
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.
-- 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.