# 矩形类定义【C++】

``3.7 0.4 6.5 4.9``

``````12.6
12.6
10``````

``````#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle();
Rectangle(double a, double b, double c, double d);
Rectangle(Rectangle &p);
void input();  //输入左下角和右上角的坐标，left<right, bottom<top在运行输入时保证，程序中不必判断
void output(); //输出面积和周长
private:
double left,bottom,right,top;  //左下角坐标和右上角坐标
};

Rectangle::Rectangle(double a,double b,double c,double d)
{
left=a;
bottom=b;
right=c;
top=d;
}
Rectangle::Rectangle(){}

Rectangle::Rectangle(Rectangle &p)
{
left=p.left;
bottom=p.bottom;
right=p.right;
top=p.top;
}
void Rectangle::input()    //输入
{
cin>>left>>bottom>>right>>top;
}
void Rectangle::output()
{
double width, height, area;
width = right - left;
height = top - bottom;
area = height * width;
cout<<area<<endl;
}
int main()
{
Rectangle p1;
p1.input();
p1.output();
Rectangle p2(p1);
p2.output();
Rectangle p3(1,1,6,3);
p3.output();
return 0;
}
``````

``````#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle();
Rectangle(double a, double b, double c, double d);
Rectangle(Rectangle &p);
void input();  //输入左下角和右上角的坐标，left<right, bottom<top在运行输入时保证，程序中不必判断
void output(); //输出面积和周长
private:
double left,bottom,right,top;  //左下角坐标和右上角坐标
};

Rectangle::Rectangle(double a,double b,double c,double d)
{
left=a;
bottom=b;
right=c;
top=d;
}
Rectangle::Rectangle(){}

Rectangle::Rectangle(Rectangle &p)
{
left=p.left;
bottom=p.bottom;
right=p.right;
top=p.top;
}
void Rectangle::input()    //输入
{
cin>>left>>bottom>>right>>top;
}
void Rectangle::output()
{
double width, height, area;
width = right - left;
height = top - bottom;
area = height * width;
cout<<area<<endl;
}
int main()
{
Rectangle p1;
p1.input();
p1.output();
Rectangle p2(p1);
p2.output();
Rectangle p3(1,1,6,3);
p3.output();
return 0;
}
``````