English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

/*The life cycle of an object*/
#include
#include
class Test
{
public:
Test()
{
cout<<"Constructor invoked"< }
~Test()
{
cout<<"Destructor invoked"< }
};
Test obj1;
int main()
{
cout<<"main() begins"< Test obj2;
{
cout<<"Inner block begins"< Test obj3;
cout<<"Inner block ends"< }
cout<<"main() ends"< getch();
return 0;
}

the output i get is

Constructor invoked
main() begins
Constructor invoked
Inner block begins
Constructor invoked
Inner block ends
Destructor invoked
main() ends

it should have been

Constructor invoked
main() begins
Constructor invoked
Inner block begins
Constructor invoked
Inner block ends
Destructor invoked
main() ends
Destructor invoked
Destructor invoked

plz help(destructor should automatically be initialized isn't it?)
i am using borland c++ builder 5.2 is it a factor??

2006-09-27 16:07:22 · 3 answers · asked by Sakar 1 in Computers & Internet Programming & Design

3 answers

The following answer is absolutly correct ....

Constructor invoked
main() begins
Constructor invoked
Inner block begins
Constructor invoked
Inner block ends
Destructor invoked
main() ends
________________
|Destructor invoked |
|Destructor invoked |
|________________|

But u can't see the last 2 statements (showed in block)....here what actully happened is ...

The destructore for obj2 will be called after the "MAIN() FUNCTION" is terminated" (i.e. after the closing braces).
B'coz, obj2 is declaired in main() function, so its life is in the main() function's scope.
The destructor for obj1 will be called just b4 the "PROGARMM" is terminated (i.e. bust b4 the programm is removed from the memory).
B'coz, obj1 is a globle object of TEST class and its life is thruout the programm.

I dont know about the Borland Compiler, but in Turbo compiler you can se the output screen again after termination of the program using "ALT + F5" and doing this will show the real output...

u can contact me if u still have doubts... send me message.

2006-09-27 17:40:50 · answer #1 · answered by Digitally Й!Й 3 · 2 0

Yeah, the last poster is right. I added this line at the top:

using namespace std;

and made other minor changes to get it going on unix.

I get the output you expected:

Constructor invoked
main() begins
Constructor invoked
Inner block begins
Constructor invoked
Inner block ends
Destructor invoked
main() ends
Destructor invoked
Destructor invoked


So, you might have an issue with text output. That doesn't explain why you get some lines printed out and not others, though. So, yeah, you might also have a flaky compiler, since the code works as expected with g++.

2006-10-05 14:28:30 · answer #2 · answered by arbeit 4 · 0 0

I have altered some of your code so it will compile AND run. Forgive me as I have cut parts out regardless, I found you had multple problems with the code, such as how you were actually giving the function protyptes in the class actual code (only pass them parameters if needed, you do all the code you want the function to do in the defintion. A class can ONLY have one deconstrutor. Also if you are compling this in one file...as your code showed...then why did you not use namespace std? That was probably half of your errors right there you would have had to use std::cout << "stuff on screen";

Good luck.
#include
using namespace std;

//Define Class
class Test
{
public:
//Function Prototype
void TestFunction();
//Destructor
~Test();
};


int main()
{
//Declare object/variable
Test obj1;

cout<<"main() begins"< //Function call
obj1.TestFunction();
cout<<"main() ends"<
system("pause");
return 0;
}
//Function Definitions
void Test::TestFunction()
{
cout<<"Inner block begins"< }
Test::~Test()
{
}

2006-09-27 16:46:30 · answer #3 · answered by D 4 · 0 1

fedest.com, questions and answers