CSCI 3310
Spring 2006
Practice questions for quiz 1

  1. Which of the following are part of an abstract data type specification? (Circle the letter of all that apply.)

    1. A type.
    2. Prototypes for functions in the abstract data type.
    3. A description of how the type is represented.
    4. Implementations of the functions.

  2. Suppose that A and B are classes, where B is a subclass of A. Suppose that a is a variable of type A*, and b is a variable of type B*.

    1. Is it allowed to assign a = b?
    2. Is it allowed to assign b = a?

  3. Explain the difference between static method selection and dynamic method selection.

  4. How do you ask for dynamic method selection in a class?

  5. A rational number can be represented by a pair of integers, the numerator and the denominator. For example, the rational number with numerator 1 and denominator 3 stands for 1/3. The advantage is that some numbers that can only be represented approximately using type double can be represented exactly as ratios of integers.

    An abstract data type Rational is intended to represent rational numbers. So a member of type Rational is thought of as having a numerator and a denominator. Among the operations are a constructor that takes two integers (a numerator and a denominator) and initializes a rational number; an operation flip so that x.flip() makes x into its reciprocal; an operation multiplyBy so that x.multiplyBy(y) makes x into the product of x's current value and y, where y is a rational number; an operation getNumerator that returns the numerator, and an operation getDenominator that returns the denominator. (So x.getNumerator() is the numerator of x.)

    Rational numbers are always stored in a form where the numerator and denominator have no common factors. Instead of 3/6, you store 1/2. You cannot assume that somebody who uses this data type knows this convention. There is nothing wrong with creating a rational number with numerator 3 and denominator 6. But if you then ask for the numerator, it will be 1, not 3. This can be achieved by dividing the numerator and denominator by their greatest common divisor. Assume that function gcd(m,n) is available to you. You do not need to write it.

    1. Write class Rational in C++ as a class, including the constructor and methods flip, multiplyBy, getNumerator and getDenominator. Also provide a private method called reduce that reduces a rational number by dividing the numerator and denominator by their greatest common divisor. You will want to use reduce in some other functions.

      Make the variables in this class protected.

    2. Write a main program that creates rational numbers 1/3 and 3/7, calculates the product of those two numbers, and prints the product in the form numerator/denominator. (So it will print "1/7".)

  6. Write a class SumRational that is a subclass of Rational. In addition to the operations of class Rational, it should provide a method add so that r.add(s) adds s and r and returns a new rational number that is the sum. (s and r are both of type SumRational.) The add method should not change the object.