Final Exam Review Supplement

<< Back

Review Topics


A. Classes and Objects

  1. Review lesson 10.1.1.
  2. Create a file named "curio.cpp" and then code the declaration for a class that represents a small and unusual object called a "curio". The curio will need a name, price and quantity so we can sell it in a store. (5m)
    1. First, decide on an appropriate name for the class.
    2. Decide what member variables are needed and the appropriate data types.
    3. Decide what constructors are needed to initialize the member variables.
    4. Decide what get- or set- functions are needed to access or update the member variables and write a short justification.
    5. Decide what actions need to be taken by a Curio product object and declare appropriate member functions in addition to get- or set- functions. For example, will the curio need to be able to print its data?
  3. Code the definition of the Curio class. (20m)
    1. Download and save the following files using the specified names. You must add all the code you write to the .cpp file.
    2. In the file named curio.cpp, code a main() function. Compile your code to verify it works.
    3. Using the starter code, write one get() and one set() function for each variable of the Curio class following the naming standards for these types of functions. Remember to add the const keyword to all accessor functions.
    4. Write a default (no-parameter) constructor for the class that sets default values for all the numeric member variables.
    5. Write an overloaded constructor for the class that sets all three member variables to the value of the arguments passed to the parameters.
    6. Write a member function for the class named print() that has no parameters and returns no values, but displays the data of the class all on one line like this:
      China plate         5.99    30    179.70
      

      Remember to add the const keyword to this accessor function.

    7. In your main() function, construct 3 objects of your class, setting type, price and quantity. Call both constructors and all the set functions of your class to construct the objects.
    8. Compile and run your program to verify it works correctly.
  4. Review your code with another student and correct any issues found.

B. Storing objects in a vector

  1. Review lesson 10.2.6 and lesson 10.4.2.
  2. Continue with your previous code in curio.cpp to work with a vector of objects. (8m)
    1. Declare a vector that will store Curio objects.
    2. Add all the objects declared in Topic A to the end of the vector.
    3. Write a function named listData() with a single parameter for a vector of Curio objects..
    4. Within the function, write a for-loop that will list all the Curio objects stored in the vector.
    5. Call the listData() function from main().
    6. Compile and run your program and verify it lists all the curios in the vector.
  3. Review your code with another student and correct any issues found.

C. Designing file read operations

  1. Review lesson 12.3.5.
  2. Create a text file named "curio.txt" that contains an example of the data needed for your Curio store. (3m)
    1. First, decide on what data needs to be stored in the file for each curio.
    2. Decide how to separate each data item in the file.
    3. Then decide the order of the data in the file.

    Do not forget to save the text file in the UNIX format.

  3. When finished, compare your examples with these: curio.txt. Notice that names may contain spaces and plan accordingly.
  4. Write the series of commands to read the data from the "curio.txt" file. (8m)
    1. In main(), declare a file input stream that opens the file curio.txt.
    2. Add code to verify the file opened correctly and that provides and error message and exits if there was an error.
    3. Write a member function for the class named read(ifstream& fin) that has an ifstream parameter passed by reference, reads data from the ifstream object and assigns the data to the three instance variables.
    4. Add a series of commands inside a loop that reads the curio.txt data from the file and loads it into the vector.
    5. Add commands to display the data for the objects in the vector.
    6. Compile and run your program and verify it lists all the curios in the vector.
  5. Review your code with another student and correct any issues found.

D. Designing file write operations

  1. Review lesson 12.3.6.
  2. Write the series of commands to write the data from the vector of objects to a file named "saved.txt". (8m)
    1. After reading from a file works correctly, in the Curio class, add a write function with the following prototype:
      void write(ofstream& fout);
        
    2. Outside the Curio class, add the implementation of the write function using the prototype just added.
      void Product::write(ofstream& fout) {
            // write the product name
            // write the price
            // write the quantity
        }
        

      Write an endl after each output command. See lesson 12.3.6 for more information.

    3. Implement the writeFile() function using the declared prototype from the starter code and the following psuedocode.
      void writeFile(vector<Product>& list, string filename) {
          // open an output file stream
          // test if the stream failed to open
          // for each object in the vector
              // call the write function
          // close the stream after the loop ends
      }
      

      See lesson 12.3.6 for more information.

    4. Call writeFile() from main() like:
      writeFile(list, "saved.txt");
      
  3. Review your code with another student and correct any issues found.

<< Back