Как создать экземпляр объекта класса, который будет генерировать и возвращать объект другого класса?

0

Это реальный вопрос. Создайте объект данного класса: AirPlaneGenerator, который будет генерировать и возвращать объект AirPlane всякий раз, когда истекло время интервала (заданного во время создания экземпляра). ПРИМЕЧАНИЕ: генератор возвращает NULL между интервалами; что ни один самолет в настоящее время не приземляется. Это файл cpp для файла генератора аэропорта

    #include "airplanegenerator.h"
        // initialize the static list of tasks
    vector<AirPlane> AirPlaneGenerator::lstPlanes(0);
    // -- Constructors --
     // default constructor
    AirPlaneGenerator::AirPlaneGenerator(int i) {
// initialise all airplanes within the list
this->interval = i;
this->cursor = 0;
this->elapsed = 0;

     AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new      

      AirPlane(JFF_GOVERNMENT));

      AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new    
    AirPlane(JFF_MAIL));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new  
    AirPlane(JFF_VIP));

    AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new  
     AirPlane(JFF_MAIL));
 AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new   
     AirPlane(JFF_MAIL));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new   
     AirPlane(JFF_GOVERNMENT));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new   
     AirPlane(JFF_VIP));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new   
     AirPlane(JFF_VIP));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
     AirPlane(JFF_MAIL));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new   
     AirPlane(JFF_SMALLPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new  
    AirPlane(JFF_SMALLPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_SMALLPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new  
    AirPlane(JFF_VIP));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_LARGEPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_MAIL));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
     AirPlane(JFF_LARGEPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new    
    AirPlane(JFF_SMALLPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_GOVERNMENT));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
     AirPlane(JFF_MAIL));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
     AirPlane(JFF_LARGEPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
     AirPlane(JFF_GOVERNMENT));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
     AirPlane(JFF_GOVERNMENT));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_LARGEPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_SMALLPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new     
    AirPlane(JFF_MAIL));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_MAIL));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_MAIL));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_SMALLPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_VIP));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_GOVERNMENT));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_LARGEPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_GOVERNMENT));
cout << " GENERATOR: AirPlanes Are Ready For Landing." << endl;
}
// -- Other Interfaces --
// get the next task in the list
AirPlane* AirPlaneGenerator::getNext() {
     // find out the current elapsed time
    this->elapsed += 1;
    // return an airplane if the elapsed time exceeds the current cursor position
     if (this->elapsed / this->interval >= 1) {
    this->elapsed -= this->interval;
    if (this->cursor < TOTALPLANES) {
        cout << " GENERATOR: AirPlane# " <<   
AirPlaneGenerator::lstPlanes[this->cursor].getIdnum() << " Now Landing." << endl;
        return &(AirPlaneGenerator::lstPlanes[this->cursor++]);
    } else {
        cout << " GENERATOR: No More AirPlanes In The Air To Land." << endl;
        return NULL;
    }
    } else {
    cout << " GENERATOR: No AirPlane Landing Now." << endl;
    return NULL;
   }
}
  • 0
    Я знаю, как сделать объект, но я просто не понимаю, чего хочет от меня вопрос
Теги:
queue
instantiation

1 ответ

0

Вопрос: вам нужно реализовать Creational Pattern (Factory Pattern), который будет создавать объект класса после заданного интервала времени.

  • 0
    спасибо за ответ .. во-первых, я слышу об этом заводском образце .. прочтем
  • 0
    Это шаблон дизайна, который действительно легко понять.
Показать ещё 1 комментарий

Ещё вопросы

Сообщество Overcoder
Наверх
Меню