The task is to create a trapezoid class with coordinates of points in the form of "x" and "y". Created a class "Point" which has 2 variables x and y. When accessing them, the error "member Trapezoid :: A unavailable" occurs.

class Point { public: int x, y; }; class Trapezoid { Point A = Point(); Point B = Point(); Point C = Point(); Point D = Point(); Point H = Point(); double GetAB() { return sqrt(pow(Bx - Ax, 2) + pow(By - Ay, 2)); } double GetBC() { return sqrt(pow(Cx - Bx, 2) + pow(Cy - By, 2)); } double GetCD() { return sqrt(pow(Dx - Cx, 2) + pow(Dy - Cy, 2)); } double GetDA() { return sqrt(pow(Ax - Dx, 2) + pow(Ay - Dy, 2)); } double Area() { return GetAB() * GetBC(); } double Perimetr() { return (GetAB() + GetBC()) * 2; } }; int main() { Trapezoid trapezoid = Trapezoid(); trapezoid.Ax = 1; } 
  • Point A = Point(); and we are definitely writing C ++ and not Java?) In general, just write Point A - pavel
  • I was simple with Sharpy started here and here it is)) - GameMaster

2 answers 2

Data members of the Trapezoid class

 class Trapezoid { Point A = Point(); Point B = Point(); Point C = Point(); Point D = Point(); Point H = Point(); // ... 

are private (that is, for a class ( class ) they have access control private by default). Therefore, this call in main to a private member of class A

 trapezoid.Ax = 1; 

not correct.

You could declare a separate metol that sets trapezoid points. For example,

 void setABCD( Point a, Point b, Point c, Point d ); 

Note that instead of initializing the members of the class as

 Point A = Point(); Point B = Point(); Point C = Point(); Point D = Point(); Point H = Point(); 

could write

 Point A = {}; Point B = {}; Point C = {}; Point D = {}; Point H = {}; 

And all your methods should be declared with const qualifier. For example,

 double GetAB() const; 

    public you did not write? Then you cannot use members (which are private in the class, unlike the structure, by default), except in the class itself ...

    And further

     Point A = Point(); 

    it's just too much. All these A and others like them will be initialized by the default constructor.