-1

I am getting a compiler error saying that the data member Point p is private within the context, when I declare Point p as private within class circle. The code and compiler error are below.

#include<iostream>
#include<vector>
 class Point
 {
   public:
   Point(double a, double b)
{
    x = a;
    y = b;
   
}
   virtual ~Point(){}
   private:
   double x;
   double y;
}; 

The code for the class shape and circle are as follows:

class shapes {
         public:
         virtual Point centre() const = 0;
         virtual void draw() const = 0;
         virtual void rotate(int angle) const = 0;
         virtual ~shapes(){}
 };

class circle: public shapes {
        public:
         Point centre() const override { return p; }
         void draw() const override { }
         void rotate(int angle) const override {}
         virtual ~circle() {}
         circle(Point x, int r):p{x},radius{r}{}
       
         private:
          Point p;
          int radius; };

Edit: Smiley face class inherits from circle class with code below:

class smiley: public circle
{ //smiley face is a circle + eyes and mouth
public:
smiley(Point p, int r):circle{p,r},mouth{nullptr}{}
Point centre() const override { return p;}
void draw() const override 
{
    //draw circle
    circle::draw();
    for(auto e:eyes)
    {
        e->draw();
    }
    mouth->draw();

}
void rotate(int angle) const {}

virtual ~smiley()
{
    delete mouth;
    for (auto eye : eyes) //why not delete [] eyes
    {
        delete eye;
    }
}
private:
std::vector<shapes*> eyes; //smiley face has eyes
shapes* mouth; //smiley face has a mouth
};

If I make the data member p public in the class circle, everything works. The compiler error is listed below: enter image description here

Why can I not define the Point object p, in the circle class private?

Edit: I have added the compiler error message and added the missing code asked for in the comments below. Would you be able to re-open the question?

Don Shanil
  • 161
  • 1
  • 7
  • 1
    Please provide the exact code and location of the error, as displayed by the compiler. There are a lot of details missing here. – Robert Andrzejuk May 27 '21 at 12:38
  • 1
    The problem must be in code you're not showing. – acraig5075 May 27 '21 at 12:49
  • 4
    The code you have shown would not exhibit the symptom you describe. The code you have posted, almost certainly, differs from your actual code presumably because (1) you have deemed some aspects of your actual code irrelevant, but they actually are relevant and (2) you have not tested to see if the code you have posted still exhibits the same concern. That forces people to guess what your actual problem is. To reduce chances of others having to guess, read up on how to provide a [mcve]. – Peter May 27 '21 at 12:49
  • *"... within this context"* What context? What **exact** (verbatim) error message? – Eljay May 27 '21 at 12:52
  • 1
    Off-topic, but don't use plural names, like "shapes", for singular things. You will make your later self very confused. – molbdnilo May 27 '21 at 13:39
  • @Peter yes you are right. I have added the remaining code. Thanks. – Don Shanil May 28 '21 at 00:51
  • @Eljay Have added a screenshot of the error message. – Don Shanil May 28 '21 at 00:51
  • 1
    A `private` member of a base class is inaccessible to any derived class (unless a derived class is declared as `friend`, which is not a good idea). In your case, `p` is a `private` member of `circle`, so is inaccessible to `smiley`. Options to fix include (1) make `centre()` non-virtual in `circle`, so it is not necessary for `smiley` to override it (and not necessary to access `p` to do so) (2) have `smiley::centre()` simply call `circle::centre()` (which is accessible since it is `public`). I would lean to the first. – Peter May 28 '21 at 01:05
  • 1
    Remove the `smiley::centre()` override - it's is not needed because it's already overridden by `circle` in the inheritance tree. – acraig5075 May 28 '21 at 06:08
  • I have voted to reopen. – acraig5075 May 28 '21 at 06:18
  • 1
    Does this answer your question: [Difference between private, public, and protected inheritance](https://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance) – Robert Andrzejuk May 28 '21 at 07:44
  • @Peter Thanks Peter, yes this answers the question. Thanks for highlighting the different solutions and the relative merits of each. Very helpful - if you think its worthwhile to others as well, then do post it as an answer and will accept it. – Don Shanil May 28 '21 at 14:53

1 Answers1

0

Private class members can only be accessed within the class or by friends, so, if you would like it to be accessed outside the class by a non-friend, you would need to use a setter/getter.

AE-H
  • 23
  • 6
  • This exact code, without trying to access p from outside the class, does not produce an error and it shouldn't. – AE-H May 27 '21 at 14:09