20、长方形(类的实现)

问题描述 :

实验目的:学习组合对象的使用。

实验内容:

定义一个Point类,包括两个私有属性:int x, int y,它们分别表示一个点的x和y座标。

再定义构造函数:

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

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

 

定义一个Rectangle类,包括三个私有属性:Point topLeft, int width, int height,它们分别表示长方形左上角顶点的座标,以及横向的宽度和纵向的高度。

注意:在计算机系统里,座标系如下定义:屏幕的左上角的座标是(0,0),x轴是横轴,屏幕的最右端x值最大,y轴是纵轴,屏幕的最下方y值最大。图如下:

1460513127870062860.jpg

再定义构造函数:

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;

 

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

       cin>>px>>py;

 

    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;

 

       return 0;

}

 

输入说明 :

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

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

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

 

输出说明 :

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

 

输入范例 :

10 20 50 80
20 30

输出范例 :

4000
In

解题代码: 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值