K
kbubbar
Hello All,
I have a question I was hoping someone could answer.
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;
}
}
I have a question I was hoping someone could answer.
the delete operator is called followed by a pointer to an objectFrom my readings (textbooks and web references), I have read that when
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;
}
}