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

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




Help with Constructors and Destructors

 
Reply to this topicStart new topic

Help with Constructors and Destructors, Need some help understanding it

AntiBNI
17 Jul, 2008 - 05:16 PM
Post #1

D.I.C Head
**

Joined: 21 Jul, 2006
Posts: 59


My Contributions
Could some one show me an example or an explanation on how Constructors and Destructors works?

I have researched and cant quite under stand how it works and what is it good for?

Help will be greatly appreciated.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Help With Constructors And Destructors
17 Jul, 2008 - 05:48 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,197



Thanked: 212 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
constructors are simply a method (with the same name as the class) which is called when an object is created. This method can take parameters just like any other method or not take any at all. If it takes parameters, these parameters are usually used to set the object up into a known "state". That is, the object is setup with values.

Destructors (shown with a tilde and again the same name of the class) as you can imagine are the method that gets called when an object is destroyed. These do not take parameters and are used to "free up resources or other types of cleanup on the object prior to being deleted.

cpp

// Class declaration. Has one constructor which can have no parameters.
// It also has a destructor.
class example {
public:
example();
~example();
};

// Class implementation for these two functions.

example::example() {
cout << "You created an object!" << endl;
}

example::~example() {
cout << "You destroyed an object!" << endl;
}


Now when you go to create an object, it will automatically call the constructor method. When you destroy the object it will automatically call the destructor (if one is defined).

cpp

// This will print out "You created an object!"
example *ex = new example();

// This will then print out "You destroyed an object!"
delete(ex);


Some rules to remember with constructors...

1) They are the same name as the class
2) They don't have return types (not even void)
3) You don't have to write one yourself, but the compiler always puts one in and is known as the "default constructor". It is empty.
4) Related to point 3, if you do create a constructor that takes parameters, then you MUST provide a default empty constructor because then the compiler will not add it for you and you have to have one for a class.
5) Use constructors to setup member variables for the object only and try to keep it short as possible. Great things for constructors include initialization of variables, creation of inner objects, setting of pointers etc.

Things not good in constructors... variables of other classes, calls to long unneeded functions, anything that depends on some other class or environment being set.

You can find more explanation and examples at the link below...


Classes Tutorial (scroll down to constructors and destructors)

Enjoy!
User is online!Profile CardPM
+Quote Post

F!st!cuffs
RE: Help With Constructors And Destructors
17 Jul, 2008 - 06:05 PM
Post #3

D.I.C Head
**

Joined: 15 Jul, 2008
Posts: 55



Thanked: 2 times
My Contributions
constructors are used for overloading EX: if you have an object circle

cpp

class circle{

private double radius;// radius attribute for class circle
circle(){
}// this is your default constructor no parameters passed

circle(double r){
radius = r;
}// overloaded constructor with double type parameter which sets radius to value passed

public double getRadius() {
return radius;
}
public void setRadius( double r ){
radius = r;
}
}

int main(){

circle a;//creates new new circle object //no parameters means default constructor is used
circle b(2.0);// creates new circle object // parameter passed so it uses overloaded constructor

return 0;
}




basically it's useful for creating an object and setting that objects attributes without having to call all the set* functions

destructors on the other hand are useful for freeing up memory after you are finished with an object. They can also be overloaded the same way as a constructor but by adding '~' before the class name. EX: ~circle()

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Help With Constructors And Destructors
17 Jul, 2008 - 10:00 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,197



Thanked: 212 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
That is not entirely right. Destructors CAN NOT be overloaded. ONE destructor, that is it! While constructors can be overloaded, their primary purpose is to put the object into a known "state" and don't have to be overloaded at all.

But I want to iterate that destructors can not be overloaded.

smile.gif
User is online!Profile CardPM
+Quote Post

F!st!cuffs
RE: Help With Constructors And Destructors
17 Jul, 2008 - 11:08 PM
Post #5

D.I.C Head
**

Joined: 15 Jul, 2008
Posts: 55



Thanked: 2 times
My Contributions
QUOTE(Martyr2 @ 17 Jul, 2008 - 11:00 PM) *

That is not entirely right. Destructors CAN NOT be overloaded. ONE destructor, that is it! While constructors can be overloaded, their primary purpose is to put the object into a known "state" and don't have to be overloaded at all.

But I want to iterate that destructors can not be overloaded.

smile.gif


Yeah sorry about that. I should watch my wording. What I meant was that you can define the destructor the same way as the constructor but with the '~' in front of it. So here's my retraction smile.gif

User is offlineProfile CardPM
+Quote Post

AntiBNI
RE: Help With Constructors And Destructors
18 Jul, 2008 - 02:40 AM
Post #6

D.I.C Head
**

Joined: 21 Jul, 2006
Posts: 59


My Contributions
QUOTE(Martyr2 @ 17 Jul, 2008 - 06:48 PM) *

constructors are simply a method (with the same name as the class) which is called when an object is created. This method can take parameters just like any other method or not take any at all. If it takes parameters, these parameters are usually used to set the object up into a known "state". That is, the object is setup with values.

Destructors (shown with a tilde and again the same name of the class) as you can imagine are the method that gets called when an object is destroyed. These do not take parameters and are used to "free up resources or other types of cleanup on the object prior to being deleted.

cpp

// Class declaration. Has one constructor which can have no parameters.
// It also has a destructor.
class example {
public:
example();
~example();
};

// Class implementation for these two functions.

example::example() {
cout << "You created an object!" << endl;
}

example::~example() {
cout << "You destroyed an object!" << endl;
}


Now when you go to create an object, it will automatically call the constructor method. When you destroy the object it will automatically call the destructor (if one is defined).

cpp

// This will print out "You created an object!"
example *ex = new example();

// This will then print out "You destroyed an object!"
delete(ex);


Some rules to remember with constructors...

1) They are the same name as the class
2) They don't have return types (not even void)
3) You don't have to write one yourself, but the compiler always puts one in and is known as the "default constructor". It is empty.
4) Related to point 3, if you do create a constructor that takes parameters, then you MUST provide a default empty constructor because then the compiler will not add it for you and you have to have one for a class.
5) Use constructors to setup member variables for the object only and try to keep it short as possible. Great things for constructors include initialization of variables, creation of inner objects, setting of pointers etc.

Things not good in constructors... variables of other classes, calls to long unneeded functions, anything that depends on some other class or environment being set.

You can find more explanation and examples at the link below...


Classes Tutorial (scroll down to constructors and destructors)

Enjoy!


Wow dude,Thanks.

Really,You just enlighten me with that simple example.

Thanks a lot!

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 05:15PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month