点派生出线2

/*
* Copyright (c) 2013, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作者:邱学伟
* 完成日期:2013 年 5 月 11 日
* 版本号:v1.0
* 输入描述:无
* 问题描述: 定义点类,并以点类为基类,派生出直线类,从基类中继承的点的信息表示直线的中点
* 程序输出:
* 问题分析:
* 算法设计:略
*/
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
  public:
  Point():x(0),y(0){};
  Point(double x1,double y1)
  {
     x=x1;
     y=y1;
  }
  double getx(){return x;}
  double gety(){return y;}
  void display();
  private:
  double x,y;
};
void Point::display()
{
    cout<<"Point:("<<x<<","<<y<<")"<<endl;
}
class Line:public Point
{
    public:
    Line(Point p1,Point p2);
    double Lengh();
    void PrintLine();
    void PrintPoint();
    private:
    class Point pts,pte;
};
Line::Line(Point p1,Point p2)
{
        pts=p1;
        pte=p2;

}
double Line::Lengh()
{
    double x0=pts.getx()-pte.getx();
    double y0=pts.gety()-pte.gety();
    return sqrt(x0*x0+y0*y0);

}
void Line::PrintLine()
{
    cout<<"端点为:"<<endl;
    pts.display();
    pte.display();
    cout<<"长度:"<<Lengh()<<endl;
}
void Line::PrintPoint()
{
    cout<<"("<<(pts.getx()+pte.getx())/2<<","<<(pts.gety()+pte.gety())/2<<")"<<endl;
}
int main()
{
    Point pt(-2,5),pe(7,9);
    Line l(pt,pe);
    l.PrintLine();
    cout<<"中点:"<<endl;
    l.PrintPoint();
    return 0;
}



心得体会:这是自己敲出来的,跟老师的有不少不同之处;

/*
* Copyright (c) 2013, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作者:邱学伟
* 完成日期:2013 年 5 月 11 日
* 版本号:v1.0
* 输入描述:无
* 问题描述: 定义点类,并以点类为基类,派生出直线类,从基类中继承的点的信息表示直线的中点
* 程序输出:
* 问题分析:
* 算法设计:略
*/
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
  public:
  Point():x(0),y(0){};
  Point(double x1,double y1)
  {
     x=x1;
     y=y1;
  }
  double getx(){return x;}
  double gety(){return y;}
  void display();
  private:
  double x,y;
};
void Point::display()
{
    cout<<"Point:("<<x<<","<<y<<")"<<endl;
}
class Line:public Point
{
    public:
    Line(Point p1,Point p2);
    double Lengh();
    void PrintLine();
    void PrintPoint();
    private:
    class Point pts,pte;
};
Line::Line(Point p1,Point p2):Point((p1.getx()+p2.getx())/2,(p1.gety()+p2.gety())/2)
{
        pts=p1;
        pte=p2;

}
double Line::Lengh()
{
    double x0=pts.getx()-pte.getx();
    double y0=pts.gety()-pte.gety();
    return sqrt(x0*x0+y0*y0);

}
void Line::PrintLine()
{
    cout<<"端点为:"<<endl;
    pts.display();
    pte.display();
    cout<<"长度:"<<Lengh()<<endl;
}
void Line::PrintPoint()
{
    cout<<"("<<(pts.getx()+pte.getx())/2<<","<<(pts.gety()+pte.gety())/2<<")"<<endl;
}
int main()
{
    Point pt(-2,5),pe(7,9);
    Line l(pt,pe);
    l.PrintLine();
    cout<<"\n The middle point of Line: ";
    l.PrintPoint() ;//输出直线l中点的信息
    return 0;
}


原文地址:https://www.cnblogs.com/xinyuyuanm/p/3073188.html