Ask Question

Given the class definition: class CreateDestroy { public: CreateDestroy () { cout << "constructor called, "; } ~CreateDestroy () { cout << "destructor called, "; } }; What will the following program output? int main () { for (int i = 1; i < = 2; + +i) CreateDestroy cd; return 0; }

A. constructor called, destructor called, constructor called, destructor called,

B. constructor called, constructor called,

C. constructor called, constructor called, destructor called, destructor called,

D. Nothing

+4
Answers (1)
  1. 23 November, 02:50
    0
    (A) constructor called, destructor called, constructor called, destructor called.

    Explanation:

    In the class definition we have created a constructor which prints constructor called and a destructor which prints destructor called. Since the class contains default constructors and destructors but when we define constructor and destructor in the class defaults are removed from the class. In the code the loop is running 2 times creating a object so constructor is called when that iteration finishes it deletes that object so destructor is called and it is happening 2 times.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Given the class definition: class CreateDestroy { public: CreateDestroy () { cout ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers