Help with delete operator and destructors (code attached)

K

kbubbar

Hello All,

I have a question I was hoping someone could answer. :)
From my readings (textbooks and web references), I have read that when
the delete operator is called followed by a pointer to an object
created on the heap, the destructor for this class is implicitly called
as a result.

I'm just wondering why this does not occur in the code I have posted.
(i.e. I expect to see the text inside the destructors when the delete
operator is called).

Any help is appreciated!

Kush

Code below:

#include <iostream.h>
#include <string.h>

//---------------------------------------------------------------------Class
declarations
class Car
{
public:
virtual void find_capacity(void) = 0;
~Car() {}
};

class BoxCar : public Car
{
private:
int used;
public:
BoxCar(void);
virtual void find_capacity(void);
~BoxCar() {cout << "BoxCar destructor called" << endl; }
};

class TankCar : public Car
{
private:
int used;
public:
TankCar(void);
virtual void find_capacity(void);
~TankCar() {cout << "TankCar destructor called" << endl;}
};

//---------------------------------------------------------------------BoxCar
BoxCar::BoxCar(void)
{
cout << "How much square footage is used? ";
cin >> used;
}

void BoxCar::find_capacity(void)
{
cout << "Still has " << (500 - used) << " square feet of capacity
left." << endl;
}

//---------------------------------------------------------------------TankCar
TankCar::TankCar(void)
{
cout << "How many gallons does it hold? ";
cin >> used;
}

void TankCar::find_capacity(void)
{
cout << "Still has " << (1000 - used) << " gallons of capacity left."
<< endl;
}

//---------------------------------------------------------------------main()
void main(void)
{
int i;
char Type[1];
Car *choochoo[4];

for(i = 0; i <= 3; ++i)
{
cout << "If box car, type 'b' else 't': ";
cin >> Type;

if(strcmp(Type, "b") == 0 )
choochoo = new BoxCar;
else
choochoo = new TankCar;
}

for(i = 0; i <= 3; ++i)
{
cout << "Car 1: ";
(*choochoo).find_capacity();
delete choochoo;
}
}
 
K

Kai-Uwe Bux

Hello All,

I have a question I was hoping someone could answer. :)

the delete operator is called followed by a pointer to an object
created on the heap, the destructor for this class is implicitly called
as a result.

I'm just wondering why this does not occur in the code I have posted.

It does. However, you are calling the wrong destructor since you use a Car*
and the destructor of Car is not declared virtual.
(i.e. I expect to see the text inside the destructors when the delete
operator is called).

Any help is appreciated!

Kush

Code below:

#include <iostream.h>
#include <string.h>

//---------------------------------------------------------------------Class
declarations
class Car
{
public:
virtual void find_capacity(void) = 0;
~Car() {}

Make that:

virtual ~Car() {}
};

class BoxCar : public Car
{
private:
int used;
public:
BoxCar(void);
virtual void find_capacity(void);
~BoxCar() {cout << "BoxCar destructor called" << endl; }
};

class TankCar : public Car
{
private:
int used;
public:
TankCar(void);
virtual void find_capacity(void);
~TankCar() {cout << "TankCar destructor called" << endl;}
};

//---------------------------------------------------------------------BoxCar
BoxCar::BoxCar(void)
{
cout << "How much square footage is used? ";
cin >> used;
}

void BoxCar::find_capacity(void)
{
cout << "Still has " << (500 - used) << " square feet of capacity
left." << endl;
}

//---------------------------------------------------------------------TankCar
TankCar::TankCar(void)
{
cout << "How many gallons does it hold? ";
cin >> used;
}

void TankCar::find_capacity(void)
{
cout << "Still has " << (1000 - used) << " gallons of capacity left."
<< endl;
}

//---------------------------------------------------------------------main()
void main(void)
{
int i;
char Type[1];
Car *choochoo[4];

for(i = 0; i <= 3; ++i)
{
cout << "If box car, type 'b' else 't': ";
cin >> Type;

if(strcmp(Type, "b") == 0 )
choochoo = new BoxCar;
else
choochoo = new TankCar;
}

for(i = 0; i <= 3; ++i)
{
cout << "Car 1: ";
(*choochoo).find_capacity();
delete choochoo;
}
}



Best

Kai-Uwe Bux
 
P

Pierre Barbier de Reuille

(e-mail address removed) a écrit :
Hello All,

I have a question I was hoping someone could answer. :)

the delete operator is called followed by a pointer to an object
created on the heap, the destructor for this class is implicitly called
as a result.

I'm just wondering why this does not occur in the code I have posted.
(i.e. I expect to see the text inside the destructors when the delete
operator is called).

Any help is appreciated!

Kush

Code below:

#include <iostream.h>
#include <string.h>

//---------------------------------------------------------------------Class
declarations
class Car
{
public:
virtual void find_capacity(void) = 0;
~Car() {}
};

This is very simply because your destructor is not virtual ... as soon
as you have polymorphic use of a class, you really *need* a virtual
destructor, otherwise only the destructor of the static type of your
variable will be called.

Pierre
 
D

Daniel T.

From my readings (textbooks and web references), I have read that when
the delete operator is called followed by a pointer to an object
created on the heap, the destructor for this class is implicitly called
as a result.

I'm just wondering why this does not occur in the code I have posted.
(i.e. I expect to see the text inside the destructors when the delete
operator is called).

Any help is appreciated!

The base class' destructor is called and it isn't virtual, therefore the
derived class' destructor isn't used.

Change ~Car() to:

virtual ~Car() { }
 
K

Kush

Ahhh.... Yes I forgot to make my base class destructor virtual!

Thanks for the help!!

Kush
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top