Ask Question
8 April, 12:47

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. 8 April, 14:34
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers