【东华大学oj】25 从Point继承的Rectangle(面向对象)

25 从Point继承的Rectangle

作者: Turbo时间限制: 1S章节: 继承

问题描述 :

实验目的:学习继承的编程方法

实验内容:

定义一个Point类,包括:

两个私有数据成员:int x, int y,它们分别表示一个点的x和y座标。

构造函数:

Point(int x, int y),即传两个参数,构造一个点对象。

注意,本题要求Point类不得定义缺省构造函数,也就是只定义以上所说的构造函数。

成员函数:

Move(int dx, int dy),将Point对象移动dx,dy的距离。 

定义一个Rectangle类,继承Point类,基类对象的x和y表示长方形左上角的坐标。

Rectangle类包括:

两个私有数据成员:int width, int height,它们分别表示长方形的横向的宽度和纵向的高度。

构造函数:

Rectangle(int x, int y, int width, int height)。

成员函数:

int getArea ()    //获取该图形的面积

bool isIn(Point p)   //判断传入的点是否在该图形之内(不包括边界),如果在内部返回true,否则返回false

使用main函数测试以上getArea方法和isIn方法。main函数使用如下代码:

int main()

{

       int topLeftX, topLeftY, width, height;

       int px, py, dx, dy;

       cin>>topLeftX>>topLeftY>>width>>height;

       cin>>px>>py;

       cin>>dx>>dy;

    Point p(px, py);

    Rectangle r(topLeftX, topLeftY, width, height);

    cout<<r.getArea()<<endl;

    if(r.isIn(p))

        cout<<"In"<<endl;

    else

        cout<<"Not in"<<endl;

    r.move(dx,dy);

    if(r.isIn(p))

        cout<<"In"<<endl;

    else

        cout<<"Not in"<<endl;

       return 0;

}

输入说明 :

第一行输入长方形r的信息,包括左上角顶点x座标、左上角顶点y座标、宽度、高度。

第二行输入一个点p的信息,包括其x座标和y座标。

第三行输入长方形移动的距离,分别为在x方向和在y方向移动的距离。

所有输入都为整数,之间以一个空格分隔。无多余空格或空行。

输出说明 :

输出三行:

第一行输出长方形面积。

第二行输出点p是否位于长方形r之内,如果在内部,则输出“In”,否则输出“Not in”。

第三行输出点p移动后是否位于长方形r之内,如果在内部,则输出“In”,否则输出“Not in”。

#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

class Point
{
private :
    int x,y;
public :
    Point(int x,int y):x(x),y(y) {}
    void  move(int dx, int dy)
    {
        x=x+dx;
        y=y+dy;
    }
    int getx()
    {
        return x;
    }
    int gety()
    {
        return y;
    }



};

class Rectangle:public Point
{
private:
    int width,height;
public:
    Rectangle(int x, int y, int width, int height):width(width),height(height),Point(x,y)
    {

    }


    int getArea( )
    {
        int area=width*height;
        return area;

    }

    bool isIn(Point p)
    {

        if(p.getx()<this->getx()+width&&p.getx()>this->getx()&&p.gety()>this->gety()&&p.gety()<this->gety()+height)
        {
            return true;

        }
        else return false;
    }

};

int main()

{

    int topLeftX, topLeftY, width, height;

    int px, py, dx, dy;



    cin>>topLeftX>>topLeftY>>width>>height;

    cin>>px>>py;

    cin>>dx>>dy;



    Point p(px, py);

    Rectangle r(topLeftX, topLeftY, width, height);

    cout<<r.getArea()<<endl;

    if(r.isIn(p))

        cout<<"In"<<endl;

    else

        cout<<"Not in"<<endl;

    r.move(dx,dy);

    if(r.isIn(p))

        cout<<"In"<<endl;

    else

        cout<<"Not in"<<endl;

    return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ixll625

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值