2011-05-09 144 views
0

作業的下一部分告訴我,類RacingCar包含由輪類定義的四個輪對象。將輪子作爲堆上的一組對象來實現。如何從另一個班級訪問班級?

class RacingCar{ 
    int speed; 

public: 
    void Accelerate(int value) { 
     speed = speed + value; 
    } 

    void Brake(int value) { 
     speed = speed - value; 
    } 

    void Turn(int value) { 
     speed = value/4; 
    } 

    void Print(){ 
     cout << "The current KM/h of the car is: " << speed; 
    } 
}; 

class Wheel { 
    int *ptrSize; 
    int pressure; 

public: 
    Wheel() : pressure(32) { 
     ptrSize = new int(30); 
    } 

    Wheel (int s, int p) : pressure(p) { 
     ptrSize = new int(s); 
    } 

    ~Wheel() { 
     delete ptrSize; 
    } 

    void pump(int amount) { 
     pressure += amount; 
    } 

    void print() { 
     cout << pressure; 
    } 
}; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    Wheel *heapArray = new Wheel[4]; 
    RacingCar F1; //Creating a "Formula 1" test car 

    //Test parameters 
    F1.Accelerate(10); 
    F1.Brake(50); 
    F1.Turn(180); 
    F1.Print(); //Checks to see if the car changes actually worked 

    getch(); 

    delete [] heapArray; //Delete when you're done 
    return 0; 
} 

建立這樣的班級,然後在RacingCar內部訪問它是理想的嗎?還是有更好的方法來做到這一點?否則我看不到在堆上創建它的方法。

+0

轉到你的教授,並告訴他們,「堆」在這方面是出moded和不準確的。將它們刺在臉上完成。 – 2011-05-09 10:38:40

+0

我認真地想給你這樣的一個給予好評,但很可惜我不能 – Herp 2011-05-09 10:39:33

+0

......那麼,你的教授死了嗎?如果你沒有回覆,我會認爲你要麼進監獄要麼妄圖破壞這個星球。如果後者是這種情況,請與我聯繫,我會考慮將你納入我的陰謀。 – 2011-11-01 04:50:16

回答

3

將輪子實現爲堆中對象的數組。

毫無意義的種類真的,但它是一個鍛鍊,所以......

分配告訴我,該班「RacingCar」包含類輪定義四輪對象。

當你的作業說「包含四輪對象」可以翻譯這個要求「輪的對象應該是一個會員我的課的」。

我假設你的老師希望你初始化RacingCar類的ctor中的wheel-object-array,然後在RacingCar類的dtor中釋放它(delete[])。

但是請注意,正確的方式做,這將是:

class Wheel; 

class RacingCar { 
... 
std::vector<Wheel> wheels; // if you need a variable amount of wheels 
Wheel wheels[4]; // if you need exactly 4 wheels. 
... 

即使你真的必須在堆中分配的對象車輪,你還是不會用delete[],但得到更好的服務使用諸如boost::scoped_array之類的工具。


讓我們充實這個了一下,因爲我們想在如此完整的答案,即使是「功課」的問題,對不對?

如上所述,建模約束通常是通過一個類的成員來完成的,儘管在這種情況下該類的成員可能是某種數組。

鑑於要求在堆上分配輪子(雖然它對玩具示例沒有意義,但有很多合法的用例將成員的堆中的對象分配到成員堆中-scenario) - 我下面要說的解決方案是良好作風:

  • 這給你正好是4堆上分配對象的數組。你會不會需要明確地釋放這些:

    class RacingCar { 
    ... 
    boost::scoped_array<Wheel> wheels; 
    ... 
    RacingCar() 
    : wheels(new Wheel[4]) 
    { } 
    ... 
    
  • 它使用4個獨立的成員,這可能是有意義的一些使用情況下,如果你有相同類的成員,但不統一使用它們:

    class RacingCar { 
    ... 
    boost::scoped_ptr<Wheel> front_left; 
    boost::scoped_ptr<Wheel> front_right; 
    boost::scoped_ptr<Wheel> rear_left; 
    boost::scoped_ptr<Wheel> rear_right; 
    ... 
    RacingCar() 
    : front_left(new Wheel) 
    , front_right(new Wheel) 
    , rear_left(new Wheel) 
    , rear_right(new Wheel) 
    { } 
    
  • 如果你想使用可變大小,你會做:

    class RacingCar { 
    ... 
    boost::ptr_vector<Wheel> wheels; 
    ... 
    RacingCar() { 
        for(size_t i=0; i<4; ++i) { 
        wheels.push_back(new Wheel); 
        } 
    } 
    ... 
    
  • 而且最後,如果你沒有提升,但普通的C++,我會做:(呵呵,注意如何在此我的第一個版本忘記添加拷貝構造函數運算符。這就是你不去搞亂原始指針和刪除的原因。你會永遠忘記某事。 ;-)

    class RacingCar { 
    ... 
    Wheel* wheels; 
    ... 
    RacingCar() 
    : wheels(new Wheel[4]) 
    { } 
    ~RacingCar() { 
        delete[] wheels;  
    } 
    private: 
    // Block copy operations. These would need to be 
    // implemented properly for the wheels member. 
    RacingCar(RacingCar const&); // no impl. 
    RacingCar& operator=(RacingCar const&); // no impl. 
    ... 
    
+0

@Martin哇,謝謝你簡潔明瞭的解釋。我試圖弄清楚如何將我現在作爲一個類的方式轉移到該構造函數中。有點難以得到我的頭,雖然到達那裏 – Herp 2011-05-09 10:58:19

+0

@Martin我在這裏錯過了什麼嗎?如果我把代碼如: '車輪[4]車輪;'在中,我收到一個錯誤「Exciated a';'」 – Herp 2011-05-09 11:31:05

+0

@Herp:我修復了錯字。它應該是'車輪輪轂[4];' – 2011-05-09 11:44:01

2

分配意味着Wheel在數組應該是RacingCar類的一部分,就像

class RacingCar { 
public: 
    ... 
private: 
    ... 
    Wheel *wheels; 

}; 

,你應該在構造函數分配它,然後在析構函數銷燬它。

+0

我明白這一點的方式是在構造函數中我創建了一個方法,而不是一類和引用這個,基本上是代替類我有一個構造函數? 我很抱歉,我沒有說我是一個網絡工程師! : - /非常感謝您的幫助 – Herp 2011-05-09 10:51:40

+0

@Herp - 要分配它,你必須記住,你是分配的內存中的數組,並與刪除相同。有關解釋請參見http://www.fredosaurus.com/notes-cpp/newdelete/50dynamalloc.html。 – Dennis 2011-05-09 11:09:40

1

我認爲最好將「* heapArray」添加爲RacingCar的屬性並在其構造函數中創建輪子。

+0

'heapArray'。不是'* heapArray'。 – 2011-05-09 10:39:45

相關問題