2014-10-28 106 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'; 
} 
相關問題