莫比乌斯蛇 2021-12-21 12:50 采纳率: 100%
浏览 32
已结题

想知道这个C++类里的矩形该怎么写?

中间缺了一些成员函数和拷贝函数没有写出来,希望老师帮帮我。

矩形类处理程序
【要求】按以下描述和要求建立矩形类Rectangle,成员函数功能见注释:
#include <math.h>
class Rectangle {
double x1, y1 ; //左下角的坐标
double x2, y2 ; //右上角的坐标
public:
Rectangle(double a=0, double b=0, double c=0, double d=0){//构造函数
x1=a, y1=b, x2=c, y2=d;}
Rectangle(Rectangle & B){
} //拷贝构造函数
~ Rectangle(){};
void fill(double *p){ //用p数组中的值依次修改4个数据成员
x1=p; y1=(p+1);
}
void Show(); //输出数据成员
double Area();//计算并输出矩形面积
};
void compare( ){…… }//比较两矩形的面积并输出比较结果。
请完成以上未定义函数体的成员函数和有空缺的部分。
测试用主函数参考如下:
void main(){
double data[12]={1,1,4,4,2.5,4,7,8.5,2,3,6,5};
Rectangle R,A[3];
cout<<"矩形R:"<<endl; R.Show();
R.fill(data);
cout<<"修改后:"<<endl; R.Show();
cout<<"矩形A[i]:"<<endl;
A[1].fill(data+4);
for(int i=0;i<3;i++) A[i].Show();
Rectangle T(A[1]);
cout<<"矩形T:"<<endl; T.Show();
compare(R,T);
}

  • 写回答

2条回答 默认 最新

  • 关注

    修改如下:

    #include <iostream>
    #include <math.h>
    using namespace std;
    class Rectangle {
        double x1, y1 ; //左下角的坐标
        double x2, y2 ; //右上角的坐标
    public:
        Rectangle(double a=0, double b=0, double c=0, double d=0){//构造函数
            x1=a, y1=b, x2=c, y2=d;}
        Rectangle(Rectangle & B){
            x1 = B.x1;
            y1 = B.y1;
            x2 = B.x2;
            y2 = B.y2;
        } //拷贝构造函数
        ~ Rectangle(){};
        void fill(double *p){ //用p数组中的值依次修改4个数据成员
            x1=*p; y1=*(p+1);
            x2=*(p+2);y2=*(p+3);
        }
        void Show(); //输出数据成员
        double Area();//计算并输出矩形面积
    };
    
    void Rectangle::Show()
    {
        cout << "(x1,y1)=(" <<x1<<","<<y1<<") , (x2,y2)=("<<x2<<","<<y2<<")"<<endl;
    }
    
    double Rectangle::Area()
    {
        double witdh = fabs(x2 - x1);
        double height = fabs(y2-y1);
        return (witdh*height);
    }
    
    
    void compare(Rectangle& r1,Rectangle& r2 )
    {
        if(r1.Area() > r2.Area())
            cout << "矩形1的面积大于矩形2的面积"<<endl;
        else if(r1.Area() == r2.Area())
            cout <<"矩形1的面积等于矩形2的面积"<<endl;
        else
            cout <<"矩形1的面积小于矩形2的面积"<<endl;
    }//比较两矩形的面积并输出比较结果。
    
    void main(){
        double data[12]={1,1,4,4,2.5,4,7,8.5,2,3,6,5};
        Rectangle R,A[3];
        cout<<"矩形R:"<<endl; R.Show();
        R.fill(data);
        cout<<"修改后:"<<endl; R.Show();
        cout<<"矩形A[i]:"<<endl;
        A[1].fill(data+4);
        for(int i=0;i<3;i++) A[i].Show();
        Rectangle T(A[1]);
        cout<<"矩形T:"<<endl; T.Show();
        compare(R,T);
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月31日
  • 已采纳回答 12月23日
  • 创建了问题 12月21日

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器