初冀 2022-10-23 19:40 采纳率: 61%
浏览 43
已结题

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;
}

  • 写回答

2条回答 默认 最新

  • 浪客 2022-10-23 21:02
    关注
    
    #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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月1日
  • 已采纳回答 10月24日
  • 修改了问题 10月23日
  • 修改了问题 10月23日
  • 展开全部

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c