作業的下一部分告訴我,類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