There is a class TFigure which should refer to its descendants TRectangle and TTriangle for the variables ry1 , rx3 and txN , tyN (where N is a number from 1 to 3, defining a pair of points.) It is necessary from the class TFigure using the IsIntersect method to refer to the classes TTriangle and TRectangle for coordinates points for processing through the block conditions. Codes:

TFigure.cpp

 #include "stdafx.h" #include "TFigure.h" #include <iomanip> using namespace std; TFigure::TFigure() { StepX = 0; StepY = 0; } TFigure::~TFigure() { } void TFigure::Move() { } void TFigure::IsIntersect() { //for (int iy = ry1; iy < ry3; iy++) // переменная ry1 И ry3 определена в классе TRectangle. Как дать знать классу TFigure про их существование и чему они равны? //{ //} } 

TRectangle.cpp

 ... TRectangle::TRectangle() { rx1 = 0; // x1,y1 x2,y2 ry1 = 0; // x3,y3 x4,y4 rx2 = 10; ry2 = 0; rx3 = 0; ry3 = 4; rx4 = 10; ry4 = 4; // rStepX = 0; // rStepY = 0; } TRectangle::~TRectangle() { } ... 
  • "N is a number from 1 to 3 defining a pair of points" - a pair of numbers? - Igor
  • one
    What does it mean to apply for variables? Do you want the parent class to refer to the descendant class members? And how could he even know about them? There is no ZAGS in C ++, the parent knows nothing about the descendants ... - Harry
  • No, that won't work. Ancestor class should not know anything about descendants. - iksuy
  • Let's start with the architecture questions: what will the base class do with this data? - free_ze
  • What will this parent have to do if I inherit from him, and I will not define your variables? =) - vp_arth

2 answers 2

In the TFigure class, TFigure purely virtual (abstract) method that returns the coordinates of a point by index, and implement (implement?) It in the heirs of TRectangle and TTriangle .

The same is for the number of points in the shape.

    Option

    Any shape on a plane can be expressed as a collection of curves, or segments, in fact, of degenerate curves. Get a container in the base class to hold this, let std :: vector.

    The curves (container element) can be described by a triple std :: tuple, where the first and second values ​​of the tuple are the domain of the function, the third value is the symbolic representation of the function.

    In order to find all the intersection points of one figure with another - it is necessary to analyze in pairs the segments of the compared figures.

    The main difficulty in this approach will be the parsing of textual formulas for later use. Nevertheless, I am sure it will pay off in the future. On the example of the school program, the intersection of two segments:

     y=0.5x+0.5 у=-3x-10 

    We get x=-3 , у=-1

    Plus: we get rid of the "raster" representation of figures, and this is getting rid of data redundancy, and the time to process them. Less: coding complexity of mathematical processing.

    • Of course this is a good answer, but not to this question. I need to contact the members of the class from the parent. I don’t understand how to do a damn thing. It is clear that suddenly, from nowhere, you can’t arrogate yourself from anywhere, and therefore I’m looking for those magic words that will describe the variables in the parent class, because I’m not quite sure how to describe them there. Although cyberforum suggests using signals or getters and setters. But in my opinion, it will be easier for me to simply describe these variables in the parent and then access them from the descendants. Although, in my opinion, this is “the same soup and the thick of it.” - MarKo
    • "Parent" about the descendants, as written above, does not know. How many of those descendants will still be. As an option, declare in the parent class some kind of storage in order to be able to fill / supplement it in descendants. The option is not the best - but possible std::map<std::string,std::any> . Keep anything and anytime. - Majestio
    • Perhaps, perhaps ... I basically went the other way. In the parent, he defined variable members for everyone and everything and then from the descendants he called them. What is the difference where they are declared in this case? in the class or in the parent? None (as I understand it) The only thing that I understood was that the IsIntersect method should be made virtual and then EXTREMELY redefined in descendants. Tomorrow I go to the Paralympics Programming ... - MarKo
    • This approach is called “the creation of a God-class”)) This is a typical paraolympic error)) - Majestio