我写了一个程序,允许用户取出和返回车辆。例如,用户决定将车出去供个人使用,剩下的车辆数量将从2减少到1.问题是无论何时创建新物体,复合车辆类别中的车辆数量都会永远是默认2,当你检查车辆时,它只会减少创建的对象的车辆数量。我不太擅长解释所以也许这会有所帮助。
int main(){
int a = 0;
int vn;
int counter = 0;
VMS *tracking[20];
while (a != 6){
cout << "(1) Company (2) Personal (3) Carpool (4) Cargo (5) Return a vehicle (6) Exit" << endl << "Select the appropriate application: ";
cin >> a;
switch (a){
case 1: tracking[counter] = new company(); cout << "YOU ARE USER #" << counter << " PLEASE REMEMBER YOUR USER NUMBER"; counter++; break;
case 2: tracking[counter] = new personal(); cout << "YOU ARE USER #" << counter << " PLEASE REMEMBER YOUR USER NUMBER"; counter++; break;
case 3: tracking[counter] = new carpool(); cout << "YOU ARE USER #" << counter << " PLEASE REMEMBER YOUR USER NUMBER"; counter++; break;
case 4: tracking[counter] = new cargo(); cout << "YOU ARE USER #" << counter << " PLEASE REMEMBER YOUR USER NUMBER"; counter++; break;
case 5: cout << "Please enter your User #: "; cin >> vn; tracking[vn]->returned(); break;
case 6: exit(1); break;//exit
}
因此,每次选择1 - 4时,都会创建一个新对象,并且每次都会有一个复合车辆类的新实例,默认值为剩余车辆数量的2。我想要做的是以某种方式使所有创建的对象全局减少类车辆的车辆数量,这样当车辆数量= 0时,将显示这样的提示。有没有办法通过在基类中拥有车辆类组成来实现这一目标? 我不太确定要包含哪些代码会有所帮助,所以我认为这些可能会有所帮助 基础课程:
class VMS{
public:
VMS(){}
virtual void set();
virtual void print();
virtual void returned();
virtual void setV();
private:
string purpose;
string dName;
int emplidNum;
string date;
string Time;
string rtime;
vehicle vehicle;//Composition of vehicle class
};
#endif
车辆类:(组成)
class vehicle{
public:
vehicle() { sedan = 2, pickup = 2, minivan = 2; }
bool empty1(){
if (sedan == 0){
cout << "All Vehicles of this type are in use, please select a different vehicle" << endl;
return true;
}
else
return false;
}
bool empty2(){
if (pickup == 0){
cout << "All Vehicles of this type are in use, please select a different vehicle" << endl;
return true;
}
else
return false;
}
bool empty3(){
if (minivan == 0){
cout << "All Vehicles of this type are in use, please select a different vehicle" << endl;
return true;
}
else
return false;
}
//the vehicle objects
int sedan;
int pickup;
int minivan;
};
#endif
对不起该帖子的长度。任何帮助将不胜感激。