Hi everyone! i have a test next week, and i have a few questions, about the following code:
1. can you please explain me the order of the delete/ destructor here?
the output is- E DESTRUCTOR,B DESRUCTOR,C FUN F, D DESTRUCTOR C FUN F
from left to right.
i have no idea why this order occured (only written delete on pointers which acttualy do nothing according to what i was told)..
2. the ****** line in the code, what excatly that mean?
thanks a lot !
////////MAIN////////
Base *base1=new E();
Base *base2=new D();
B *b= new E();
base1->f();
base1->i();
base2->f();
base2->i();
b->f();
b->i();
D d;
d.c=new D();
d.C::c=new C(); ***********
C c=d;
cout<<d.j()<<endl;
cout<<c.j()<<endl;
cout<<b->j()<<endl;
D* d0=dynamic_cast<D*>(&c);
D* d1=dynamic_cast<D*>(b);
C* d2=dynamic_cast<D*>(base2);
C* d3=dynamic_cast<C*>(base2);
if(d0)
d0->f();
if(d1)
d1->f();
if(d2)
d2->f();
if(d3)
d2->f();
delete base1;
delete base2;
delete b;
/////////////CLASSES//////////////
class Base
{
public:
Base() {cout<<"Base: default constructor"<<endl;}
virtualvoid f() {cout<<"Base:f"<<endl;}
void i() {cout<<"Base: fun I"<<endl;}
};
class B: public Base
{
public:
virtual ~B() {cout<<"B: destructor "<<endl;}
void f() {cout<<"B: fun F"<<endl;}
virtualvoid h()=0;
virtualvoid i() {cout<<"B: fun I"<<endl;}
int j() {cout<<"B: fun J"<<endl;return 2;}
};
class C:public Base
{
public:
int iN;
C *c;
C() {iN=1;}
~C() {cout<<"C: fun F"<<endl;}
void f() {cout<<"C: fun F"<<endl;}
void h() {cout<<"C: fun I"<<endl;}
virtualvoid i() {cout<<"C: fun I"<<endl;}
int j() {cout<<"C: fun J"<<endl;return c->iN;}
};
class D: public C{
public:
int iN;
C *c;
D() {iN=2;}
~D() {cout<<"D: destructor "<<endl;}
void h() {cout<<"D: fun H"<<endl;}
int j() {cout<<"D: fun J"<<endl;return c->iN;}
};
class E:public B{
public:
int iN;
C *c;
E() {iN=3;}
~E() {cout<<"E:destructor "<<endl;}
void h() {cout<<"E: fun H"<<endl;}
int j() {cout<<"E: fun J"<<endl;return c->iN;}
void f() {cout<<"E: fun F"<<endl;}
};
1. can you please explain me the order of the delete/ destructor here?
How can we begin to see the order of the destructors since you haven't provided the complete program?
I suggest you run the program in your debugger, set a breakpoint early in main and single step through the program stepping into the functions. You should then be able to note the order of the constructor calls, destructor calls and the like.
When an object is destroyed, it runs its own destructor. Things happen in the following order:
1) The code in the body of the destructor is executed
2) The destructors for all of its data members are invoked, so they are properly destroyed.
3) The destructor of parent (i.e. base) clas is invokeds, so that the base class part of itself is properly destroyed.
Knowing that, you should be able to work out the order in which destructors are called in your program.
You might also want to look at the text you're outputting in the destructor for C ;)
EDIT: as for that line:
d.C::c=new C();
An object of type D has two pointers of type C* called among its data members - one as part of its own definition, and one that's inherited from its parent. Both of these pointers have been given the name c. d.c accesses the first of them, but how do we specify the inherited one?
We do that by specifying the parent class name. Within D itself, we would use:
C::c
so to access that as a member of an object called d, we use:
d.C::c
So, that line is dynamically allocating an object of type C, and assigning the address of that object to the pointer c that's part of the base class of d.
Mikey, thanks a lot!
i tried working with your elaborated answer, but i have mistakes.. this is what i did:
delete base1-> it points to E, so E dtor activated, and then B dtor (e derived from b).
delete vase 2-> it points to D so D dtor activated, and then C dtor.
delete b-> B dtor.