The next part of the assignment tells me that the Class RacingCar
contains four wheel objects as defined by class Wheel. Implement the wheels as an array of objects on the heap.
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;
}
Is it ideal to build a class like this, and then access it inside RacingCar
? Or is there a better way to do so? I can't see a way to create it on the heap otherwise.
Implement the wheels as an array of objects on the heap.
Kind of pointless really, but it's an exercise, so ...
the assignment tells me that the Class "RacingCar" contains four wheel objects as defined by class Wheel.
When your assignment says "contains four wheel objects" you can translate this requirement to "the wheel objects should be a member of my class".
I assume your instructor expects you to initialize the wheel-object-array in the ctor of the RacingCar class and then release (delete[]
) it in the dtor of the RacingCar class.
Note, however, that the correct way to do this would be:
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.
...
Even if you really must allocate the Wheel objects on the heap, you still wouldn't use delete[]
, but be better served to use a tool such as boost::scoped_array
.
Let's flesh this out a bit, since we want complete answers on SO, even for "homework" questions, right?
As mentioned above, modeling containment is usually done via members of a class, although in this case the member of the class will probably be some kind of array.
Given the requirement to allocate Wheel on the heap (while it doesn't make sense for the toy example, there are a lot of legitimate use-cases to have objects of a class allocated on the heap in a member-scenario) -- I'd say the following solutions are Good Style:
This gives you an array of exactly 4 heap allocated objects. You will not need to free these explicitly:
class RacingCar { ... boost::scoped_array<Wheel> wheels; ... RacingCar() : wheels(new Wheel[4]) { } ...
This uses 4 separate members, which may make sense in some use cases, where you have members of the same class, but don't use them uniformly:
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) { }
If you want to use variable size, you'll do:
class RacingCar { ... boost::ptr_vector<Wheel> wheels; ... RacingCar() { for(size_t i=0; i<4; ++i) { wheels.push_back(new Wheel); } } ...
And finally, if you don't have boost but plain C++, I'd do: (Oh, and note how in the 1st version of this I forgot to add the copy ctor operator. That's the reason you do not go about messing with raw pointers and delete. You'll always forget sth. ;-)
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. ...
The assignment means that the array of Wheel
s should be part of class RacingCar
, like
class RacingCar {
public:
...
private:
...
Wheel *wheels;
};
and you should allocate it in the constructor, then destroy it in the destructor.
I think it's better to add the "*heapArray" as an attribute of RacingCar and creating the wheels in its constructor.
精彩评论