请C++高手指点一个简单的程序设计问题。由Point类派生Line类,计算两点之间的距离。我编的运行结果为0,悲
#include<iostream>#include<cmath>usingnamespacestd;classPoint{protected:floatx,y;publ...
#include<iostream>
#include<cmath>
using namespace std;
class Point{
protected:
float x,y;
public:
Point(float x,float y){this->x=x;this->y=y;}
void Setxy(float x,float y){this->x=x;this->y=y;}
float Getx( ){return x;}
float Gety( ){return y;}
};
class Line:public Point{
protected:
Point p1;
public:
Line(float x,float y):Point(x,y),p1(x,y){
cout<<sqrt((x-p1.Getx( ))*(x-p1.Getx( ))+(y-p1.Gety( ))*(y-p1.Gety( )))<<endl;
}
};
int main(void){
Point d1(1,2);
Line Line(3,4);
return 0;
} 展开
#include<cmath>
using namespace std;
class Point{
protected:
float x,y;
public:
Point(float x,float y){this->x=x;this->y=y;}
void Setxy(float x,float y){this->x=x;this->y=y;}
float Getx( ){return x;}
float Gety( ){return y;}
};
class Line:public Point{
protected:
Point p1;
public:
Line(float x,float y):Point(x,y),p1(x,y){
cout<<sqrt((x-p1.Getx( ))*(x-p1.Getx( ))+(y-p1.Gety( ))*(y-p1.Gety( )))<<endl;
}
};
int main(void){
Point d1(1,2);
Line Line(3,4);
return 0;
} 展开
5个回答
展开全部
你就是想求两点之间的距离吧,何必要派生一个类呢?把Line作为一个函数就好了。我想可以这样写:
#include<iostream>
#include<cmath>
using namespace std;
class Point{
protected:
float x,y;
public:
Point(float x,float y){this->x=x;this->y=y;}
void Setxy(float x,float y){this->x=x;this->y=y;}
float Getx( ){return x;}
float Gety( ){return y;}
};
void Line(Point p1,Point p2){
cout<<sqrt((p2.Getx()-p1.Getx( ))*(p2.Getx()-p1.Getx( ))+(p2.Gety()-p1.Gety( ))*(p2.Gety()-p1.Gety( )))<<endl;
}
int main(void){
Point d1(1,2);
Point d2(3,4);
Line(d1,d2);
return 0;
}
#include<iostream>
#include<cmath>
using namespace std;
class Point{
protected:
float x,y;
public:
Point(float x,float y){this->x=x;this->y=y;}
void Setxy(float x,float y){this->x=x;this->y=y;}
float Getx( ){return x;}
float Gety( ){return y;}
};
void Line(Point p1,Point p2){
cout<<sqrt((p2.Getx()-p1.Getx( ))*(p2.Getx()-p1.Getx( ))+(p2.Gety()-p1.Gety( ))*(p2.Gety()-p1.Gety( )))<<endl;
}
int main(void){
Point d1(1,2);
Point d2(3,4);
Line(d1,d2);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
#include<cmath>
using namespace std;
class Point{
protected:
float x,y;
public:
Point(float x,float y){this->x=x;this->y=y;}
void Setxy(float x,float y){this->x=x;this->y=y;}
float Getx( ){return x;}
float Gety( ){return y;}
};
class Line:public Point{
protected:
Point p1;
public:
Line(float x1,float y1,float x2,float y2):Point(x1,y1),p1(x2,y2){
cout<<sqrt((x1-p1.Getx( ))*(x1-p1.Getx( ))+(y1-p1.Gety( ))*(y1-p1.Gety( )))<<endl;
}
};
int main(void){
Line Line(3,4,6,8);//修改为两个点,因为一条线有两个端点
return 0;
}
#include<cmath>
using namespace std;
class Point{
protected:
float x,y;
public:
Point(float x,float y){this->x=x;this->y=y;}
void Setxy(float x,float y){this->x=x;this->y=y;}
float Getx( ){return x;}
float Gety( ){return y;}
};
class Line:public Point{
protected:
Point p1;
public:
Line(float x1,float y1,float x2,float y2):Point(x1,y1),p1(x2,y2){
cout<<sqrt((x1-p1.Getx( ))*(x1-p1.Getx( ))+(y1-p1.Gety( ))*(y1-p1.Gety( )))<<endl;
}
};
int main(void){
Line Line(3,4,6,8);//修改为两个点,因为一条线有两个端点
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
#include<cmath>
using namespace std;
class Point{
protected:
float x,y;
public:
Point(float xx,float yy){x=xx;y=yy;}
Point(){x=y=0;}
void Setxy(float x,float y){this->x=x;this->y=y;}
float Getx( ){return x;}
float Gety( ){return y;}
};
class Line:public Point{
protected:
Point p1,p2;
public:
Line(float x,float y,float z,float j):p1(x,y),p2(z,j){
cout<<sqrt((p2.Getx()-p1.Getx( ))*(p2.Getx()-p1.Getx( ))+(p2.Gety()-p1.Gety( ))*(p2.Gety()-p1.Gety( )))<<endl;
}
};
int main(void){
//Point d1(1,2);
Line Line(3,4,1,2);
return 0;
}//线类要有两个点的对象才能计算线段的长
#include<cmath>
using namespace std;
class Point{
protected:
float x,y;
public:
Point(float xx,float yy){x=xx;y=yy;}
Point(){x=y=0;}
void Setxy(float x,float y){this->x=x;this->y=y;}
float Getx( ){return x;}
float Gety( ){return y;}
};
class Line:public Point{
protected:
Point p1,p2;
public:
Line(float x,float y,float z,float j):p1(x,y),p2(z,j){
cout<<sqrt((p2.Getx()-p1.Getx( ))*(p2.Getx()-p1.Getx( ))+(p2.Gety()-p1.Gety( ))*(p2.Gety()-p1.Gety( )))<<endl;
}
};
int main(void){
//Point d1(1,2);
Line Line(3,4,1,2);
return 0;
}//线类要有两个点的对象才能计算线段的长
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Line(float x,float y):Point(x,y),p1(x,y) ; 基类的点和p1都用x,y赋值,然后求这两个的距离不为那不就错了, 大哥你是对的,呵呵
追问
应该怎么改呢,请赐教,我是菜鸟啊
追答
线从点派生不好,应用组合,直接在line中定义两个点,然后做。。。。
若按你做法,线有两个点你也可写函数设置那两个点的值然后计算距离。。。
初学分不清组合和继承正常,好好学习吧孩子
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-05-11
展开全部
Line 包含两个 Point 成员,更为合适。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询