CS401 - Self-Check Quiz #3

REMINDER:  YOU SHOULD TRY THESE ON YOUR OWN FIRST!  JUST READING THE SOLUTIONS WILL DO VERY LITTLE TO HELP YOU LEARN THE MATERIAL!

Question 1:  Short Answer

Answer the following questions in two or three sentences each.  Give examples when indicated.
  1. What is separate compilation?  Identify one benefit.
  2. Separate compilation is used when you write your class header, class method definitions, and the program that uses the class all in separate files.  The class (the header and method files) can then be compiled on their own.  Perhaps the biggest benefit is the fact that you can take the header file and compiled class source code file and use it other projects with minimal work.  See the handout on separate compilation for more.
  3. Why is it generally a good idea to make data members private?  Give an example of a small class and demonstrate an illegal use of a private data member.
  4. It is generally good to make data items private because you want to adhere to the concept of information hiding.  Data items should not be readily available for a user of the class because they may misuse the data.  Rather, they should use the public methods to interract with the class. 

    EXAMPLE OF MISUSE:

    class Sample {
      private:
        int x;
      public:
        int getX() const { return x; }
    };

    ...

      Sample s;

      s.x = 100;     // illegal, x is private
     

  5. How are arrays and classes different?  Give at least two non-trivial differences (e.g., saying the syntax is different is not good enough).
  6. Arrays hold some number of elements that are all of the same type, whereas classes allow you to have as many items as you need, which can be of different types.  Also, the class construct is used to create a new type, whereas an array does not introduce a new type, you just delcare it when you need one.
  7. What is a constant parameter?  What purpose do the serve?
  8. A parameter that uses the const modifier.  It is a guarantee that the function/method will not modify the argument passed into the function.  The purpose is so that someone calling the function/method will know that their object is safe from change.

Question 2:  Problems from the book

Do the following problems from the book:
  • 8.6 (p.396)
  • The problem did not specify how far to go with these, so I just wrote the data members, a constructor, and accessors for each. 

    // Circle
    class Circle {
      private:
        float x, y, radius;    // (x,y) is the center of the circle
      public:
        Circle(float,float,float);  // rad,x,y
        float getRadius() const;
        float getX() const;
        float getY() const;
    };
     

    // Rectangle
    class Rectangle {
      private:
        float x, y, height, width;    // (x,y) is the upper left corner
      public:
        Rectangle(float,float,float,float);  // x,y,ht,wdth
        float getHeight() const;
        float getWidth() const;
        float getX() const;
        float getY() const;
    };

    // Ellipse
    class Ellipse {
      private:
        float x, y, radUp, radAcross;  // (x,y) is the center
                                       // radUp and radAcross is the radius up, and over
      public:
        Ellipse(float,float,float,float);  // x,y, rup, raccross
        float getRadUp() const;
        float getRadAcross() const;
        float getX() const;
        float getY() const;
    };

    // Triangle
    //  (there should probably be methods to get the lengths of the three sides, but this
    //  can be easily added by writing methods that use the three points for the calculations)
    class Circle {
      private:
        float x1, y1, x2, y2, x3, y3;    // (x1,y1) top, (x2,y2) LL corner, (x3,y3) LR corner
      public:
        Triangle(float,float,float,float,float,float);  // same order as data members
        float getTopX() const;
        float getTopY() const;
        float getLLX() const;
        float getLLY() const;
        float getLRX() const;
        float getLRY() const;
    };

    // Quadrilateral
    // just do it like a triangle, but add one more point and two more accessors.
    // again, it would be good idea to have methods that calculated the lengths of
    // the sides.
     

  • 9.1 (p.458)
  • A class is not automatically an ADT, you have to define a full set of interface methods that allow it to be used such that the user of the class need not be aware of the details of the implementation.
  • 9.23 (p.459 - 460)
  • (a) there are four member functions, counting the constructor.  The four are Widget(), Widget(int), GetValue(), and SetValue().
    (b) two.  Flag and DataItem.
    (c) SetValue() is not a constructor because its name is not the same as the class.
    (d) SetValue() is defined as a protected data member, not public.
    (e) The public member functions can access DataItem, that is the point.  They offer controlled access to the private/protected section.  Users of the class must use the member functions, which in turn work on the data members on behalf of the user of the class.
    (f) Clients (aka, users of the class) can use Flag because it is declared in the public section of the class.  This violates the principle of information hiding.
    (g) Nope!  See (f).  Not all data members are declared as private or protected.
  • 9.24 (p.460)
  • There is an error in the second constructor, it should read A(int &n);

    Class A has two data members by the name A3 (one in the public section and one in the private).

    The definition of the constructor for class B has no parameters in the header, but in the method definition there are three.

    The creation of the the A object in the last line of the constructor for B (last line of the code segement) tries to use 3 arguments, but there is no corresponding constructor for this.

  • 10.18 (p.550)
  • int A[100]  produces 100 integers

    float B[25][30]  produces 25 X 30, or 750 floats

    char C[9][4][4]  produces 9 X 4 X 4, or 144 characters

    Rational D[2]  produces 2 Rational objects, and since each Rational number consists of two integers, you get 4 integers (of course, D cannot be used like a 4-element integer array, but that's how much space is allocated).

  • 10.29 (p.552)
  • const int MaxSize = 20;         // (a)

    vector<int> List(MaxSize,1);    // (b)

    A.front() = 19;                 // (c)

    A.back() = 54;                  // (d)

    for (int i=1; i<MaxSize-1; i++) // (e)
       A[i] = 0;

    // skip (f), it doesn't apply to us

    (g) (h)  3.1415 is neither a valid element value or index value, both must be integers.

  • 13.10 (p.707)
  • A will have access to Top(), look(), and peek().  It will not be able to use value, other than through calls to look() and peek().


Question 3:  Output
 

#include <iostream>
#include <string>
using namespace std;

class A { 
  protected:
    int x;
    int z;
  public:
    void setX(int i) { x=i; }
    int getX() const { return x; }
    int getZ() const { return z; }
};

class B : public A {
  private:
    int y;
  public:
    B() { y = 0; }
    void bumpY() { y++; }
    int getY() const { return y; }
    int update() { z = y + x; }
};

int main() {
  A a;
  B b1, b2;

  cout << b1.getY() << ' ' << b2.getY() << endl;
  a.setX(10);
  b1.setX(20);
  b1.update();
  b2.setX(30);
  b2.update();
  cout << b1.getX() << ' ' << b1.getY() << ' ' << b1.getZ() << endl;
  cout << b2.getY() << ' ' << b2.getY() << ' ' << b2.getY() << endl;
  b1.bumpY();
  b1.update();
  b2.bumpY();
  b2.bumpY();
  b2.update();
  a.setX(b1.getY() + b2.getZ());
  cout << a.getX() << endl;
  cout << b1.getX() << ' ' << b1.getY() << ' ' << b1.getZ() << endl;
  cout << b2.getY() << ' ' << b2.getY() << ' ' << b2.getY() << endl;

  return 0;
}

// OUTPUT
0 0
20 0 20
0 0 0
33
20 1 21
2 2 2
 

You should draw yourself some pictures to help you get through this one. 
 

Question 5:  Write C++ code

  1. Write a class header (don't implement the methods) that represents an address.  You should have fields for the house number, street name, city, state, and zip code.  Also, declare an array of 100 of these objects.
  2. // this requires #include <string>
    // Again, no specifications were given regarding the methods, so I just included
    // the typical ones.

    class Address {
       private:
          int houseNum;
          string strName, city, state;
          long zip;
       public:
          Address();
          Address(int, string, string, string, long);  // same order as above

          int getHNum() const;
          string getStrName() const;
          string getCity() const;
          string getState() const;
          long getZip() const;

          void print() const;
          void read();
    };
     

    // an array of 100 Addresses
    Address addrBook[100];
     

  3. Write a function called countHigh() to return the number of items in an integer array all above some other number.  It should have the array, the length of the array, and the lower bound on the integer as its parameters.  For example, suppose the array myArray has 10 elements equal to {4, 8, 3, 1, 8, 9, 3, 10, 0, 2 }.  The call  count = countHigh(myArray, 10, 5) would leave count holding 4.
  4. int countHigh(int a[], int length, int lowBound) {
       int hcount=0;
       for (int i=0; i<length; i++) {
          if (a[i] > lowBound)
             hcount++;
       }
       return hcount;
    }
  5. Add the operator += to the Rational class. 
  6. We won't really add it to the class as a member function, but we can make the functionality available like this:

    Rational operator+=(Rational &r, const Rational &s) {
       r = r + s;
    }

    CLARIFICATION:  This calls the already-defined + (as defined on page 423), which returns a new Rational object, the sum of r and s.  Then, an assignment statement stores that result into r, which is the argument appearing on the left side of the original call to +=.  Notice that r was not declared as a const parameter, this method would not compile if it were.

Last Updated: 7/27/01 by H. Chad Lane, hcl@cs.pitt.edu
© 2000-2001 Jim Skrentny, University of Wisconsin