#include <iostream>
#include <cmath>
using namespace std;
class Point
{
protected:
double x,y,z;
double mag;
public:
Point(double a=0.0, double b=0.0, double c=0.0) : x(a), y(b), z(c)
{
mag = sqrt((x*x)+(y*y)+(z*z));
}
friend ostream& operator<<(ostream& os, const Point& point);
};
ostream& operator<<(ostream& os, const Point& point)
{
os <<"("<<point.x<<", "<<point.y<<", "<<point.z<<") : "<<point.mag;
return os;
}
class ePlane : public Point
{
private:
Point origin;
public:
static double distance(Point a, Point b);
ePlane() : origin(0,0,0){}
};
double ePlane::distance(Point a, Point b) //does not compile
{
return sqrt(pow((a.x-b.x),2)+pow((a.y-b.y),2)+pow((a.z-b.z),2));
}
int main()
{
Point a(3,4,0);
Point b(6,8,0);
cout <<a<<endl;
cout <<b<<endl;
cout <<ePlane::distance(a,b)<<endl;
return 0;
}
The above program does not compile when data members double x,y,z
of class Point
are declared protected
. I do not understand why it does not compile, since protected members of base class should be accessible in derived class ePlane
I do not want to use a friend function to implement this, since protected members should already be accessible