Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 12th, 2005, 9:06 AM   #1
Cipher
Hobbyist Programmer
 
Cipher's Avatar
 
Join Date: Feb 2005
Location: /home/cipher
Posts: 123
Rep Power: 4 Cipher is on a distinguished road
Send a message via AIM to Cipher Send a message via MSN to Cipher
Constructor/destructor

I am having problems with constructors and destructors. Could someone please explain them? Like the example. Say I have a class called Cat. How do I declare a new constructor
__________________
And there was much rejoicing... Yay....
Cipher is offline   Reply With Quote
Old Mar 12th, 2005, 10:04 AM   #2
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
Cat();
and
~Cat();
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Mar 12th, 2005, 1:59 PM   #3
Cipher
Hobbyist Programmer
 
Cipher's Avatar
 
Join Date: Feb 2005
Location: /home/cipher
Posts: 123
Rep Power: 4 Cipher is on a distinguished road
Send a message via AIM to Cipher Send a message via MSN to Cipher
... Wtf is that?
__________________
And there was much rejoicing... Yay....
Cipher is offline   Reply With Quote
Old Mar 12th, 2005, 3:04 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Like this:
class Cat {
     char name[32];
     short age;
     char owner[32];
     
     Cat() {
         // constructor
         name[0] = '\0'; // this is an quick way to clear a string
         age = 0;
         owner[0] = '\0';
     }
 
     Cat(char *cat_name, int cat_age, char *cat_owner) {
         // this is another constructor - you can have more than one
         // this one lets the user specify the info in the declaration
         strcpy(name, cat_name);
         age = cat_age;
         strcpy(owner, cat_owner);
     }
 
     ~Cat() {
         // destructor - clean up in here
     }
 }
 
 int main() {
     Cat my_cat;
     strcpy(my_cat.name, "Fluffy");
     my_cat.age = 3;
     strcpy(my_cat.owner, "Me");
     
     Cat your_cat("Tibbles", 4, "You");
 }
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 12th, 2005, 3:37 PM   #5
Xero
Hobbyist Programmer
 
Join Date: Dec 2004
Location: a cardboard box
Posts: 118
Rep Power: 4 Xero is on a distinguished road
Quote:
Originally Posted by Cipher
I am having problems with constructors and destructors. Could someone please explain them? Like the example. Say I have a class called Cat. How do I declare a new constructor
Constructors and destructors are class functions that cannot be called upon, but are automatically called upon initialization of a variable of that class. You would use constructors if you wanted to initialize all the values in the class to zero or whatever. So basically, when ever a new class object was created there would be a default value for all the variables. Destructors do the opposite and activate upon the end of a class. Usually, you would use these to clear the memory from dynamic variables and linked lists using delete.
__________________
...
Xero is offline   Reply With Quote
Old Mar 12th, 2005, 4:07 PM   #6
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
Quote:
Originally Posted by Cipher
... Wtf is that?
that is your constructor and destructor.
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Mar 14th, 2005, 12:27 PM   #7
brkstf
Programmer
 
brkstf's Avatar
 
Join Date: Feb 2005
Posts: 89
Rep Power: 4 brkstf is on a distinguished road
the best way to get it is to make these silly output notices for yourself.

cat(int number)
{
cout<<"you just made cat number"<<number<<endl;
}

~cat()
{
cout<<"you just killed cat number"<<number<<endl;
}

i ALWAYS do this, until i'm absolutely positive that my memory crap is working
brkstf is offline   Reply With Quote
Old Mar 14th, 2005, 3:27 PM   #8
Cipher
Hobbyist Programmer
 
Cipher's Avatar
 
Join Date: Feb 2005
Location: /home/cipher
Posts: 123
Rep Power: 4 Cipher is on a distinguished road
Send a message via AIM to Cipher Send a message via MSN to Cipher
I think I understand...ARRGHHH! C++ is so confusing.
__________________
And there was much rejoicing... Yay....
Cipher is offline   Reply With Quote
Old Mar 14th, 2005, 5:03 PM   #9
Mad_guy
Hobbyist Programmer
 
Mad_guy's Avatar
 
Join Date: Oct 2004
Location: Sandstorm, Techno Club
Posts: 239
Rep Power: 4 Mad_guy is on a distinguished road
Send a message via AIM to Mad_guy Send a message via MSN to Mad_guy
Constructors basically let you interface with the class the moment you declare an object for the class.

Destructors basically clean up the shit that's left over from the other member functions of the class. So if one member function Allocates a bit of memory, the destructor can delete the memory.

It seems hard to grasp at first but it can save you quite a bit of code.

I should also note, Constructors and Destructors never have return values. Constructors just let you interface with member variables and other things involving the class. Destructors just get rid of the shit that's left to pick up.
Mad_guy is offline   Reply With Quote
Old Mar 14th, 2005, 5:54 PM   #10
Cipher
Hobbyist Programmer
 
Cipher's Avatar
 
Join Date: Feb 2005
Location: /home/cipher
Posts: 123
Rep Power: 4 Cipher is on a distinguished road
Send a message via AIM to Cipher Send a message via MSN to Cipher
kk...thanks to all that helped. I am pretty sure I got it. Thanks.
__________________
And there was much rejoicing... Yay....
Cipher is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:09 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC