你今天机器学习了么 2020-05-28 23:19 采纳率: 0%
浏览 127

C++问题,没有啥思路,求大佬帮忙

在不改变现有main.cpp的情况下,写一个shape.h和一个shape.cpp来计算两个长方形的大小同时用is_larger比较大小,同时重载运算符 >,下面是main.cpp



#include<iostream>
#include"shape.h"

using namespace std;

int main()
{
// construct a point at position (1,1)
// constuctor of class "Point" is called
Point p(1,1); 

// construct two rectangles using different parameters
Rectangle r1; //r1 is initialized of size 1X1 at the origin 
Rectangle r2(p,0.3,2.9);

// move rectangle r1
// shift the rectangle horizontally by 0.2
// shift the rectangle vertically by -1.3
r1.move(0.2,-1.3);

// compute the areas using member function .area()
cout << r1.area() << endl;
cout << r2.area() << endl;

// compare whether r1 has a larger area than r2?
cout << r1.is_larger(r2) << endl;

// overloading the member function .is_larger() with >
cout << (r1 > r1) << endl;
cout << (r1 > r2) << endl;

return 0;
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-05-29 07:24
    关注

    问题解决的话,请点下采纳

    #pragma once
    
    class Point
    {
    private:
        double x;
        double y;
    public:
        Point() { x = y = 0; }
        Point(double a, double b) { x = a; y = b; }
        Point(Point& pt) { x = pt.x; y = pt.y; }
        void move(double a, double b)
        {
            x += a;
            y += b;
        }
    };
    
    class Rectangle
    {
    private:
        Point p;
        double w;
        double h;
    public:
        Rectangle() : p(0, 0), w(1), h(1) { }
        Rectangle(Point pt, double a, double b) 
        {
            p = pt;
            w = a;
            h = b;
        }
        void move(double a, double b)
        {
            p.move(a, b);
        }
        double area()
        {
            return w * h;
        }
        bool is_larger(Rectangle& another)
        {
            return area() > another.area();
        }
        bool operator>(Rectangle& another)
        {
            return is_larger(another);
        }
    };
    

    1
    0.87
    1
    0
    1
    Press any key to continue . . .

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值