第十五周oj刷題——Problem B: 矩形類定義【C++】

來源:互聯網
上載者:User

標籤: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++】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.