点类为基类声明为共有继承调用基类的私有成员

  • Post author:
  • Post category:其他


#include <iostream>
#include<cmath>
using namespace std;
class Point
{
    public:Point():x(0),y(0){};
    Point(double x0,double y0):x(x0),y(y0){};
    double returnx(){return x;}
    double returny(){return y;}
    void PrintPoint();
    private :
    double x,y;
};
class Line:public Point
{
    public:
    Line(Point pt,Point ps):pts(pt),pte(ps){};
    void PrintLine();
    private:
    class Point pts,pte;
};
void Point::PrintPoint()
{
    cout<<"("<<x<<","<<y<<")"<<endl;
}
void Line::PrintLine()
{
    double b;
    cout<<"直线两端点为:";
    pts.PrintPoint();
    pte.PrintPoint();
    cout<<endl<<"直线长度为:";
    b=sqrt((pts.returnx()-pte.returnx())*(pts.returnx()-pte.returnx())+
               (pts.returny()-pte.returny())*(pts.returny()-pte.returny()));
    cout<<b;
    cout<<endl<<"直线中点为:";
    cout<<"("<<(pts.returnx()+pte.returnx())/2<<","<<(pts.returny()+pte.returny())/2<<")";
}

int main()
{
    Point ps(-2,5),pe(7,9);
    Line l(ps,pe);
    l.PrintLine();
    return 0;
}




版权声明:本文为yang1067155909原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。