c++ - How to call different derived classes in constructors of classes derived from a different base class -


In C ++, I have two separate base classes, whose derivative classes are coupled together Here's an example for what kind of thing:

Firstly define a set of classes, e.g.,:

  class shape {public: double area; Double diameter; }; Square rectangle: public size {public: double width; Double height; }; Square circle: public size {public: double radius; };  

The second set of classes is then related to the operation done on the first set of classes, something like this:

  class calculator {public: static calculator * Create_calculator (size * size, const char * shape_type); // factory virtual zero calculation () = 0; // real calculation}; Classroom Area: Public Calculator {Class Circles * Circle; Public Sector Cabinets (Circles * Board) {this- & gt; Circle = circle; } Zero calculation () {it-> Area = PI * pow (circle-> radius, 2); }} Square area_kale: public calculator {square rectangles * rectangle; Public area_kalines (rectangular * rectangular) {it- & gt; Rectangle = rectangular; } Double count () {it-> Area = rectangle-> height * rectangle-> width; }} Calculator * Calculator :: create_calculator (size * size, const char * shape_type) {if (shape_type == "circle") returns new area shapes (shape); If (size_type == "rectangular") changes the new area (size); }  

Again, the idea will be to call all this way:

  rectangles * my_rectangle; Calculator * Area_acculator; Area_calculator = area_colator-> gt; Clalkulator (my_rectangle, "rectangle"); Area_calculator-> Calculation (); However, it does not compile and I get an error (quite intelligently) to describe how a member of the shape class is not "width" and "value of size" Size * "No type of unit can be assigned" rectangle. "The error is clear why this code is not working.  

Would anyone know that to get the code here What am I trying to do?

From a design perspective, I understand that the part of this problem is that the derivative classes are coupling, so there is a better way of trying to reduce the calculator class from the shape class. I have taken this approach at least in my implementation I also want to experiment, and for that I need the code to compile.

Not sure what you are here I am trying to cobble but I think the more general approach is to do something like this:

  class shape {public: virtual ~ shape () {} / / concrete sections to be unwanted Should not t this function virtual double get_area () const = 0; }; Square circle: public size {double diameter; Public: circle (double diameter): diameter (diameter) {} virtual double get_area () const {return M_PI * diameter * diameter / 4; }}; Square rectangle: Public shape {double width; Double height; Public: Rectangle (double width, double height): width (width), height (height) {} virtual double get_area () const {Return width * height; }}; Int main () {Circle C (2); Rectangle R (20, 40); // Use shape reference (or hint) to open polymophic behavior Size & amp; Cs = c; Size & amp; RS = R; Std :: cout & lt; & Lt; "Area of ​​Circle:" & lt; & Lt; Cs.get_area () & lt; & Lt; '\ N'; Std :: cout & lt; & Lt; "Area of ​​rectangular:" & lt; & Lt; Rsget_area () & lt; & Lt; '\ N'; }  

Comments