I am pretty sure it isn't but I am going to ask anyway...
Here's an example:
let's say I have a class called Elipse
:
class Elipse
{
private:
double d_m_lAxis; // the length of the large Axis of the elipse
double d_m_sAxis; // short axis
//Point is a struct of 2 doubles (x and y)
Point P_m_focal1; //focal point 1
Point P_m_focal2; //focal point 2
protected:
Point P_m_center;
public:
Elipse(Point _P_lowerLeft,Point _L_upperRight)
{
//draw elipse in a rectangle, just like in paint
}
}
Now I want to inherit Circle
from Elipse
(because Circle
is an Elipse
with both Axis
of the same length):
class Circle : public Elipse
{
private:
double d_m_radius;
public:
Circle(Point _P_lowerLeft,double _d_diameter) :
Elipse(_P_lowerLeft,/*get the upper ight*/
Point(_P_lowerLeft.x + _d_diameter,_P_lowerLeft.y + _d_diameter))
{
// draw a circle in a square, just like in paint
}
}
So now I got the class I wanted and it doesn't have the access to any of the private members of the Elipse
which would become redundant in Circle
.
However, as far as I know these members are still inherited and the child class simply does not have access to them. This makes the class unnecessarily large in size and it is also possible to access those members via functions inherited from Elipse
, unless they are overriden.
Is there a way to not inherit a member at all?
The mathematical phrasing "a circle is an ellipse where [...]" should be rephrased to the more type aware "For every circle, there exists an ellipse where [...], that describes the same set of points".
With that in mind, your implementation should allow the creation of
Ellipse
objects fromCircle
objects, but not polymorphic substitution.One such possibility, is an explicit conversion operator: