c++程序定义一个点类Point

c++程序定义一个点类Point,第1张

class Point {

private:

double x

double y

public:

Point()

Point(double x1, double y1) {x=x1y=y1}

void set(double x1, double y1) {x=x1y=y1}

void Display() {cout <渗弯<"(x,y)=(" <<雹喊庆 x <<"," <源握<y <<")" <<endl

void move(double x1, double y1) {x=x1y=y1}

double getx() {return x}

double gety() { return y}

double Distance(Point &p) {

return sqrt( pow((p.getx()-x), 2) + pow((p.gety()-y),2))

}

}

#include<iostream>

#include<cmath>

using namespace std

class Point

{

private:

float x,y

public:

Point(){}

Point(float a,float b):x(a),y(b){}

Point(Point&a)

float Distance(Point b)

}

Point::Point(Point&a)

{

x=a.x

y=a.y

}

float Point::Distance(Point b)

{

return sqrt((x-b.x)*(x-b.x)+(y-b.y)*(y-b.y))

}

class Line:public Point

{

private:

Point a,b

public:

Line(float x1,float y1,float x2,float y2):a(x1,y1),b(x2,y2){}

float Display()

}

float Line::Display()//这个地方不知道你要输出的是什么,但从你的漏誉源程序中知道要返回一个值,所以,我返回了线段的距离

{

return a.Distance(b)

}

int main()

{

Point a

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

return 0

}

#include<iostream>

#include<math.h>

using namespace std

class Point {

public:

 Point() {x = 0 y = 0 }

 Point(double xv,double yv) {x = xvy = yv}

 Point(Point& pt) { x = pt.x y = pt.y 拆段}

 double getx() { return 余锋x }

 double gety() { return y }

 double Area() { return 0 }

 void Show() { cout<<"x="<<x<<' '<<"y="<<y<<endl }

private:

 double x,y

}

class Rectangle:public Point{

 double a,b

 double l

 double w

 public:

  Rectangle(double aa,double bb,double ll,double ww):Point(aa,bb),a(aa),b(bb),l(ll),w(ww){

  }

  void position(Point& pt){

   if(pt.getx()>a && pt.getx()<a+l && pt.gety()>b-w && pt.gety()<b) cout<<"The point is in the rectangle!"<<endl

   else if((pt.getx()==a &&(pt.gety()>=b-w && pt.gety()<=b)) || (pt.getx()==a+l &&(pt.gety()>=b-w && pt.gety()<=b)) || (pt.gety()==b && (pt.getx()>=a && pt.getx()<=a+l)) || (pt.gety()==b-w && (pt.getx()>=a && pt.getx()<=a+l))){

    cout<<"The point is at the edge of the rectangle!"<<endl

   }

   else cout<<"The point is out of the rectangle!"<<endl

  }

  double Area(){

   return l*w

  }

}

class Circle:public Point{

 double a,b

 double r

 public:

  Circle(double aa,double bb,double rr):Point(aa,bb),a(aa),b(bb),r(rr){

  }

  void position(Point& pt){

   double d=sqrt((pt.getx()-a)*(pt.getx()-a)+(pt.gety()-b)*(pt.gety()-b))

   if(d<r) cout<<"The point is in the circle!"<<endl

   else if (d==r) cout<<"The point is on the circle!"<<endl

   else cout<<"The point is out of the circle!"<<endl

  }

  double Area(){

   return 3.14*r*r

  }

}

//TEST!!!

/*******

int main(){

 Point p(1,2)

 Rectangle r(0,2,4,2)

 旅毁誉cout<<"The area of rectangle: "<<r.Area()<<endl

 r.position(p)

 Circle c(0,2,1)

 cout<<"The area of circle: "<<c.Area()<<endl

 c.position(p)

}

*/


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12454754.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存