題目要求如下:
-----------------------------------------------------------------------------------------------------------------------------------------------
代碼如下:
/*
* Copyright (c) 2013, 煙臺大學計算機學院
* All rights reserved.
* 作 者: 沈遠宏
* 完成日期:2014 年 06月27日
* 版 本 號:v1.0
* 問題描述:Description
定義一個矩形類,數據成員包括左下角和右上角座標,定義的成員函數包括必要的構造函數、輸入座標的函數,以及計算並輸出矩形面積的函數。要求使用提示中給出的測試函數並不得改動。
Input
四個數,分別表示矩形左下角和右上角頂點的座標,如輸入3.7 0.4 6.5 4.9,代表左下角座標爲(3.7, 0.4),右上角座標爲(6.5, 4.9)。
Output
輸出一共有3行(請參考提示(hint)中的main函數):
第一行:由輸入的座標確定的矩形對象p1的面積
第二行:由對象複製得到的矩形對象p2的面積
第三行:直接初始化得到的矩形對象p3的面積
*/#include <iostream>
using namespace std;
class Point
{
private:
double x;
double y;
public:
Point(double xx=0,double yy=0):x(xx),y(yy) {}
void set(double xx,double yy)
{
x=xx;
y=yy;
}
double get_x()
{
return x;
}
double get_y()
{
return y;
}
};
class Rectangle
{
private:
Point p1;
Point p2;
public:
Rectangle(double x1=0,double y1=0,double x2=0,double y2=0)
{
p1.set(x1,y1);
p2.set(x2,y2);
}
Rectangle(Rectangle &r);
void input();
void output();
double R_area();
};
Rectangle::Rectangle(Rectangle &r)
{
p1.set(r.p1.get_x(),r.p1.get_y());
p2.set(r.p2.get_x(),r.p2.get_y());
}
void Rectangle::input()
{
double x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
p1.set(x1,y1);
p2.set(x2,y2);
}
void Rectangle::output()
{
cout<<R_area()<<endl;
}
double Rectangle::R_area()
{
return (p2.get_x()-p1.get_x())*(p2.get_y()-p1.get_y());
}
int main()
{
Rectangle p1;
p1.input();
p1.output();
Rectangle p2(p1);
p2.output();
Rectangle p3(1,1,6,3);
p3.output();
return 0;
}
運行結果:
OJ要求結果輸出例樣: