/************

./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 << \nInvalid 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 << \nNew width:; 
  45.             cin >> newWidth; 
  46.             cout << \nNew height:; 
  47.             cin >> newLength; 
  48.  
  49.             theRect.SetSize(newWidth, newLength); 
  50.             DoDrawRect(theRect); 
  51.             break
  52.         case Quit: 
  53.             fQuit = true
  54.             cout << \nExiting.. << endl << endl; 
  55.             break
  56.         default
  57.             cout << \nError 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;