I wanted to create a little program to demonstrate constructors and destructors to some students. I am using Dev C++ v.4.9.9.2
The code looked like this:
include <iostream>
using namespace std;
class Truck
{
public:
Truck();
~Truck();
};
Truck::Truck()
{
cout<<"The Truck constructor has run"<<endl;
}
Truck::~Truck()
{
cout<<"The Truck destructor has run"<<endl;
}
int main()
{
Truck TwoTon;
cout<<"This program demonstrates an object with \n";
cout<<"a constructor and a destructor"<<endl;
system("pause");
return 0;
}
The code is pretty straight forward, and it does compile; however, the destructor message never appears. The first two messages do appear in the appropriate order. I was wondering if anyone else had experienced this problem. The point was to demonstrate the order in which the methods/output statements execute.
Thanks in advance,
Leslie
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
int main()
{
{
Truck TwoTon;
cout<<"This program demonstrates an object with \n";
cout<<"a constructor and a destructor"<<endl;
}
system("pause");
return 0;
}
TwoTon's destructor should be called when it leaves it's scope,
before the destructor wasn't called until after the system("pause") command.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The first two messages do appear in the appropriate order. I was wondering if
anyone else had experienced this problem. The point was to demonstrate the
order in which the methods/output statements execute.
I wanted to create a little program to demonstrate constructors and destructors to some students. I am using Dev C++ v.4.9.9.2
The code looked like this:
include <iostream>
using namespace std;
class Truck
{
public:
Truck();
~Truck();
};
Truck::Truck()
{
cout<<"The Truck constructor has run"<<endl;
}
Truck::~Truck()
{
cout<<"The Truck destructor has run"<<endl;
}
int main()
{
Truck TwoTon;
cout<<"This program demonstrates an object with \n";
cout<<"a constructor and a destructor"<<endl;
system("pause");
return 0;
}
The code is pretty straight forward, and it does compile; however, the destructor message never appears. The first two messages do appear in the appropriate order. I was wondering if anyone else had experienced this problem. The point was to demonstrate the order in which the methods/output statements execute.
Thanks in advance,
Leslie
Ok, great, thanks. I was wondering if it was actually called before or after the system ("pause");
I tried putting the code in its own code block, as suggested, with the system("pause"); on the outside and it worked like a charm.
Thanks so much!
Old post I know,
but try this...
int main()
{
{
Truck TwoTon;
cout<<"This program demonstrates an object with \n";
cout<<"a constructor and a destructor"<<endl;
}
system("pause");
return 0;
}
TwoTon's destructor should be called when it leaves it's scope,
before the destructor wasn't called until after the system("pause") command.
hi,
The first two messages do appear in the appropriate order. I was wondering if
anyone else had experienced this problem. The point was to demonstrate the
order in which the methods/output statements execute.
thanks in advice
regards,
phe9oxis,
http://www.guidebuddha.com