用c++构造一个矩形类

来源:百度知道 编辑:UC知道 时间:2024/09/22 03:52:47
建立两个点如(x1,y1)(x2,y2)来求矩形类的长宽 面积 周长等问题 主函数里面只采用调用

#include<iostream.h>
#include<math.h>
class Rectangle{
public:
Rectangle(double,double,double,double);
Rectangle();
double Width();
double Height();
double Girth();
double Area();
private:
double x0,y0,x1,y1;
};
Rectangle::Rectangle(double a,double b,double c,double d){
x0=a;
y0=b;
x1=c;
y1=d;
}
Rectangle::Rectangle(){
cout<<"输入矩形左下角坐标"<<endl;
cin>>x0>>y0;
cout<<"再输入矩形右上角坐标"<<endl;
cin>>x1>>y1;
}
double Rectangle::Width(){
return fabs(x1-x0);
}
double Rectangle::Height(){
return fabs(y1-y0);
}
double Rectangle::Girth(){
return (this->Width()+this->Height())*2;
}
double Rectangle::Area(){
return this->Width()*this->Height();
}
void main(){
Rectangle test(0.11,10