C++学习笔记
#include<iostream>
#include<limits>
using namespace std;
class rectangle
{
public:
class point
{
public:
void setx(int X){x=X;}
void sety(int Y){y=Y;}
int getx(){return x;}
int gety(){return y;}
private:
int x;
int y;
};
point GetUpleft()const{return upleft;}
point GetLowerLeft()const {return lowerleft;}
point GetUpRight()const {return upright;}
point GeLowRight()const {return lowerright;}
rectangle (int top ,int letft,int bottom,int right);//构造函数无函数名
~rectangle(){}
int GetTop()const{return Top;}
int GetLelft()const{return Left;}
int GetBottom()const{return Bottom;}
int GetRight()const{return Right;}
void SetLeft(int left){Left=left;}
void SetBottom(int bottom){Bottom=bottom;}
void SetTop(int top){Top=top;}
void SetRight(int right){Left=right;}
void SetUpLeft(){upleft.setx(Left);upleft.sety(Top);}
void SetLowerLeft(){lowerleft.setx(Left);upleft.sety(Bottom);}
void SetLowerRight(){upright.setx(Right);upright.sety(Top);}
void SetUpRight(){upright.setx(Right);upright.sety(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 Bottom;
int Right;
int Left;
};
rectangle::rectangle (int top ,int left,int bottom,int right)
{
Top=top;
Left=left;
Bottom=bottom;
Right=right;
upleft.setx(Left);
upleft.sety(Top);
lowerleft.setx(Left);
lowerleft.sety(Bottom);
upright.setx(Right);
upright.sety(Top);
lowerright.setx(Right);
lowerright.setx(Bottom);
}
class point
{
public:
int GetArea(rectangle &rec){return rec.GetArea();}
};
int main()
{
rectangle date(40,50,60,80);
cout<<"左边为:"<<date.GetLelft()<<endl;
cout<<"右边为:"<<date.GetBottom()<<endl;
cout<<"左上的x坐标为:"<<date.GetLowerLeft().getx()<<endl;
cout<<"左下的y坐标为:"<<date.GetLowerLeft().gety()<<endl;
cout << "矩形的面积为:"<<date.GetArea()<<endl;
cout<<"重新设置Left和Bottom值"<<endl;
date.SetLeft(0);
date.SetBottom(100);
date.SetLowerLeft();
cout<<"左边为:"<<date.GetLelft()<<endl;
cout<<"下边为:"<<date.GetBottom()<<endl;
cout<<"左上的x坐标为:"<<date.GetLowerLeft().getx()<<endl;
cout<<"左上的y坐标为:"<<date.GetLowerLeft().gety()<<endl;
point pt;
cout << "矩形的面积为:"<<pt.GetArea(date)<<endl;
return 0;
}
浙公网安备 33010602011771号