Lemur zaprasza
In Review Listing R1.1. Week 1 in Review listing. 1: #include <iostream.h> 2: 3: typedef unsigned short int USHORT; 4: typedef unsigned long int ULONG; 5: enum BOOL { FALSE, TRUE}; 6: enum CHOICE { DrawRect = 1, GetArea, 7: GetPerim, ChangeDimensions, Quit}; 8: // Rectangle class declaration 9: class Rectangle 10: { 11: public: 12: // constructors 13: Rectangle(USHORT width, USHORT height); 14: ~Rectangle(); 15: 16: // accessors 17: USHORT GetHeight() const { return itsHeight; } 18: USHORT GetWidth() const { return itsWidth; } 19: ULONG GetArea() const { return itsHeight * itsWidth; } 20: ULONG GetPerim() const { return 2*itsHeight + 2*itsWidth; } 21: void SetSize(USHORT newWidth, USHORT newHeight); 22: 23: // Misc. methods 24: void DrawShape() const; 25: 26: private: 27: USHORT itsWidth; 28: USHORT itsHeight; 29: }; 30: 31: // Class method implementations 32: void Rectangle::SetSize(USHORT newWidth, USHORT newHeight) 33: { 34: itsWidth = newWidth; 35: itsHeight = newHeight; 36: } 37: 38: 39: Rectangle::Rectangle(USHORT width, USHORT height) 40: { 41: itsWidth = width; 42: itsHeight = height; 43: } 44: 45: Rectangle::~Rectangle() {} 46: 47: USHORT DoMenu(); 48: void DoDrawRect(Rectangle); 49: void DoGetArea(Rectangle); 50: void DoGetPerim(Rectangle); 51: 52: void main () 53: { 54: // initialize a rectangle to 10,20 55: Rectangle theRect(30,5); 56: 57: USHORT choice = DrawRect; 58: USHORT fQuit = FALSE; 59: 60: while (!fQuit) 61: { 62: choice = DoMenu(); 63: if (choice < DrawRect || choice > Quit) 64: { 65: cout << "\nInvalid Choice, please try again.\n\n"; 66: continue; 67: } 68: switch (choice) 69: { 70: case DrawRect: 71: DoDrawRect(theRect); 72: break; 73: case GetArea: 74: DoGetArea(theRect); 75: break; 76: case GetPerim: 77: DoGetPerim(theRect); 78: break; 79: case ChangeDimensions: 80: USHORT newLength, newWidth; 81: cout << "\nNew width: "; 82: cin >> newWidth; 83: cout << "New height: "; 84: cin >> newLength; 85: theRect.SetSize(newWidth, newLength); 86: DoDrawRect(theRect); 87: break; 88: case Quit: 89: fQuit = TRUE; 90: cout << "\nExiting...\n\n"; 91: break; 92: default: 93: cout << "Error in choice!\n"; 94: fQuit = TRUE; 95: break; 96: } // end switch 97: } // end while 98: } // end main 99: 100: 101: USHORT DoMenu() 102: { 103: USHORT choice; 104: cout << "\n\n *** Menu *** \n"; 105: cout << "(1) Draw Rectangle\n"; 106: cout << "(2) Area\n"; 107: cout << "(3) Perimeter\n"; 108: cout << "(4) Resize\n"; 109: cout << "(5) Quit\n"; 110: 111: cin >> choice; 112: return choice; 113: } 114: 115: void DoDrawRect(Rectangle theRect) 116: { 117: USHORT height = theRect.GetHeight(); 118: USHORT width = theRect.GetWidth(); 119: 120: for (USHORT i = 0; i<height; i++) 121: { 122: for (USHORT j = 0; j< width; j++) 123: cout << "*"; 124: cout << "\n"; 125: } 126: } 127: 128: 129: void DoGetArea(Rectangle theRect) 130: { 131: cout << "Area: " << theRect.GetArea() << endl; 132: } 133: 134: void DoGetPerim(Rectangle theRect) 135: { 136: cout << "Perimeter: " << theRect.GetPerim() << endl; 137: } Output: *** Menu *** (1) Draw Rectangle (2) Area (3) Perimeter (4) Resize (5) Quit 1 ****************************** ****************************** ****************************** ****************************** ****************************** *** Menu *** (1) Draw Rectangle (2) Area (3) Perimeter (4) Resize (5) Quit 2 Area: 150 *** Menu *** (1) Draw Rectangle (2) Area (3) Perimeter (4) Resize (5) Quit 3 Perimeter: 70 *** Menu *** (1) Draw Rectangle (2) Area (3) Perimeter (4) Resize (5) Quit 4 New Width: 10 New height: 8 ********** ********** ********** ********** ********** ********** ********** ********** *** Menu *** (1) Draw Rectangle (2) Area (3) Perimeter (4) Resize (5) Quit 2 Area: 80 *** Menu *** (1) Draw Rectangle (2) Area (3) Perimeter (4) Resize (5) Quit 3 Perimeter: 36 *** Menu *** (1) Draw Rectangle (2) Area (3) Perimeter (4) Resize (5) Quit 5 Exiting... Analysis: This program utilizes most of the skills you learned this week. You should not only be able to enter, compile, link, and run this program, but also understand what it does and how it works, based on the work you've done this week. The first six lines set up the new types and definitions that will be used throughout the program. Lines 9-29 declare the Rectangle class. There are public accessor methods for obtaining and setting the width and height of the rectangle, as well as for computing the area and perimeter. Lines 32-43 contain the class function definitions that were not declared inline. The function prototypes, for the non-class member functions, are on lines 47-50, and the program begins on line 52. The essence of this program is to generate a rectangle, and then to print out a menu offering five options: Draw the rectangle, determine its area, determine its perimeter, resize the rectangle, or quit. A flag is set on line 58, and when that flag is not set to TRUE the menu loop continues. The flag is only set to TRUE if the user picks Quit from the menu. Each of the other choices, with the exception of ChangeDimensions, calls out to a function. This makes the switch statement cleaner. ChangeDimensions cannot call out to a function because it must change the dimensions of the rectangle. If the rectangle were passed (by value) to a function such as DoChangeDimensions(), the dimensions would be changed on the local copy of the rectangle in DoChangeDimensions() and not on the rectangle in main(). On Day 8, "Pointers," and Day 10, "Advanced Functions," you'll learn how to overcome this restriction, but for now the change is made in the main() function. Note how the use of an enumeration makes the switch statement much cleaner and easier to understand. Had the switch depended on the numeric choices (1-5) of the user, you would have to constantly refer to the description of the menu to see which pick was which. On line 63, the user's choice is checked to make sure it is in range. If not, an error message is printed and the menu is reprinted. Note that the switch statement includes an "impossible" default condition. This is an aid in debugging. If the program is working, that statement can never be reached. Week in Review Congratulations! You've completed the first week! Now you can create and understand sophisticated C++ programs. Of course, there's much more to do, and next week starts with one of the most difficult concepts in C++: pointers. Don't give up now, you're about to delve deeply into the meaning and use of object-oriented programming, virtual functions, and many of the advanced features of this powerful language. Take a break, bask in the glory of your accomplishment, and then turn the page to start Week 2. |