2014-10-28 105 views
-2

在C++中,我有两个独立的基类,每个派生类都有些耦合。下面是这种东西一个例子,我想这样做:如何从派生自不同基类的类的构造函数中调用不同的派生类

class Shape 
    { 
    public: 
     double area; 
     double diameter; 
    }; 

class Rectangle : public Shape 
    { 
    public: 
     double width; 
     double height; 
    }; 

class Circle : public Shape 
    { 
    public: 
     double radius; 
    }; 

第二套类则涉及到运营这个第一执行

首先定义了一组类,如,:组类,像这样:

class Calculator 
    { 
    public: 
     static Calculator *create_calculator(shape *shape,const char *shape_type); // the factory 
     virtual void calculate()=0; // the 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") 
     return new area_circles(shape); 

    if (shape_type=="rectangle") 
     return new area_rectangles(shape); 
    } 

然后,想法是将调用所有这使用类似:

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

但是,这不会编译,我得到一个错误(相当明智)指出如何Shape类没有成员的“宽度”,并且“一个类型的值”形状*“不能被赋予一个类型的实体”矩形”。该错误很明显,为什么这个代码不起作用。

有人会知道如何让代码在这里做我想做的事吗?从设计的角度来看,我认识到问题的一部分是派生类最终被耦合,所以也许有更好的方法来尝试将计算器类从形状类中分离出来。但我想至少在我的实现中尝试这种方法一段时间,为此我需要编译代码。

+0

你为什么不使用'struct'时是你的描述?顺便说一下:为什么“直径”和“半径”?另外,'area'和'diameter'几乎可以让你得到'width'和'height',你只需要一个位来决定哪个(可能)更大。 – Deduplicator 2014-10-28 00:50:54

+0

您的代码与您的描述不符。你的第一块代码声明了“Rectangle”和“Circle”类。你的第二块代码引用“矩形”和“圆圈”类。您声明的第三个代码块因涉及“宽度”的错误而无法编译,似乎没有任何对“宽度”的引用。你需要重新组织这个问题,并且确切地说明你的问题是什么。 – 2014-10-28 00:53:11

+3

是否有理由将面积计算与形状分离开始,而不是仅将面积计算作为由每个形状子类实现的虚拟方法? – paisanco 2014-10-28 00:58:50

回答

0

我不完全相信你正试图在这里实现什么,但我觉得更常见的做法是这样的:

class Shape 
{ 
public: 
    virtual ~Shape() {} 

    // concrete classes must implement this function 
    virtual double get_area() const = 0; 
}; 

class Circle 
: public Shape 
{ 
    double diameter; 

public: 
    Circle(double diameter): diameter(diameter) {} 

    virtual double get_area() const 
    { 
     return 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 
    { 
     return width * height; 
    } 
}; 

int main() 
{ 
    Circle c(2); 
    Rectangle r(20, 40); 

    // use Shape references (or pointers) to 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'; 
} 
相关问题