不知道这个标题是否对~~
下面是个求矩形周长及面积的程序
原来是这样的:
#include<iostream.h>
class Rectangle
{
public:
void SetRectangle (double top,double left,double bottom,double right);
double GetTop(){return itsTop;}
double GetLeft(){return itsLeft;}
double GetBottom(){return itsBottom;}
double GetRight(){return itsRight;}
double Perimeter();
double Area();
private:
double itsTop,itsLeft,itsBottom,itsRight;
};
void Rectangle::SetRectangle (double top,double left,double bottom,double right)
{
itsTop=top;
itsLeft=left;
itsBottom=bottom;
itsRight=right;
}
double Rectangle::Perimeter()
{
double Width,Height;
Width=itsRight-itsLeft;
Height=itsTop-itsBottom;
return(2*(Width+Height));
}
double Rectangle::Area()
{
double Width,Height;
Width=itsRight-itsLeft;
Height=itsTop-itsBottom;
return (Width*Height);
}
int main()
{
Rectangle Rect;
Rect.SetRectangle(6.3,2.1,3.2,5.2);
cout<<"左下角与右上角坐标分别为("<<Rect.GetLeft()<<","<<Rect.GetBottom()<<")、("<<Rect.GetRight()<<","<<Rect.GetTop()<<")的矩形";
cout<<endl;
cout<<"周长:"<<Rect.Perimeter()<<endl;
cout<<"面积:"<<Rect.Area()<<endl;
return 0;
}
但我想改成自己输入参数的,于是就: (但编译没通过,正确的应该是怎样的?)
#include<iostream.h>
class Rectangle
{
public:
void SetRectangle (double top,double left,double bottom,double right);
double GetTop(){return itsTop;}
double GetLeft(){return itsLeft;}
double GetBottom(){return itsBottom;}
double GetRight(){return itsRight;}
double Perimeter();
double Area();
private:
double itsTop,itsLeft,itsBottom,itsRight;
};
void Rectangle::SetRectangle (double top,double left,double bottom,double right)
{
cout<<"请按顺序输入左下标和右上标"<<endl;
cin>>top>>left>>bottom>>right;
itsTop=top;
itsLeft=left;
itsBottom=bottom;
itsRight=right;
}
double Rectangle::Perimeter()
{
double Width,Height;
Width=itsRight-itsLeft;
Height=itsTop-itsBottom;
return(2*(Width+Height));
}
double Rectangle::Area()
{
double Width,Height;
Width=itsRight-itsLeft;
Height=itsTop-itsBottom;
return (Width*Height);
}
int main()
{
Rectangle Rect;
Rect.SetRectangle(top,left,bottom,right);
cout<<"左下角与右上角坐标分别为("<<Rect.GetLeft()<<","<<Rect.GetBottom()<<")、("<<Rect.GetRight()<<","<<Rect.GetTop()<<")的矩形";
cout<<endl;
cout<<"周长:"<<Rect.Perimeter()<<endl;
cout<<"面积:"<<Rect.Area()<<endl;
return 0;
}
[此贴子已经被作者于2007-5-12 14:25:44编辑过]