简体   繁体   English

如何使用多态使复合对象全局化到派生类?

[英]How to make composite objects global to derived classes using polymorphism?

I have written a program that allows users to take out and return vehicles. 我编写了一个程序,允许用户取出和归还车辆。 For example, a user decides to take a car out for personal use, the amount of vehicles left would then be decremented from 2 to 1. The problem is that whenever a new object is created, the amount of vehicles in the composite vehicle class would always be defaulted 2 and when you check out a vehicle, it would only decrement the amount of vehicles for that object created. 例如,用户决定带出一辆汽车供个人使用,然后剩余的车辆数量将从2减少到1。问题是,每当创建一个新对象时,复合车辆类别中的车辆数量就会减少。始终默认为2,并且当您签出车辆时,它只会减少为该对象创建的车辆数量。 I am not so good at explaining so maybe this would help. 我不太善于解释,所以也许会有所帮助。

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
    }

So every time 1 - 4 has been selected a new object will be created and every time there will be a new instance of the composite vehicle class that defaults 2 to the amount of vehicles remaining. 因此,每次选择1-4时,都会创建一个新对象,并且每次都会有一个复合车辆类的新实例,默认情况下剩余车辆数量为2。 What i am looking to do is somehow make the amount of vehicles in class vehicle decrement globally for all objects created so that when the amount of vehicles = 0, a prompt will display such. 我想做的是以某种方式针对创建的所有对象全局降低类车辆中的车辆数量,以便在车辆数量= 0时显示提示。 Is there any way to accomplish this by still having a vehicle class composition in the base class? 有什么办法可以通过在基础班级中保留车辆类组成来实现这一目标的? I am not so sure what code to include that will help so i think these may be helpful. 我不确定要包含的代码会有所帮助,所以我认为这些方法可能会有所帮助。
BASE CLASS: 基本类别:

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

VEHICLE CLASS: (COMPOSITION) 车辆类别:(组成)

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

Sorry for the length of the post. 抱歉,帖子的长度。 Any help would be appreciated. 任何帮助,将不胜感激。

To make the values independent of any instance of the class you can use static variables. 要使值独立于类的任何实例,可以使用静态变量。 You simply declare the variables static in your class and they will be the same variable for all instances of the class. 您只需在类中声明static变量,它们对于该类的所有实例都是相同的变量。 If you change your declarations of sedan , pickup and minivan to 如果您将sedanpickupminivan声明更改为

static int sedan;
static int pickup;
static int minivan;

the variables will be global. 变量将是全局变量。

You will not be able to initialize these in the constructor of the class as doing that will reset the values each time a new instance of vehicle is created. 您将无法在类的构造函数中对其进行初始化,因为这样做将在每次创建新的vehicle实例时重置这些值。 You will have to have an initializer method to initialize the values. 您将必须具有一个初始化方法来初始化值。 So if you create a method like this 因此,如果您创建这样的方法

static void vehicle::init()
{
    sedan = 2;
    pickup = 2;
    minivan = 2;
}

and you call it at the start of your program the values will be initialized as in your code. 并在程序开始时调用它,则值将按照代码中的值进行初始化。

You can read more about static here and storage duration specifiers here . 你可以阅读更多关于static 这里和储存期限符这里


As dwcanillas pointed out, a more oop way of accomplishing this would be to have a garage class that keeps track of some vehicles. 正如dwcanillas指出的那样,实现这一目标的另一种方法是拥有一个可以跟踪某些车辆的garage类。 So you could have a class like this 所以你可以上这样的课

class garage
{
public:
   vehicle getVehicle();
   void returnVehicle();

   garage(int numVehicles);

private:
   int numVehicles;

};

which keeps track of all vehicles, or only keeps track of a specific subset of the vehicles, so you might have one garage for sedans, one for minivans and one for pickups. 它可以跟踪所有车辆,或仅跟踪特定的车辆子集,因此您可能有一个用于轿车的garage ,一个用于小型货车的garage和一个用于皮卡garage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM