莫比乌斯蛇 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 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?