Question 1 of 1 25 Points Consider the following definition and implementation of FortuneCookie to answer the questions
Posted: Sat Feb 19, 2022 3:21 pm
Question 1 of 1 25 Points Consider the following definition and implementation of FortuneCookie to answer the questions that follow: //definition class Fortune Cookie: public QDialog { Q_OBJECT private: QStringList messages; public: Fortune Cookie (); public slots: void show Fortune (); }; 1/implementation Fortune Cookie:: FortuneCookie () { messages << "Express yourself : Don't hold back" << "For success today look first at yourself" << "Whenever possible, keep it simple"; QPushButton "pb = new QPushButton("Open a fortune cookie"); QVBoxLayout *layout = new QvBoxLayout(); layout->addWidget (pb); this->setLayout (layout); connect (pb, SIGNAL (clicked()), this, SLOT (showFortune())); } void FortuneCookie::showFortune () { QString m = messages (rand() % 3]; MessageBox:: information (this, "Message", m); }
1.1 What does the code above do? (2) 1.2 Explain the purpose of signals and slots using the code above. (2) 1.3 Why is messages declared as a data member as opposed to the button and the layout that are declared in the constructor? (2) 1.4 Why is the Q_OBJECT macro required in this class definition? (2) 1.5 In the constructor, two objects are allocated on the heap but they are not de-allocated. Is that a problem in this class? Explain. (2) 1.6 Illustrate the parent-child object tree after an object of Fortunecookie is created. (2) 1.7 Assume that the above class needs to be changed so that the messages are displayed at regular intervals. In particular, the user should be able to: - specify the time interval on the graphical user interface - click on a start button for the program to read the time entered as well as to start displaying the messages at regular intervals Write down the modified code of Fortune Cookie (both definition and implementation) to achieve the above requirements. No pre-processor directives and no additional features are required in your answer. (8) 1.8 Study the MyMainWindow class definition below to answer the question that follows: class MyMainWindow: public QMainWindow Q_OBJECT public: MyMainWindow(); public slots: void showFortune Cookie (); }; Implement the constructor of MyMainWindow so that a menu named applications and a menu item named Fortune Cookie will be displayed on the main window and so that clicking on the menu item Fortune Cookie executes the slot showFortuneCookie () defined in this class. (5)
1.1 What does the code above do? (2) 1.2 Explain the purpose of signals and slots using the code above. (2) 1.3 Why is messages declared as a data member as opposed to the button and the layout that are declared in the constructor? (2) 1.4 Why is the Q_OBJECT macro required in this class definition? (2) 1.5 In the constructor, two objects are allocated on the heap but they are not de-allocated. Is that a problem in this class? Explain. (2) 1.6 Illustrate the parent-child object tree after an object of Fortunecookie is created. (2) 1.7 Assume that the above class needs to be changed so that the messages are displayed at regular intervals. In particular, the user should be able to: - specify the time interval on the graphical user interface - click on a start button for the program to read the time entered as well as to start displaying the messages at regular intervals Write down the modified code of Fortune Cookie (both definition and implementation) to achieve the above requirements. No pre-processor directives and no additional features are required in your answer. (8) 1.8 Study the MyMainWindow class definition below to answer the question that follows: class MyMainWindow: public QMainWindow Q_OBJECT public: MyMainWindow(); public slots: void showFortune Cookie (); }; Implement the constructor of MyMainWindow so that a menu named applications and a menu item named Fortune Cookie will be displayed on the main window and so that clicking on the menu item Fortune Cookie executes the slot showFortuneCookie () defined in this class. (5)