Monday 15 April 2013

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



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

in c++, have 2 separate base of operations classes, each of derived classes coupled. here's illustration kind of thing i'd do:

first define set of classes, e.g.,:

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

the sec set of classes pertains operations beingness performed on first set of classes, this:

class calculator { public: static calculator *create_calculator(shape *shape,const char *shape_type); // mill virtual void calculate()=0; // actual calculation }; class area_circles : public calculator { class circles *circle; public area_circles(circles *circle) { this->circle = circle; } void calculate() { this->area = pi*pow(circle->radius,2); } } class area_rectangles : public calculator { class rectangles *rectangle; public area_rectangles(rectangles *rectangle) { this->rectangle = rectangle; } double calculate() { this->area = rectangle->height * rectangle->width; } } calculator *calculator::create_calculator(shape *shape, const char *shape_type) { if (shape_type=="circle") homecoming new area_circles(shape); if (shape_type=="rectangle") homecoming new area_rectangles(shape); }

then, thought phone call using like:

rectangles *my_rectangle; calculator *area_calculator; area_calculator = area_calculator->create_calculator(my_rectangle, "rectangle"); area_calculator->calculate();

however, doesn't compile , error (quite sensibly) pointing out how shape class has no fellow member "width", , "a value of type "shape *" cannot assigned entity of type "rectangles". error's pretty clear on why code isn't working.

would know how code here i'm trying do?

from design perspective, recognize part of problem derived classes end beingness coupled, maybe there improve way seek decouple calculator class shape class. i'd @ to the lowest degree seek out approach while in implementation, , need code compile.

i not exclusively sure trying accomplish here think more usual approach this:

class shape { public: virtual ~shape() {} // concrete classes must implement function virtual double get_area() const = 0; }; class circle : public shape { double diameter; public: circle(double diameter): diameter(diameter) {} virtual double get_area() const { homecoming m_pi * diameter * diameter / 4; } }; class rectangle : public shape { double width; double height; public: rectangle(double width, double height): width(width), height(height) {} virtual double get_area() const { homecoming width * height; } }; int main() { circle c(2); rectangle r(20, 40); // utilize shape references (or pointers) invoke polymorphic behaviour shape& cs = c; shape& rs = r; std::cout << "area of circle : " << cs.get_area() << '\n'; std::cout << "area of rectangle: " << rs.get_area() << '\n'; }

c++ factory strategy-pattern decoupling

No comments:

Post a Comment