中间缺了一些成员函数和拷贝函数没有写出来,希望老师帮帮我。
矩形类处理程序
【要求】按以下描述和要求建立矩形类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);
}
修改如下:
#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);
}
double Area()//计算并输出矩形面积
{
return (x2-x1)*(y1-y2);
}