【OJ】(二)---A---矩形类定义



题目要求如下:

-----------------------------------------------------------------------------------------------------------------------------------------------

代码如下:

/*
 * 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要求结果输出例样:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值