The code below does not show the output when the destructor is called. It shows when the constructor is called, then it shows the age. Then it finishes. Am I doing something wrong here? Thanks
Code:
#include <iostream>
using namespace std;
enum BREED { YORKIE, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB}; //global enumeration
class Mammal //This is the base class
{
public:
// constructors
Mammal();
~Mammal();
//accessor methods
int GetAge()const {return itsAge;}
void SetAge(int age){itsAge = age;}
int GetWeight() const {return itsWeight;}
void SetWeight(int weight){itsWeight = weight;}
//Other methods
void Speak() const {cout << "Talking" << endl;}
void Sleep()const {cout << "Sleeping" << endl;}
protected:
int itsAge;
int itsWeight;
};
class Dog : public Mammal
{
public:
// Constructors...dog class must have its own constructor //and destructor.
Dog();
~Dog();
// Accessor methods
BREED GetBreed() const {return itsBreed;}
void SetBreed(BREED breed){itsBreed = breed;}
// Other methods
void WagTail(){cout << "Tail wagging..." << endl;}
void BegForFood(){cout << "Begging for food..." << endl;}
protected:
BREED itsBreed;
};
Mammal::Mammal()
{
itsAge=1;
itsWeight=5;
cout<<"Mammal constructor called" << endl;
}
Mammal::~Mammal()
{
cout<<"Mammal destructor called" << endl;
}
Dog::Dog()
{
itsBreed = YORKIE;
cout << "Dog constructor called" << endl;
}
Dog::~Dog()
{
cout<<"Dog destructor called" << endl;
}
int main()
{
Dog Benny;
Benny.Speak();
Benny.BegForFood();
Benny.SetAge(56);
cout << "Benny is " << Benny.GetAge() << " years old." << endl;
system("pause");
return 0;
}
Comment