设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,根据坐标能计算矩形的面 5
1个回答
展开全部
#include<iostream>
#include<math.h>
using namespace std;
class Rectangle
{
public:
Rectangle(double X1=0,double Y1=0,double X2=0,double Y2=0)
{x1=X1;y1=Y1;x2=X2;y2=Y2;}
double area();
private:
double x1,y1,x2,y2;
};
double Rectangle::area()
{
return fabs((x1-x2)*(y1-y2));
}
void main()
{
double x1,y1,x2,y2,Area;
cout<<"请输入左下角和右上角点的坐标x1,y1,x2,y2:"<<endl;
cin>>x1>>y1>>x2>>y2;
Rectangle rect(x1,y1,x2,y2);
Area=rect.area();
printf("矩形的面积为:%5f",Area);
}*/
/************128页 4-13**************/
/*#include<iostream>
using namespace std;
const float PI=3.14;
class Circle
{
public:Circle (float r=0)
{radius=r;}
float getArea ();
private:float radius;
};
float Circle::getArea()
{return PI*radius*radius;}
void main()
{
float r;
cout<<"请输入圆的半径:"<<endl;
cin>>r;
Circle circle(r);
cout<<"圆的面积为:"<<circle.getArea()<<endl;
}
#include<math.h>
using namespace std;
class Rectangle
{
public:
Rectangle(double X1=0,double Y1=0,double X2=0,double Y2=0)
{x1=X1;y1=Y1;x2=X2;y2=Y2;}
double area();
private:
double x1,y1,x2,y2;
};
double Rectangle::area()
{
return fabs((x1-x2)*(y1-y2));
}
void main()
{
double x1,y1,x2,y2,Area;
cout<<"请输入左下角和右上角点的坐标x1,y1,x2,y2:"<<endl;
cin>>x1>>y1>>x2>>y2;
Rectangle rect(x1,y1,x2,y2);
Area=rect.area();
printf("矩形的面积为:%5f",Area);
}*/
/************128页 4-13**************/
/*#include<iostream>
using namespace std;
const float PI=3.14;
class Circle
{
public:Circle (float r=0)
{radius=r;}
float getArea ();
private:float radius;
};
float Circle::getArea()
{return PI*radius*radius;}
void main()
{
float r;
cout<<"请输入圆的半径:"<<endl;
cin>>r;
Circle circle(r);
cout<<"圆的面积为:"<<circle.getArea()<<endl;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询