作业的下一部分告诉我,类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
内部访问它是理想的吗?还是有更好的方法来做到这一点?否则我看不到在堆上创建它的方法。
转到你的教授,并告诉他们,“堆”在这方面是出moded和不准确的。将它们刺在脸上完成。 – 2011-05-09 10:38:40
我认真地想给你这样的一个给予好评,但很可惜我不能 – Herp 2011-05-09 10:39:33
......那么,你的教授死了吗?如果你没有回复,我会认为你要么进监狱要么妄图破坏这个星球。如果后者是这种情况,请与我联系,我会考虑将你纳入我的阴谋。 – 2011-11-01 04:50:16