標籤:c++ iostream namespace class 指標
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的面積
Sample Input
3.7 0.4 6.5 4.9
Sample Output
12.6
12.6
10
/* All rights reserved. * 檔案名稱:test.cpp * 陳丹妮 * 完成日期:2015年 6 月 21 日 * 版 本 號:v1.0 */#include <iostream>#include <cmath>using namespace std;class Rectangle{private: double x1; double y1; double x2; double y2;public: Rectangle(double a1=1,double b1=1,double a2=6,double b2=3):x1(a1),y1(b1),x2(a2),y2(b2) {} Rectangle(Rectangle &p); void input(); void output();};void Rectangle::input(){ cin>>x1>>y1>>x2>>y2;}void Rectangle::output(){ double s; s=abs(x2-x1)*abs(y2-y1); cout<<s<<endl;}Rectangle::Rectangle(Rectangle &p){ x1=p.x1; y1=p.y1; x2=p.x2; y2=p.y2;}int main(){ Rectangle p1; p1.input(); p1.output(); Rectangle p2(p1); p2.output(); Rectangle p3(1,1,6,3); p3.output(); return 0;}
心得體會:這個還是比較簡單,繼續努力,刷題刷題!!!
第十五周oj刷題——Problem B: 矩形類定義【C++】