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

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




constructors and destructors

 
Reply to this topicStart new topic

constructors and destructors, explanation required

red_4900
post 4 Aug, 2008 - 09:27 AM
Post #1


Code Dreamers

****
Joined: 22 Feb, 2008
Posts: 791



Thanked 10 times
My Contributions


I know the 'what' and 'how' constructors and destructors, but I don't really understand why it is needed. explanation from the book said it is used to initialize an object when it is created. and this is one of the sample program :
cpp
class MyClass{
public:
int x;
MyClass();
~MyClass();
}
MyClass::MyClass(){
x=10;
}
~MyClass::MyClass(){
}


as opposed of initializing x using this way, how about doing it this way :
cpp
class MyClass{
public:
int x=10;
}

what is the real purpose of using constructors and destructors actually?

any explanation would be greatly appreciated smile.gif
User is online!Profile CardPM

Go to the top of the page

KYA
post 4 Aug, 2008 - 09:33 AM
Post #2


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,175



Thanked 48 times

Dream Kudos: 1150
My Contributions


Constructor: used to initialize object variables (if wanted), "creates" the object.

Destructor: cleans up after an object, frees memory (if you had a pointer in that object for example)

You normally wouldn't want data members to be public, you only want your class to access them. You can overload constructors to make various parameters to initialize your private data variables
User is offlineProfile CardPM

Go to the top of the page

Bench
post 4 Aug, 2008 - 09:43 AM
Post #3


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 599



Thanked 8 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


The second method is illegal in C++ so doing it that way isn't an option

You could think of a constructor as a function which is always run immediately as an object is created - sometimes its useful to be able to perform certain operations when an object is made, in order for the object to be in a particular 'state'

For example, you might build your own container class, which has a flag to indicate whether or not the container has any data in it. With a default constructor, the container object would probably be empty, so you would set the 'empty' flag reflect this.
- On the other hand, you might have another constructor which copies the contents of some or all of an existing container - so the flag would need to be set to show that the container was holding some data instead.


As for destructors - these are functions which are run immediately as an object is destroyed. Typically, you'll not need to do anything as an object is destroyed, though a common use for them is to automatically clean up any dynamically allocated memory which the object is responsible for, in order to prevent that object from "leaking" when it dies.
Another example of when a destructor is used, could be when the object is handling a file - You might add a line of code into the destructor to make sure that the file is closed when the object dies.

This post has been edited by Bench: 4 Aug, 2008 - 09:46 AM
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 10:32AM

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