Previous Section
 < Free Open Study > 
Next Section


Exercises

  1. Explain what we mean by "data abstraction."

  2. What is data encapsulation? Explain the programming goal "to protect our data abstraction through encapsulation."

  3. Name three perspectives from which we can view data. Using the logical data structure "a list of student academic records," give examples of what each perspective might tell us about the data.

  4. Consider the abstract data type GroceryStore.

    1. At the application level, describe GroceryStore.

    2. At the logical level, what grocery store operations might be defined for the customer?

    3. Specify (at the logical level) the operation CheckOut.

    4. Write an algorithm (at the implementation level) for the operation CheckOut.

    5. Explain how parts (c) and (d) represent information hiding.

  5. What composite types are predefined in the C++ language?

  6. Describe the component selectors for structs and classes at the logical level.

  7. Describe the accessing functions for structs and classes at the implementation level.

  8. Describe the component selectors for one-dimensional arrays at the logical level.

  9. Describe the accessing functions for one-dimensional arrays at the implementation level.

    1. Declare a one-dimensional array, name, that contains 20 characters.

    2. If each character occupies one "cell" in memory, and the base address of name is 1000, what is the address of the cell referenced in the following statement?

      name[9] = 'A';
      

Use the following declarations for Exercises 11 and 12:

      enum MonthType {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP,
                      OCT, NOV, DEC};
      struct WeatherType
{
        int avgHiTemp;
        int avgLoTemp;
        float actualRain;
        float recordRain;
};

Assume that an int requires one cell in memory, that a float number requires two cells, and that the struct members are found in contiguous memory locations with no gaps.

    1. Declare a one-dimensional array type, weatherListType, of WeatherType components, to be indexed by values of type MonthType. Declare a variable, yearlyWeather, of weatherListType.

    2. Assign the value 1.05 to the actual rainfall member of the July record in yearlyWeather.

    3. If the base address of yearlyWeather is 200, what is the address of the member that you assigned in part (b)?

    1. Declare a two-dimensional array, decadeWeather, of WeatherType components, to be indexed by values of type MonthType in the first dimension.

    2. Draw a picture of decadeWeather.

    3. Assign the value 26 to the avgLoTemp member of the March 1989 entry.

    1. Define a three-dimensional array at the logical level.

    2. Suggest some applications for three-dimensional arrays.

Use the following declarations for Exercises 14-16.

      typedef char String[10];
      struct StudentRecord
      {
        String firstName;
        String lastName;
        int id;
        float gpa;
        int currentHours;
        int totalHours;
      };
      StudentRecord student;
      StudentRecord students[100];

Assume that an int requires one cell in memory, that a float number requires two cells, and that the struct members are found in contiguous memory locations with no gaps.

  1. Construct a member-length-offset table for StudentRecord.

  2. If the base address of student is 100, what address does the compiler generate as the target of the following assignment statement?

    student.gpa = 3.87;
    
  3. How much space does the compiler set aside for students?

  4. Indicate which predefined C++ types would most appropriately model each of the following (more than one may be appropriate for each):

    1. a chessboard

    2. information about a single product in an inventory-control program

    3. a list of famous quotations

    4. the casualty figures (number of deaths per year) for highway accidents in Texas from 1954 to 1974

    5. the casualty figures for highway accidents in each of the states from 1954 to 1974

    6. the casualty figures for highway accidents in each of the states from 1954 to 1974, subdivided by month

    7. an electronic address book (name, address, and phone information for all your friends)

    8. a collection of hourly temperatures for a 24-hour period

  5. What C++ construct is used to represent abstract data types?

  6. Explain the difference between a C++ struct and class.

  7. How is the client prevented from directly accessing the details of an instance of a class?

    1. The details of a private member can be seen by the user of a class. (True or False?)

    2. The details of a private member may be accessed by a client program. (True or False?)

  8. Why is it good practice to put a class declaration in one file and the implementation in another?

  9. Name three ways that classes can relate to each other.

  10. Distinguish between composition and inheritance.

  11. Dintinguish between a base class and a derived class.

  12. Does a derived class have access to the private data members of the base class?

  13. Does a derived class have access to the public member functions of the base class?

    1. Write the specification for an ADT SquareMatrix. (A square matrix can be represented by a two-dimensional array with N rows and N columns.) You may assume a maximum size of 50 rows and columns. Include the following operations:

      MakeEmpty(n), which sets the first n rows and columns to zero

      StoreValue(i, j, value), which stores value into the [i, j] position

      Add, which adds two matrices together

      Subtract, which subtracts one matrix from another

      Copy, which copies one matrix into another

    2. Convert your specification to a C++ class declaration.

    3. Implement the member functions.

    4. Write a test plan for your class.

  14. Enhance StrType by adding a ComparedTo function with the following specification:

    1. Write the member function ComparedTo using strcmp in <cstring>.

    2. Rewrite the member function ComparedTo in the class StrType without using the strcmp library function.

  15. Rewrite the member function CopyString in the class StrType without using the strcpy library function.

  16. Rewrite the specification for the String ADT to include the changes that were made during the implementation phase.

  17. Complete the member functions GetAlpha and GetNonWhite in the class StrType.



Previous Section
 < Free Open Study > 
Next Section
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)