22、从Point继承的Rectangle

问题描述 :

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

实验内容:

定义一个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”。

 

输入范例 :

1 0 10 9
11 3
2 0

输出范例 :

90
Not in
In

解题代码: 

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
#include<set>
using namespace std;
class Point{
private:
    int x;
    int y;
public:
    Point(int x,int y);
    void move(int dx, int dy);
    friend class Rectangle;
};
Point::Point(int x,int y)
{
    Point::x=x;
    Point::y=y;
}
void Point::move(int dx,int dy)
{
    x=x+dx;
    y=y+dy;
    return ;
}
class Rectangle:public Point{
private:
    int width;
    int height;
public:
    Rectangle(int x, int y, int width, int height);
    int getArea ();
    bool isIn(Point p);
};
Rectangle::Rectangle(int x, int y, int width, int height):Point(x,y)
{
    Rectangle::width=width;
    Rectangle::height=height;
}
int Rectangle::getArea ()
{
    return width*height;
}
bool Rectangle::isIn(Point p)
{
    bool ok1=0,ok2=0;
    if(p.x>x && p.x<x+width)
        ok1=1;
    else ok1=0;
    if(p.y>y && p.y<y+height)
        ok2=1;
    else ok2=0;
    return (ok1&&ok2);
}
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;
}

 

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值