Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,329 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,104 people online right now. Registration is fast and FREE... Join Now!




Simple Question about

 
Reply to this topicStart new topic

Simple Question about, Destroying a created class

Renzokusen
post 13 Mar, 2008 - 08:35 AM
Post #1


New D.I.C Head

*
Joined: 11 Mar, 2008
Posts: 32

How would I go about destroying a declared class?.

my code.
CODE

#include <iostream>
using namespace std;
class Cat
{
      public:
             Cat(int intialAge); // Constructor
             ~Cat();              //deconstructor
             int GetAge() const;        //accessor function
             void SetAge(int age);      //accessor function
             void Meow() const;
      private:
              int itsAge;      //member variable
};

I declared
Cat boots(4);


How would I create a function or method to destroy class boots?

This post has been edited by Renzokusen: 13 Mar, 2008 - 08:37 AM
User is offlineProfile CardPM

Go to the top of the page

skaoth
post 13 Mar, 2008 - 10:02 AM
Post #2


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 337



Thanked 9 times

Dream Kudos: 100
My Contributions


In your example you don't need to explicityly
delete the objects you created

when you create an automatic object like you did
the object will take care of deleting itself
when the object goes out of scope

For example
CODE

int main(void)
{
    Cat c1

    {
    // This object will delete
    // itself after the '}' below c2
    // because the inner scope will end
        Cat c2;
    }

    // c1 will be deleted when the
    // program exits
}


The only time you need to be concerned with
deleting an object explictly is when
you allocate the memory yourself

CODE

int main(void)
{
    // Create a cat object
    Cat *pCat = new Cat(4);

    ...

   // You need to delete the object
   // explicitly or you will have a
   // memory leak
   if(pCat) delete pCat;
}


cheers
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 13 Mar, 2008 - 10:27 AM
Post #3


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,170



Thanked 33 times

Dream Kudos: 25
My Contributions


That being said, garbage collection in C++ is usually suspect smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 02:51AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month