"Friend" of my class :

I have a Circle class,and there are two derived classes from this base class. They are class: Sphere and class:cylnder.
Of class Sphere - it has a public function that calls a public function from Circle for the radius. Do I need to list the Sphere function as a friend in Circle class, or is inheritance enough for this.

Thank You in advance,
Bob
Last edited on
Hi,

IMO you should not do inheritance or friends between these 2 at all. Inheritance implies an IS A relation: a sphere is not a circle. Have a look at the Liskov Subtitution Principle:

https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)

Another way to do is:

1
2
3
4
5
6
7
8
class DrawingEntity;

class Shape2D : public DrawingEntity;
class Solid3D : public DrawingEntity;

class Circle : public Shape2D;

class Sphere : public Solid3D;
Last edited on
OP: you could also consider containment in that both sphere and cylinder 'contains' a circle, thereby passing the radius of the circle to the ctor's of these types:
1
2
3
4
struct Circle{double m_radius; Circle(const double& radius) : m_radius(radius){}};

struct Sphere{Circle c; /*other methods*/};
struct Cylinder{Circle c; double m_height;/*other methods*/};
Last edited on
Topic archived. No new replies allowed.