Now, I faced with an exercise problem: Deal with some data in a class CRect. But my results are different from what I expected.
My code is as followed:
#include<iostream>
#include<cmath>
using namespace std;
class CRect
{
public:
void Diagonal(CRect one)
{
float l;
l=sqrt( (one.right-one.left)*(one.right-one.left)+(one.bottom-one.top)*(one.bottom-one.top) );
cout<<"The length is "<<l<<endl;
}
void Show(CRect one)
{
cout<<"("<<one.left<<","<<one.top<<")"<<" ";
cout<<"("<<one.right<<","<<one.bottom<<")"<<endl;
}
CRect(float left1,float top1,float right1,float bottom1)
{
left=left1;
top=top1;
right=right1;
bottom=bottom1;
}
CRect(float left1,float top1)
{
left=left1;
top=top1;
}
CRect(CRect &c)
{
right=c.right;
bottom=c.bottom;
}
private:
float left,top,right,bottom;
};
int main()
{
CRect r1(10,10,20,20);
CRect r2(0,0);
r2=CRect(r1);
r1.Show(r1);
r1.Diagonal(r1);
r2.Show(r2);
r2.Diagonal(r2);
return 0;
}
Then,the result is as the following picture:
I think that I have not initialized the left
and top
. But, I don't know how to correct it. I can't find the mistake in my code.