点与直线
#include<iostream> #include<Cmath> using namespace std; class Dot { public: float x,y; Dot(float a=0,float b=0) { x=a; y=b; } void Show(void) { cout<<"x="<<x<<'\t'<<"y="<<y<<endl; } }; class Line:public Dot { Dot d1,d2; public: Line(Dot dot1,Dot dot2):d1(dot1),d2(dot2) { x=(d1.x+d2.x)/2; y=(d1.x+d2.y)/2; } void Showl(void) { cout<<"Dot1: "; d1.Show(); cout<<"Dot2: "; d2.Show(); cout<<"Length="<<sqrt((d1.x-d2.x)*(d1.x-d2.x)+(d1.y-d2.y)*(d1.y-d2.y))<<endl; cout<<"Center: "<<"x="<<x<<'\t'<<"y="<<y<<endl; } }; int main() { float a,b; cout<<"Input Dot1: \n"; cin>>a>>b; Dot dot1(a,b);//调用Dot的构造函数 cout<<"Input Dot2: \n"; cin>>a>>b; Dot dot2(a,b); Line line(dot1,dot2); line.Showl(); return 0; }
时间: 2024-09-20 10:47:00