Midterm 2 Solution


  1. Short Answer Problems.

    1. (35 pts.) For a class declaration define public, private, protected, constructor, and destructor.

      • Public --- Member functions or variables which any function may access.
      • Protected --- Member functions or variables which only member functions of member function of derived classes may access.
      • Private --- Member functions or variables which only member functions may access.
      • Constructor --- A function called when a class variable comes into scope, usually to initialize the variable.
      • Destructor --- A function called when a class variable goes out of scope, usually to ``tear-down'' the variable.

    2. (20 pts.) Use the typedef statement to define the following two new types and show how to declare a variable of each of the new types.

      1. An int array with 20 elements.
        typedef int array[20];
        array d;
      2. A complex number (use a structure implementation).
        typedef struct Complex
           double re, im;
        } complex;
        complex x;

    3. (20 pts.) Consider the following declaration:
      double data[] = { 1.0, 2.0, 3.0, 4.0 };
      Assume that a double occupies four bytes and that data is allocated starting at address 1000 in memory.

      1. How many elements are contained in data?
      2. What is printed by
        cout << data;
      3. What is printed by
        double* p = data + 1;
        p += 2;
        cout << *p;
      4. What is printed by
        double* p = data;
        p += 2;
        cout << p;

  2. Programming Problems. Each of the following problems is worth 50 points. You are more likely to receive partial credit if you write your code so that I can understand what you've done.

    1. Consider the following implementation of a list:
      const int LIST_MAX = 100;
      class List
         int l[LIST_MAX];   // Holds the items of the list.
         int size;          // Number of items in the list.
         List(void) : size(0) {}
         void ListRetrieve(int index, int& value, int* success);
         int ListLength(void);
         int ListIsEmpty(void);
         // ...

      Implement ListRetrieve() (read the prototype carefully), ListLength(), and

      void List::ListRetrieve(int index, int& value, int *success)
         if (0 <= index && index < size)
            value = l[index];
            *success = 1
            *success = 0;
      int List::ListLength(void)
         return size;
      int List::ListIsEmpty(void)
         return size == 0;

    2. Design a single class which holds geometric objects, in this case either rectangles or circles. Several data members will be required:
      • A flag indicating if the object is a rectangle or circle.
      • If the object is a rectangle, two points. One point is the lower left corner, the other is the upper right corner.
      • If the object is a circle, a point and a radius.
      Use the following structure to represent points:
      struct point
         double x;
         double y;
      Show the class declaration and the definition of the class' constructors (one for creating a rectangle and one for creating a circle) and a public method which computes and returns the area of an object.

      enum ObjectType { CIRCLE, RECTANGLE };
      class Object
         ObjectType type;
         point ll;
         point ur;
         point c;   // Center or circle.  I could have used one of the other
                    // two to save some space.
         double radius;
         // Circle constructor.
         Object(double x, double y, double rad);
         // Rectangle constructor.
         Object(double llx, double lly, double urx, double ury);
         double Area(void);
      Object::Object(double x, double y, double rad)
         type = CIRCLE;
         c.x = x;
         c.y = y;
         radius = rad;
      Object::Object(double llx, double lly, double urx, double ury)
         type = RECTANGLE;
         ll.x = llx;
         ll.y = lly;
         ur.x = urx;
         ur.y = ury;
      double Object::Area(void)
         switch (type)
          case RECTANGLE:
            return (ur.x - ll.x) * (ur.y - ll.y);
          case CIRCLE:
            return PI * radius * radius;
          default:   // This is a major fault.  Terminate.

Thomas P. Kelliher
Tue Apr 23 00:25:44 EDT 1996
Tom Kelliher