Destructor

K

Kuku

#include<iostream>

using namespace std;


class Mammal
{
private:
int age, weight;

public:
Mammal()
{
cout<<"I'm a mammal constructor 1\n";
}

~Mammal()
{
cout<<"I'm a mammal destructor\n";
}

Move()
{
cout<<"Mammal move 5 steps\n";
}

Move(int steps)
{
cout<<"Mammal move"<<steps<<"steps\n";
}

};

class Dog: public Mammal
{
private:
int x,y;

public:
Dog()
{
cout<<"I'm dog constructor 1\n";
}

~Dog()
{
cout<<"I'm dog destructor\n";
}

Move()
{
cout<<"Dog move 5 steps\n";
}
};

int main()
{
Mammal bigMammal;
Dog frisky;

bigMammal.Move();
bigMammal.Move(10);
frisky.Mammal::Move(3);

return 0;
}



In the above code when the 2 objects get destroyed(Mammal and Dog) the
Mammal destructor is called twice after the Dog dtor. I think it should
be called once, but why is the Mammal dtor called twice
 
P

Peter Jansson

Kuku said:
#include<iostream>

using namespace std;


class Mammal
{
private:
int age, weight;

public:
Mammal()
{
cout<<"I'm a mammal constructor 1\n";
}

~Mammal()
{
cout<<"I'm a mammal destructor\n";
}

Move()
{
cout<<"Mammal move 5 steps\n";
}

Move(int steps)
{
cout<<"Mammal move"<<steps<<"steps\n";
}

};

class Dog: public Mammal
{
private:
int x,y;

public:
Dog()
{
cout<<"I'm dog constructor 1\n";
}

~Dog()
{
cout<<"I'm dog destructor\n";
}

Move()
{
cout<<"Dog move 5 steps\n";
}
};

int main()
{
Mammal bigMammal;
Dog frisky;

bigMammal.Move();
bigMammal.Move(10);
frisky.Mammal::Move(3);

return 0;
}



In the above code when the 2 objects get destroyed(Mammal and Dog) the
Mammal destructor is called twice after the Dog dtor. I think it should
be called once, but why is the Mammal dtor called twice

Hi,

Yoou have one Dog which is destryed first sice it is created last. A Dog is
a Mammal so therefore Mammal's dtor is called when it is destroyed. Also,
you have bigMammal which is a Mammal´. It is also destryed. Thus, you have
two calls to the Mammal dtor.

Regards,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
 
R

Rolf Magnus

Kuku said:
#include<iostream>

using namespace std;


class Mammal
{
private:
int age, weight;

public:
Mammal()
{
cout<<"I'm a mammal constructor 1\n";
}

~Mammal()
{
cout<<"I'm a mammal destructor\n";
}

Move()
{
cout<<"Mammal move 5 steps\n";
}

Move(int steps)
{
cout<<"Mammal move"<<steps<<"steps\n";
}

};

class Dog: public Mammal
{
private:
int x,y;

public:
Dog()
{
cout<<"I'm dog constructor 1\n";
}

~Dog()
{
cout<<"I'm dog destructor\n";
}

Move()
{
cout<<"Dog move 5 steps\n";
}
};

int main()
{
Mammal bigMammal;
Dog frisky;

bigMammal.Move();
bigMammal.Move(10);
frisky.Mammal::Move(3);

return 0;
}



In the above code when the 2 objects get destroyed(Mammal and Dog) the
Mammal destructor is called twice after the Dog dtor. I think it should
be called once, but why is the Mammal dtor called twice

Because you have two mammals. Dog is derived from Mammal, so frisky is both
a Dog and a Mammal, so when it's destroyed, both destructors get called.
Since bigMammal must be destroyed too, Mammal's destructor is called again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top