By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,404 Members | 2,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,404 IT Pros & Developers. It's quick & easy.

Destructor

P: n/a
#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

Apr 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Kuku" <me******@yahoo.com> skrev i meddelandet
news:11*********************@e56g2000cwe.googlegro ups.com...
#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/
Apr 20 '06 #2

P: n/a
Kuku wrote:
#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.

Apr 20 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.