Week 1. In Review

You have finished your first week of learning how to program in C++. You should feel comfortable entering programs and using your editor and compiler. Now that you have some experience using C++, it is time for a more robust program. The following program pulls together many of the topics you have learned over the previous seven days’ lessons.

After you look through Listing R1.1, you will see that analysis has been included. You will find that every topic in this listing has been covered in the preceding week’s lessons. You will see similar Weeks in Review after Weeks 2 and 3.

Listing R1.1. Week 1 in Review Listing

Image

 1: /* Listing: WR01.cpp
  2:  * Description: Week in Review listing for week 1
  3:  *===============================================*/

Image

 4: #include <iostream>

Image

 5: using namespace std;
  6:

Image

 7:  enum CHOICE {
  8:              DrawRect = 1,
  9:             GetArea,
10:            GetPerim,
11:            ChangeDimensions,
12:            Quit  };
13:

Image

14: // Rectangle class declaration

Image

15: class Rectangle
16: {

Image

17:   public:
18:        // constructors
19:        Rectangle(int width, int height);
20:        ~Rectangle();
21:
22:        // accessors
23:        int GetHeight() const { return itsHeight; }
24:        int GetWidth() const { return itsWidth; }
25:        int GetArea() const { return itsHeight * itsWidth; }
26:        int GetPerim() const { return 2*itsHeight + 2*itsWidth; }
27:        void SetSize(int newWidth, int newHeight);
28:
29:        // Misc. methods
30:

Image

31:   private:

Image

32:         int itsWidth;
33:        int itsHeight;
34: };
35:
36: // Class method implementations
37: void Rectangle::SetSize(int newWidth, int newHeight)
38: {
39:      itsWidth = newWidth;
40:      itsHeight = newHeight;
41: }
42:
43: Rectangle::Rectangle(int width, int height)
44: {
45:      itsWidth = width;
46:      itsHeight = height;
47: }
48:

Image

49: Rectangle::~Rectangle() {}
50:

Image

51: int DoMenu();
52: void DoDrawRect(Rectangle);
53: void DoGetArea(Rectangle);
54: void DoGetPerim(Rectangle);
55:

Image

56: /*===============================================*/
57: int main()
58: {
59:    // initialize a rectangle to 30,5

Image

60:    Rectangle theRect(30,5);
61:
62:    int choice = DrawRect;

Image

63:    int fQuit = false;
64:

Image
65:    while (!fQuit)
66:    {

Image

67:      choice = DoMenu();

Image

68:      if (choice < DrawRect || choice >  Quit)
69:      {
70:         cout << " Invalid Choice, try again. ";
71:         cout << endl << endl;

Image

72:         continue;
73:      }

Image

74:      switch (choice)
75:      {

Image

76:        case  DrawRect:
77:           DoDrawRect(theRect);
78:           break;
79:        case GetArea:

Image

80:           DoGetArea(theRect);
81:           break;
82:        case GetPerim:
83:           DoGetPerim(theRect);
84:           break;
85:        case ChangeDimensions:

Image

86:           int newLength, newWidth;
87:           cout << " New width: ";
88:           cin >> newWidth;
89:           cout << "New height: ";
90:           cin >> newLength;

Image

91:           theRect.SetSize(newWidth, newLength);
92:           DoDrawRect(theRect);
93:           break;
94:      case Quit:

Image

95:           fQuit = true;
96:           cout << " Exiting... " << endl << endl;
97:           break;

Image

98:        default:
99:           cout << "Error in choice!" << endl;
100:           fQuit = true;
101:           break;
102:      }   // end switch
103:    }     // end while

Image

104:    return 0;
105: }       // end main
106:

Image

107: int DoMenu()
108: {

Image

109:    int choice;

Image

110:    cout << endl <<   endl;        //  create two new lines
111:    cout << "       *** Menu *** "     << endl;
112:    cout << "(1) Draw Rectangle"   << endl;
113:    cout << "(2) Area"                     << endl;
114:    cout << "(3) Perimeter"             << endl;
115:    cout << "(4) Resize"                  << endl;
116:    cout << "(5) Quit"                      << endl;
117:

Image

118:   cin >> choice;
119:   return choice;
120: }
121:
122:  void DoDrawRect(Rectangle theRect)
123:  {

Image

124:    int height = theRect.GetHeight();
125:    int width = theRect.GetWidth();
126:

Image

127:    for (int i = 0; i<height; i++)
128:    {
129:      for (int j = 0; j< width; j++)
130:         cout << "*";
131:      cout << endl;
132:    }
133:  }
134:
135:

Image

136:  void DoGetArea(Rectangle theRect)
137:  {

Image

138:    cout << "Area: " <<  theRect.GetArea() << endl;
139:  }
140:

Image

141:  void DoGetPerim(Rectangle theRect)
142:  {
143:    cout << "Perimeter: " <<  theRect.GetPerim() << endl;
144:  }
145: // ==========   End of Listing   ==========

Image


*** 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...

Image

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. If you are confused by any of the lines in this listing, you should go back and review the previous week’s material. To the left of many of the lines are references to which day that line’s primary function is covered.

This program presents a text menu and waits for you to make a selection. The menu works with a rectangle. You have options to print out a representation of the rectangle as well as options to get its area and perimeter. You can also change the default values for the rectangle. The menu does not do all of the error checking that a full-fledged program should do; however, it does do some checking.

On lines 7–12, the program listing sets up the new types and definitions that will be used throughout the program.

Lines 15–34 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 37–47 contain the class function definitions that were not declared inline. Because a constructor was created on lines 43–47, a destructor is also created on line 49.

The function prototypes, for the nonclass member functions, are on lines 51–54, and the entry point of the program begins on line 57. As stated, 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 63, and as long as the flag is set to false, the menu loop continues. The flag is only set to true if the user chooses Quit from the menu.

Each of the other choices, with the exception of ChangeDimensions, calls a function. This makes the switch statement on lines 74–102 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, “Understanding Pointers,” and Day 10, “Working with 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 68, the user’s choice is checked to be certain 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.

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.7.131