Data Structures and Algorithms I - Lab 2- Nested Classes

Name ______________________________ Section __________ Score ________________

Objectives

To practice using and defining classes with fields that are classes or arrays of classes. To practice separation of implementation and interface.

Background

Classes and structs can contain fields of various types, such as integers, reals, strings, etc. We will practice defining and using nested classes, classes with fields that are themselves classes or arrays of classes.

Experimental Procedure/Analysis

Task 1

Copy the lab files to your desktop and unzip them. Unzipping will create a folder named Lab02. Open the workspace found inside this folder (named Lab02.dsw) in Visual C++.

Build and execute the program. You should not get any errors. The program declares a pair of Point objects. A Point object represents a point on the real plane, so it has two fields (data members), one for the x-coordinate and one for the y-coordinate.

Task 1a

Study the way the main program creates and sets points a and b. Point a is created by the parameterless (default) constructor, then it is given specific coordinates. What values are assigned to the x and y coordinates of Point a when it is created?

Point a's xcoord is _______ and ycoord is _________ (hint: examine the constructor in Point.h)

Prove your assumption by adding a statement to main that will print Point a before its coordinates are set. Leave this line in the program as it will be graded.

Point b is created with specified coordinates using a different constructor. The output should verify this. Both Points are declared in the main program block. 

______ (1, 2, or 3) When are the Points actually created?

  1. before execution begins (at the time the program is loaded)
  2. before the first statement of main is executed
  3. when execution reaches the declaration in the code

Examine the distance method found in the Point class. This uses the standard distance formula to determine the distance between two Points. Notice that the only argument is a Point object, passed by value. In general, it is bad practice to pass objects by value (unless you need to make a copy for some reason). Change the method to receive its argument by constant reference (we want to enforce the principle of least privilege). Document the change by adding a comment just above the function with the date and your initials.

Test the program to be sure you have not changed the behavior.

Task 1b

Is it possible to set only one of the coordinates of a Point object in main? Try it! Uncomment the last few lines of Task 1 code and compile.

There error message tells why you cannot directly set the xcoord member of a Point object. Explain it in your own words:

_________________________________________________________________________
Devise a way to accomplish the goal of changing the x-coordinate only of Point b (by adding code to main only). Replace the incorrect line in main with your solution. You can do this in a single statement (although some may prefer using a sequence of 2-3 statements). Remember: your solution is to change only the x-coordinate of Point b to the number entered by the user. Document your solution with a comment that includes your initials and what you have done.

Task 2

Change the TASK identifier to have the value 2. Build and run the program. (It should run without error.) In this task, we use the PathN class to represent a path of points in the real plane. This object stores the points in an array of length PATHLEN, where PATHLENis a global constant > 0. In what file is this constant defined? What is its value?

Defined in _________________________________ with value ___________

Task 2a

Inspect main and the output. A PathN object is created after main displays two lines of output. What constructor is called four times?

The ___________________ constructor from the class ___________________________

Why is it necessary for this constructor to be called this many times?

_________________________________________________________________________

_________________________________________________________________________

Notice that the object d is a PathN object representing a path (sequence) of Points in the plane. It appears that two of the Points are then set to coordinates (1.0, 2.0) and (3.0, 4.0), yet when the path is displayed, this data does not appear. 

Look at the member function definition for setPoint in pathN.cpp. It is incomplete. Complete it to match the specifications of the comments that are located above the method implementation (be sure to use a constant identifier rather than a "magic number" in your solution) and then run the program again. The output problem should be corrected. Record the output when the Path d is displayed:
_________________________________________________________________________

Task 2b

Uncomment the code in the main program labeled Task 2b. Add a loop that sets every point in the PathN object d to (1.0, 9.0). This loop should be immediately followed by the statement that displays the path. Make sure your loop has no "magic numbers." Write down the path that is printed:

_________________________________________________________________________

Change PATHLEN to 5 (in pathN.h), recompile and run. No errors should occur. Record the last line of output.

_________________________________________________________________________

Change PATHLEN  to 2, recompile and run. Record the line of output that begine "Path d:"

_________________________________________________________________________

Explain what happened to the (3.0, 4.0) Point that was supposedly set in the Path..

_________________________________________________________________________

_________________________________________________________________________

Task 3

Change the TASK identifier to have the value 3. Set the value of PATHLEN to 5. Your task is to write a section of code that will reverse the order of the Points in a PathN object. If the original path is (1,3), (2,7), (0,1), (9,0), (7,5), then the reversed path is (7,5), (9,0), (0,1), (2,7), (1,3). Your program should work for any length path. Test your solution with various values of PATHLEN. When finished, be sure to add your name to the top of all 4 files, print them, and submit them along with your answers to this assignment. Make sure you clean out the Debug folder before saving your project and remove any private files from the lab machine hard drive.