2011-05-09 131 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

相关问题