C++中用类求矩形的面积

img

img

img


img


题目具体是这样的,我写的代码是这样的,然后我设置了一个Point类,一个Rectangle类就是不知道出了什么问题,想要设置两个点,不知道该怎么求面积,大一新生,想求解此题。

include

using namespace std;
class Point
{
public:
void setx(int x) {
m_x = x;
}
int getx()
{
return m_x;
}
void sety(int y)
{
m_y = y;
}
int gety()
{
return m_y;
}
private:
int m_x;
int m_y;
};
class Rectangle {
public:
void setbottomleft(Point bottomleft) { m_bottomleft = bottomleft;
}
Point getbottomleft()
{
return m_bottomleft;
}
void settopright(Point topright)
{
m_topright = topright;
}
Point gettopright()
{
return m_topright;
}
private:
Point m_bottomleft;
Point m_topright;
};
void area(Point& b, Point& t) {
int S= (b.getx() - t.getx()) * (b.gety() - t.gety());
}
int main() {
Point bottomleft;
bottomleft.setx(2);
bottomleft.sety(2);
Point topright;
topright.sety(3);
topright.sety(4);
cout << "矩形的面积为:" << area(bottomleft, topright) << endl;
}


#include <iostream>
using namespace std;

class Point
{
public:
    Point(int x = 0, int y = 0) : m_x(x), m_y(y) {}
    void X(int x)
    {
        m_x = x;
    }
    int X()
    {
        return m_x;
    }
    void Y(int y)
    {
        m_y = y;
    }
    int Y()
    {
        return m_y;
    }

private:
    int m_x;
    int m_y;
};
class Rectangle
{
public:
    Rectangle(int x1 = 0, int y1 = 0, int x2 = 0, int y2 = 0) : m_bottomleft(x1, y1), m_topright(x2, y2) {}

    Rectangle(Point bl, Point tr) : m_bottomleft(bl), m_topright(tr) {}

    void Bottom_Left(int x, int y)
    {
        m_bottomleft.X(x);
        m_bottomleft.Y(y);
    }
    void Bottom_Left(Point bottomleft)
    {
        m_bottomleft = bottomleft;
    }
    Point Bottom_Left()
    {
        return m_bottomleft;
    }

    void Top_Right(int x, int y)
    {
        m_topright.X(x);
        m_topright.Y(y);
    }
    void Top_Right(Point topright)
    {
        m_topright = topright;
    }
    Point Top_Right()
    {
        return m_topright;
    }
    int Area();

private:
    Point m_bottomleft;
    Point m_topright;
};

int Rectangle::Area() //
{
    return (m_topright.X() - m_bottomleft.X()) * (m_topright.Y() - m_bottomleft.Y());
}

int main()
{
    Rectangle rect(2, 2, 4, 5);
    cout << "矩形的面积为:" << rect.Area() << endl;
    return 0;
}