点类与线类(继承)

来源:互联网 发布:新郎 婚礼开场白 知乎 编辑:程序博客网 时间:2024/06/10 08:10

#include <iostream>
using namespace std;
#include <Cmath>

class Point  //定义了类Point
{
public:
 double x;
 double y;

 Point( double a,double b)
 {
  x = a;
  y = b;
 }
 virtual void display(){cout <<"你输入的坐标是"<<"(" << x << "," << y << ")" << endl;}  //以(X,Y)的形式打印
};
class Line : public Point  // 定义Point的派生类Line(Public)
{
public:
 Line(double a,double b,double q,double w) : Point (a,b)
 {
  x1 = q;
  y1 = w;
 }
 void display()
 {
  cout <<"这两个坐标是"<< "(" << x1 << "," << y1 << ")" << " " << "(" << x << "," << y << ")" << endl;
 }
 inline void length()
 {
  cout <<"这条线段的长为"<< sqrt( (x - x1)*(x - x1) + (y - y1)*(y - y1)) << endl;

 }

private:
    double x1;
    double y1;
};
void main()
{
 Point p1(1.2,2.4);
 p1.display();
 Line l1(1.2,2.4,4.5,1.6);
 l1.display();
 l1.length();
}

0 0
原创粉丝点击