C++编程 定义一个点类,由它派生出直线类和矩形类。我的程序哪里错了,运行出来结果全是0。
#include<iostream.h>#include<math.h>classCPoint{public:CPoint(intx=0,inty=0){xPos=x;y...
#include<iostream.h>
#include<math.h>
class CPoint
{
public:
CPoint(int x=0,int y=0)
{
xPos=x;
yPos=y;
}
int xPos,yPos;
};
class CLine:public CPoint
{
public:
CPoint x1,x2;
CLine(CPoint x1,CPoint x2)
{
this->x1=x1;
this->x2=x2;
}
float length()
{
return sqrt((x1.xPos-x1.xPos)^2+(x2.yPos-x2.yPos)^2);
}
};
class CRect:public CPoint
{
public:
CPoint x3,x4;
CRect(CPoint x3,CPoint x4)
{
this->x3=x3;
this->x4=x4;
}
int area()
{
return abs((x3.xPos-x3.xPos)*(x4.yPos-x4.yPos));
}
int per()
{
return abs(x3.xPos-x3.xPos)+abs(x4.yPos-x4.yPos);
}
};
int main()
{
CPoint a(10,30);
CPoint b(40,50);
CLine c(a,b);
CRect d(a,b);
cout<<"length:"<<c.length()<<endl;
cout<<"area:"<<d.area()<<endl;
cout<<"per:"<<d.per()<<endl;
} 展开
#include<math.h>
class CPoint
{
public:
CPoint(int x=0,int y=0)
{
xPos=x;
yPos=y;
}
int xPos,yPos;
};
class CLine:public CPoint
{
public:
CPoint x1,x2;
CLine(CPoint x1,CPoint x2)
{
this->x1=x1;
this->x2=x2;
}
float length()
{
return sqrt((x1.xPos-x1.xPos)^2+(x2.yPos-x2.yPos)^2);
}
};
class CRect:public CPoint
{
public:
CPoint x3,x4;
CRect(CPoint x3,CPoint x4)
{
this->x3=x3;
this->x4=x4;
}
int area()
{
return abs((x3.xPos-x3.xPos)*(x4.yPos-x4.yPos));
}
int per()
{
return abs(x3.xPos-x3.xPos)+abs(x4.yPos-x4.yPos);
}
};
int main()
{
CPoint a(10,30);
CPoint b(40,50);
CLine c(a,b);
CRect d(a,b);
cout<<"length:"<<c.length()<<endl;
cout<<"area:"<<d.area()<<endl;
cout<<"per:"<<d.per()<<endl;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询