CS401 - Self-Check Quiz #3

Give yourself about 45 minutes to do these problems.  Try not to look at your notes or the book until you're done.  Solutions will be made available online near the end of the week.
 

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. 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.
  3. How are arrays and classes different?  Give at least two non-trivial differences (e.g., saying the syntax is different is not good enough).
  4. What is a constant parameter?  What purpose do the serve?

Question 2:  Problems from the book

Do the following problems from the book:
  • 8.6 (p.396)
  • 9.1 (p.458)
  • 9.23 (p.459 - 460)
  • 9.24 (p.460)
  • 10.18 (p.550)
  • 10.29 (p.552)
  • 13.10 (p.707)
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;
}
 

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. 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.
  3. Add the operator += to the Rational class.  
Last Updated: 7/2/01 by H. Chad Lane, hcl@cs.pitt.edu
© 2000-2001 Jim Skrentny, University of Wisconsin