C++中派生类的函数问题

来源:百度知道 编辑:UC知道 时间:2024/05/26 23:41:26
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;

class Point
{
protected:
double p1,p2;
public:
Point(double x,double y)
{
p1=x;
p2=y;
};
double Distance(Point b)
{
return sqrt((p1-b.p1)*(p1-b.p1)+(p2-b.p2)*(p2-b.p2));
};
};
class Line:public Point
{
double p3,p4;
public:
Line(double x1,double y1,double x2,double y2):Point(x1,y1)
{
p3=x2;
p4=y2;
};
double Display()
{
return sqrt((p1-p3)*(p1-p3)+(p2-p4)*(p2-p4));
};
};
void main()
{
Point a(0,0);
Point b(7.8,9.8),c(34.5,67.8);
a=c;
cout<<"两点之间距离是:"<<a.Distance(b)<<endl;
Line s(7.8,8.34,34.5,67.8);
cout<<"线段距离是:"<<s.Display<<endl;
}
以上是我的问题程序 运行时线段距离怎么都是1,我觉得是不是我的 Display这个

我在电脑上演示了一下,发现了你的错误。错在了最后一段cout<<"线段距离是:"<<s.Display<<endl;
应该是cout<<"线段距离是:"<<s.Display()<<endl;这是两个不同的语句哦!不能这么粗心大意的阿~~~其他的没什么大问题,关于警告我没有这个问题:1.exe - 0 error(s), 0 warning(s),其实哪个可以忽略的。