关闭

嵌套类

243人阅读 评论(0) 收藏 举报
分类:
#include <iostream>
using namespace std;
class rectangle
{
  public:
class point
{
public:
void setx(int X){x=X;}
void sety(int Y){y=Y;}
int getx()const{return x;}
int gety()const{return y;}
private:
int x;
int y;
};
point GetUpLeft()const{return upleft;}
point GetLowLeft()const{return lowerleft;}
point GetUpRight()const{return upright;}
point GetLowRight()const{return lowerright;}


rectangle(int top,int left,int bottom,int right);
~rectangle(){}


int GetTop()const{return Top;}
int GetLeft()const{return Left;}
int GetBottom()const{return Bottom;}
int GetRight()const{return Right;}


void SetUpLeft(){upleft.setx(Left);upleft.sety(Top);}
void SetLowLeft(){lowerleft.setx(Left);lowerleft.sety(Bottom);}
void SetUpRight(){upright.setx(Right);upright.sety(Top);}
void SetLowRight(){lowerright.setx(Right);lowerright.sety(Bottom);}
void SetTop(int top){Top=top;}
void SetLeft(int left){Left=left;}
void SetRight(int right){Right=right;}
void SetBottom(int bottom){Bottom=bottom;}
int GetArea()const{int width=Right-Left;int height=Bottom-Top; return(width*height);}


  private:
point upleft;
point lowerleft;
point upright;
point lowerright;


int Top;
int Left;
int Bottom;
int Right;
};


rectangle::rectangle(int top,int left,int bottom,int right)
{
Top=top;
Left=left;
Bottom=bottom;
Right=right;


upleft.setx(Left);
upleft.sety(Top);


upright.setx(Right);
upright.sety(Top);


lowerright.setx(Right);
lowerright.sety(Bottom);


lowerleft.setx(Left);
lowerleft.sety(Bottom);
}
class point 
{
  public:
int GetArea(rectangle &rec){return rec.GetArea();}
};


int main()
{
rectangle data(40,50,60,80);
cout<<"左边为:"<<data.GetLeft()<<endl;
cout<<"下边为:"<<data.GetBottom()<<endl;
cout<<"左下的x坐标为:"<<data.GetLowLeft().getx()<<endl;
cout<<"左下的y坐标为:"<<data.GetLowLeft().gety()<<endl;
cout<<"矩形面积为:"<<data.GetArea()<<endl;
cout<<"重新设置Left和Bottom值"<<endl;
data.SetLeft(0);
data.SetBottom(100);
data.SetLowLeft();
cout<<"左边为:"<<data.GetLeft()<<endl;
cout<<"下边为:"<<data.GetBottom()<<endl;
cout<<"左下的x坐标为:"<<data.GetLowLeft().getx()<<endl;
cout<<"左下的y坐标为:"<<data.GetLowLeft().gety()<<endl;
point pt;
cout<<"矩形面积为:"<<pt.GetArea(data)<<endl;
return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:19723次
    • 积分:697
    • 等级:
    • 排名:千里之外
    • 原创:52篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条
    文章存档