c++練習1


/************
./header/class.hpp
************/

  
  
  
  
  1. #include 
  2. using namespace std; 
  3.  
  4. /** 
  5.  *  1:  
  6.  */ 
  7. class Rectangle { 
  8. public
  9.     //  Rectangle  
  10.     Rectangle(int width, int height); 
  11.  
  12.     //  Rectangle  
  13.     ~Rectangle() {}; 
  14.  
  15.     //  Rectangle  
  16.     int GetHeight() const {return itsHeight;} 
  17.  
  18.     //  Rectangle  
  19.     int GetWidth() const {return itsWidth;} 
  20.  
  21.     //  Rectangle  
  22.     int GetArea() const {return itsHeight * itsWidth;} 
  23.  
  24.     //  Rectangle  
  25.     int GetPerim() const {return 2 * itsHeight + 2 * itsWidth;} 
  26.  
  27.     //  Rectangle  
  28.     void SetSize(int newWidth, int newHeight); 
  29.  
  30. private
  31.     int itsWidth; 
  32.     int itsHeight; 
  33. }; 
  34.  
  35. //  Rectangle  
  36. void Rectangle::SetSize(int newWidth, int newHeight) { 
  37.     itsWidth = newWidth; 
  38.     itsHeight = newHeight; 
  39.  
  40. //  Rectangle  
  41. Rectangle::Rectangle(int width, int height) { 
  42.     itsWidth = width; 
  43.     itsHeight = height; 
  44.  
  45. //   
  46. int DoMenu(); 
  47. //   
  48. void DoDrawRect(Rectangle); 
  49. //   
  50. void DoGetArea(Rectangle); 
  51. //   
  52. void DoGetPerim(Rectangle); 

/************
./src/test1.hpp
************/
 

  
  
  
  
  1. #include ../header/class.hpp 
  2.  
  3. //  , 1  
  4. enum CHOICE { 
  5.     DrawRect = 1, 
  6.     GetArea, 
  7.     GetPerim, 
  8.     ChangeDimensions, 
  9.     Quit 
  10. }; 
  11.  
  12. int main() { 
  13.     //   
  14.     Rectangle theRect(30, 5); 
  15.     //   
  16.     int choice = DrawRect; 
  17.     //   
  18.     bool fQuit = false
  19.  
  20.     while (!fQuit) { 
  21.         //   
  22.         choice = DoMenu(); 
  23.  
  24.         //  ,  
  25.         if (choice < DrawRect || choice > Quit) { 
  26.             cout << 
    Invalid Choice, try again.; 
  27.             cout << endl << endl; 
  28.             continue
  29.         } 
  30.  
  31.         //   
  32.         switch(choice) { 
  33.         case DrawRect: 
  34.             DoDrawRect(theRect); 
  35.             break
  36.         case GetArea: 
  37.             DoGetArea(theRect); 
  38.             break
  39.         case GetPerim: 
  40.             DoGetPerim(theRect); 
  41.             break
  42.         case ChangeDimensions: 
  43.             int newLength, newWidth; 
  44.             cout << 
    New width:; 
  45.             cin >> newWidth; 
  46.             cout << 
    New height:; 
  47.             cin >> newLength; 
  48.  
  49.             theRect.SetSize(newWidth, newLength); 
  50.             DoDrawRect(theRect); 
  51.             break
  52.         case Quit: 
  53.             fQuit = true
  54.             cout << 
    Exiting.. << endl << endl; 
  55.             break
  56.         default
  57.             cout << 
    Error in choice << endl; 
  58.             fQuit = true
  59.             break
  60.         } 
  61.     } 
  62.     return 0; 
  63.  
  64. //   
  65. int DoMenu() { 
  66.     int choice; 
  67.  
  68.     cout << endl << endl; 
  69.     cout <<    *** Menu ***   << endl; 
  70.     cout << (1) Draw Rectangle << endl; 
  71.     cout << (2) Area << endl; 
  72.     cout << (3) Perimeter << endl; 
  73.     cout << (4) Resize << endl; 
  74.     cout << (5) Quit << endl; 
  75.     cin >> choice; 
  76.     return choice; 
  77.  
  78. //   
  79. void DoDrawRect(Rectangle theRect) { 
  80.     int height = theRect.GetHeight(); 
  81.     int width = theRect.GetWidth(); 
  82.     int i, j; 
  83.  
  84.     for (i = 0; i < height; i++) { 
  85.         for (j = 0; j < width; j++) { 
  86.             cout << *; 
  87.         } 
  88.         cout << endl; 
  89.     } 
  90.  
  91. void DoGetArea(Rectangle theRect) { 
  92.     cout << Area: << theRect.GetArea() << endl; 
  93.  
  94. void DoGetPerim(Rectangle theRect) { 
  95.     cout << Perimeter: << theRect.GetPerim() << endl;