[C++] c++ 嵌套类 →→→→→进入此内容的聊天室

来自 , 2020-01-16, 写在 C++, 查看 118 次.
URL http://www.code666.cn/view/5b6ba13f
  1. #include <iostream>
  2. using namespace std;
  3. class rectangle
  4. {
  5. public:
  6.         class point
  7.         {
  8.         public:
  9.                 void setx ( int x ) {itsx=x;}
  10.                 void sety ( int y ) {itsy=y;}
  11.                 int getx() const {return itsx;}
  12.                 int gety() const {return itsy;}
  13.         private:
  14.                 int itsx;
  15.                 int itsy;
  16.         };
  17.         point GetUpLeft() const {return upleft;}
  18.         point GetLowLeft() const {return lowerleft;}
  19.         point GetUpRight() const {return upright;}
  20.         point GetLowRight() const {return lowerright;}
  21.  
  22.         rectangle ( int top,int left,int bottom,int right );
  23.         ~rectangle() {}
  24.  
  25.         int GetTop() const {return Top;}
  26.         int GetLeft() const {return Left;}
  27.         int GetBottom() const {return Bottom;}
  28.         int GetRight() const {return Right;}
  29.  
  30.         void SetUpLeft() {upleft.setx ( Left ); upleft.sety ( Top );}
  31.         void SetLowLeft() {lowerleft.setx ( Left ); lowerleft.sety ( Bottom );}
  32.         void SetUpRight() {upright.setx ( Right ); upright.sety ( Top );}
  33.         void SetLowRight() {lowerright.setx ( Right ); lowerright.sety ( Bottom );}
  34.  
  35.         void SetTop ( int top ) {Top=top;}
  36.         void SetLeft ( int left ) {Left=left;}
  37.         void SetRight ( int right ) {Right=right;}
  38.         void SetBottom ( int bottom ) {Bottom=bottom;}
  39.  
  40.         int GetArear() const {int width=Right-Left; int height=Bottom-Top; return ( width*height );}
  41.  
  42. private:
  43.         point upleft;
  44.         point lowerleft;
  45.         point upright;
  46.         point lowerright;
  47.  
  48.         int Top;
  49.         int Left;
  50.         int Bottom;
  51.         int Right;
  52. };
  53. rectangle::rectangle ( int top,int left,int bottom,int right )
  54. {
  55.         Top=top;
  56.         Left=left;
  57.         Bottom=bottom;
  58.         Right=right;
  59.  
  60.         upleft.setx ( Left );
  61.         upleft.sety ( Top );
  62.  
  63.         upright.setx ( Right );
  64.         upright.sety ( Top );
  65.  
  66.         lowerright.setx ( Right );
  67.         lowerright.sety ( Bottom );
  68.  
  69.         lowerleft.setx ( Left );
  70.         lowerleft.sety ( Bottom );
  71. }
  72. class point
  73. {
  74. public:
  75.         int GetArear ( rectangle &rec ) {return rec.GetArear();}
  76. };
  77.  
  78. int main()
  79. {
  80.         rectangle date ( 40,50,60,80 );
  81.         cout<<"左边为:"<<date.GetLeft() <<endl;
  82.         cout<<"下边为:"<<date.GetBottom() <<endl;
  83.         cout<<"左下的X坐标为:"<<date.GetLowLeft().getx() <<endl;
  84.         cout<<"左下的y坐标为:"<<date.GetLowLeft().gety() <<endl;
  85.         cout<<"矩形面积为:"<<date.GetArear() <<endl;
  86.         cout<<"重新设置Left和Bottom值"<<endl;
  87.         date.SetLeft ( 0 );
  88.         date.SetBottom ( 100 );
  89.         date.SetLowLeft();
  90.         cout<<"左边为:"<<date.GetLeft() <<endl;
  91.         cout<<"下边为:"<<date.GetBottom() <<endl;
  92.         cout<<"左下的X坐标为:"<<date.GetLowLeft().getx() <<endl;
  93.         cout<<"左下的y坐标为:"<<date.GetLowLeft().gety() <<endl;
  94.         point pt;
  95.         cout<<"矩形面积为:"<<pt.GetArear ( date ) <<endl;
  96.         system ( "pause" );
  97. }

回复 "c++ 嵌套类"

这儿你可以回复上面这条便签

captcha