jeudi 27 mai 2021

Why can't I declare a data member from another class private within my class definition [closed]

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 error are below.

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; };

If I make the data member p public in the class circle, everything works. The compiler error I am receiving says Point circle::p is declared private within this context. Also get a compiler error for the member function centre() in circle due to issue with p. Why can I not define the Point object p, in the circle class private?

Aucun commentaire:

Enregistrer un commentaire