11.3点クラス派生直線クラス


/*
* Copyright (c) 2014,          
* All rights reserved.
*       :  
*     :2014   5   7  
*      :v1.0
*     :  
*     :         
*     : 
*     : 
*     : 
*/
#include<iostream>
#include<Cmath>
using namespace std;
class Point //      
{
public:
    Point():x(0),y(0) {};
    Point(double x0, double y0):x(x0), y(y0) {};
    void PrintPoint(); //      
    int getx()
    {
        return x;
    }
    int gety()
    {
        return y;
    }
protected:
    double x,y;   //         
};
void Point::PrintPoint()
{
    cout<<"   Point: ("<<x<<","<<y<<")";    //   
}
class Line: public Point   //           ,                
{
public:
    Line(Point pts, Point pte); //    ,                        
    double Length();    //          
    void PrintLine();   //              
private:
    class Point pts,pte;   //       , Point               
};
Line::Line(Point pts, Point pte):pts(pts),pte(pte){}
double Line::Length()
{
    double m,n,s;
    x=(pts.getx()+pte.getx())/2;
    y=(pts.gety()+pte.gety())/2;
   m=(pts.getx()-pte.getx())*(pts.getx()-pte.getx());
   n=(pts.gety()-pte.gety())*(pts.gety()-pte.gety());
   s=sqrt(m+n);
   return s;
}
void Line::PrintLine()
{
    cout<<"     :"<<"("<<pts.getx()<<","<<pts.gety()<<")"<<endl;
    cout<<"("<<pte.getx()<<","<<pte.gety()<<")"<<endl;
    cout<<"     :"<<Length()<<endl;
}
int main()
{
    Point ps(-2,5),pe(7,9);
    Line l(ps,pe);
    cout<<"About the Line: "<<endl;
    l.PrintLine();  //    l   :      
    cout<<"The middle point of Line is: ";
    l.PrintPoint(); //    l     
    return 0;
}