cmsc 341 homework 1 - version a (1)

4
CMSC 341 Homework 1 – Version A Inheritance and Cout Overloading When creating a project, the class twoDimension below was created to fit that need. class twoDimension { private: int x, y; // x and y coordinates public: // inline implementation of constructor twoDimension(int i, int j):x(i), y(j){} // this is an initializer list // inline implementation of member functions void setX(int NewX){x = NewX;} void setY(int NewY){y = NewY;} int getX() const {return x;} int getY() const {return y;} }; But as projects grow, so do the clients wants and needs. The client now wants to plot 3D points. A rookie programmer began creating the threeDimensional class below. class threeDimension { private: int x, y, z; // x, y, z coordinates public: // inline implementation of constructor threeDimension(int i, int j, int k):x(i), y(j), z(k){} // inline implementation of member functions void setX(int NewX){x = NewX;} void setY(int NewY){y = NewY;} void setZ(int NewZ){z = NewZ;} int getX() const {return x;} int getY() const {return y;} int getZ() const {return z;} }; But you knowing better, you can use inheritance instead to reduce coding AND be able reuse the 2D class.

Upload: nicholas

Post on 26-Sep-2015

21 views

Category:

Documents


2 download

DESCRIPTION

Homework cmsc341

TRANSCRIPT

CMSC 341 Homework 1 Version A

Inheritance and Cout Overloading

When creating a project, the class twoDimension below was created to fit that need.

class twoDimension{ private: int x, y; // x and y coordinates public: // inline implementation of constructortwoDimension(int i, int j):x(i), y(j){} // this is an initializer list// inline implementation of member functionsvoid setX(int NewX){x = NewX;}void setY(int NewY){y = NewY;}int getX() const {return x;}int getY() const {return y;}};

But as projects grow, so do the clients wants and needs. The client now wants to plot 3D points. A rookie programmer began creating the threeDimensional class below.

class threeDimension{ private: int x, y, z; // x, y, z coordinates public: // inline implementation of constructorthreeDimension(int i, int j, int k):x(i), y(j), z(k){} // inline implementation of member functionsvoid setX(int NewX){x = NewX;}void setY(int NewY){y = NewY;}void setZ(int NewZ){z = NewZ;}int getX() const {return x;}int getY() const {return y;}int getZ() const {return z;}};

But you knowing better, you can use inheritance instead to reduce coding AND be able reuse the 2D class.

1. Using twoDimension as the base class, change the threeDimension class to inherit twoDimension, making sure there are no redundancies between the two classes.

2. Remove ALL inline implementations of member functions and place them in corresponding source files of twoDimension.cpp and threeDimension.cpp.

3. Create the Driver.cpp that will contain your main. Create ONE instance named firstPt of a threeDimension(al) point of values 3,4,5 respectfully in the main.

4. In the class and source files for threeDimension, create an overloaded cout friend function that will display the values x, y, and z as such:

This 3D point has the values of:x: 3y: 4z: 5---

(Yes, include the dashes)

5. From the main, use the overloaded cout to display firstPt.

6. Create a DYNAMIC array of 10 threeDimension(al) points named points. (The array gets the "new" and this means there has to be some constructor work for both objects.) Use a loop and a random generator to create values for all 10 points. The range of possible values will be from -9 to 9 inclusive. As a suggestion, you MANY want to increase the size from 10 to 100000 temporarily, to make sure some of the functions below work.

7. Create a static function displayPoints in the threeDimension(al) class that will pass by reference:a. accept the array of threeDimension(al) points and length of the arrayb. display all Points in the array using the overloaded cout.Prove this works by calling this function from the main, using points.

8. Create a static function sameXYZ in the threeDimension(al) class that will pass by reference:a. accept the array of threeDimension(al) points and length of the arrayb. return a POINTER of the FIRST occurrence of a point that has the same value for x, y and z. (What happens if it does not find one?)Prove this works by calling this function from the main, using points, and displaying the point that has the same x, y, and z values (x == y == z of a single instance). If one is NOT found, display that a point was not found with the same x, y, and z values.

9. Now its time to review the CMSC 341 Coding Standards. Overall they are very similar to CMSC 202, but there are a few minor changes. Please read the WHOLE document, and notice the highlighted portions.

www.csee.umbc.edu/courses/undergraduate/341/spring15/projects/coding-standards.shtml

Make sure to fix anything that does not meet the standards, OTHER than setters and getters and where they should be placed. For this assignment, they will be placed in a source file. Also make sure that the size of the array is back to 10 if you had changed the size. It will be graded.

10. In your home directory On GL, make the directory ./cmsc341/HW1/src . Transfer all of the code given to your directory (inside src). Create a make file that will compile, run and clean your code. Make sure ALL aspects run.

11. Submit your code. We are not using CVS. Remember to use the link you created in Project 0. It should look like this.

cd ../HW1cp -r src ~/cs341proj/HW1/

12. Check your submission which is located on your 341 Coordinators GL directory. (It will look something like afs/umbc.edu/users/s/l/slupoli/cs341/proj0/) Go to the shared directory for Homework 1 and make sure that your program runs correctly there:cd ~/cs341proj/HW1/srcmakemake runmake cleanMake sure ALL of your files are inside the src directory.

Note: The submission directory is only for submission when you are complete. Using the submission directory to work on your files is an abuse of the directory and will be monitored and shut down by OIT if there is abnormal use of the directory.