Help me plizz

What this code is gona do a show after compile.
Class ex{
int x;
Public:
Ex(int x) {this->x=x;
Cout<<"C:"<<x<<endl;}
~Ex(){Cout<<"D:"<<x<<endl;}
};
Ex* y2;
Ex y1(1);
Ex* y3;
Void func(){
y2 = new Ex(2);
y3 = new Ex(3);
Ex* y5(5);
}
void main(){
Ex y4(4);
Func();
Delete y3;
}
What this code is gone show !!!! Thx
Last edited on
Why don't you compile it and see? This sounds rather like a homework problem to me...

Also, bear in mind that there are some signs of bad programming practice in that code.

For example, "void main()" is no longer used. We use "int main()", and as such you need a return statement at the end of your main function. For example, "return 0" returns the integer value 0, which is the traditional way of saying to the OS that your program ended successfully (note that the OS doesn't really use this information anymore, but we still return 0 for success out of convention).

Also this code does some pretty dangerous stuff with pointers, but I'm not sure. That might be part of the point of the exercise - to work out which variables are constructed and/or destructed correctly.

If you do want to work this out without a compiler, just think about when constructors are called for the variables, and then about when they are destroyed. Remember that dynamically allocated memory stays there until you deallocate it or the program ends. However, automatic stack variables (i.e. the default, non pointer variables) are destroyed when the block in which they were created ends. If they are in global scope they are destroyed when the program quits.

Hope this helps,
Xander

PS: To tidy up code posted on here, put it inside code tags:
[code]int main()[/code] -> int main()
Topic archived. No new replies allowed.