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
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