设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角和右上角两个点的坐标,能计算矩形的面积。
展开全部
#include <iostream.h>
class Point
{private:
double x,y;
public:
Point(double xx,double yy){x=xx;y=yy;}
double getX(){return x;}
double getY(){return y;}
};
class Rectangle
{ private:
Point Leftdown,Rightup;
public:
Rectangle(double x1,double y1,double x2,double y2):LeftDown(x1,y1),Rightup(x2,y2){}
double Area(){return (Rightup.getX()-Leftdown.getX())*(Rightup.getY()-Leftdown.getY());}
};
void main()
{ Rectangle r(1.0,1.5,2.0,3.3);
cout<<"矩形面积是:"<<r.Area()<<endl;
}
class Point
{private:
double x,y;
public:
Point(double xx,double yy){x=xx;y=yy;}
double getX(){return x;}
double getY(){return y;}
};
class Rectangle
{ private:
Point Leftdown,Rightup;
public:
Rectangle(double x1,double y1,double x2,double y2):LeftDown(x1,y1),Rightup(x2,y2){}
double Area(){return (Rightup.getX()-Leftdown.getX())*(Rightup.getY()-Leftdown.getY());}
};
void main()
{ Rectangle r(1.0,1.5,2.0,3.3);
cout<<"矩形面积是:"<<r.Area()<<endl;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询