473,398 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Help with delete operator and destructors (code attached)

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[i] = new BoxCar;
else
choochoo[i] = new TankCar;
}

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

Oct 28 '06 #1
4 1448
kb*****@hotmail.com a écrit :
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() {}
};
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

Oct 28 '06 #2
kb*****@hotmail.com wrote:
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() { }

--
To send me email, put "sheltie" in the subject.
Oct 28 '06 #3
kb*****@hotmail.com wrote:
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.
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[i] = new BoxCar;
else
choochoo[i] = new TankCar;
}

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

Best

Kai-Uwe Bux
Oct 28 '06 #4
Ahhh.... Yes I forgot to make my base class destructor virtual!

Thanks for the help!!

Kush

Oct 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object...
2
by: skscpp | last post by:
I have a question about multiple inheritance, operator new/delete and ambiguity. Here is some code that I wrote to analyze this problem. // test.h ========================== #include <new>...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
2
by: Pierre Phaneuf | last post by:
At my workplace, we are in the process of migrating to a component system similar to COM (but not COM itself, as we have some space and portability requirements) that uses refcounting for resource...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
10
by: junw2000 | last post by:
Hi, Below is a small code about memory allocation and deallocation. #include <cstdlib> #include <iostream> using namespace std; class X { public: void* operator new(size_t sz) throw (const...
6
by: Pablo | last post by:
Hello, I am writing a windows application using C++ and BorlandBuilder 6 compiler. It is an event driven program and I need to create objects of some classes written by me. One of the classes...
12
by: yufufi | last post by:
Hello, How does delete know how much memory to deallocate from the given pointer? AFAIK this informations is put there by new. new puts the size of the allocated memory before the just before...
29
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I remembered delete is implemented through operator overloading, but I am not quite clear. Could anyone recommend some links about how delete is implemented so that I can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.