/*
*copyright (c)2015,烟台大学计算机学院
*All rights reserved
*文件名称:project.cpp
*作者:孙春红
*完成日期:2015年5月29日
*版本号:v1.0
*
*问题描述:定义一个矩形类,数据成员包括左下角和右上角坐标,
定义的成员函数包括必要的构造函数、输入坐标的函数,以及计算并输
出矩形面积的函数。要求使用提示中给出的测试函数并不得改动。
,
*输入描述:四个数,分别表示矩形左下角和右上角顶点的坐标,。
*程序输出:输出一共有3行(请参考提示(hint)中的main函数):
第一行:由输入的坐标确定的矩形对象p1的面积
第二行:由对象复制得到的矩形对象p2的面积
第三行:直接初始化得到的矩形对象p3的面积
*/
#include <iostream>
#include <cmath>
using namespace std;
class Rectangle
{
public:
Rectangle(double x1=0,double y1=0,double a1=0,double b1=0):x(x1),y(y1),a(a1),b(b1) {}
void input();
void output();
private:
double x,y,a,b;
};
void Rectangle::input()
{
cin>>x>>y>>a>>b;
}
void Rectangle::output()
{
double v;
v=(a-x)*(b-y);
if (v<0)
{
v=-v;
cout<<v<<endl;
}
else
cout<<v<<endl;
}
int main()
{
Rectangle p1;
p1.input();
p1.output();
Rectangle p2(p1);
p2.output();
Rectangle p3(1,1,6,3);
p3.output();
return 0;
}
运行结果: