The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 09-19-2007, 12:53 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Icon8 How to start C++ programming?? Newbie Question!!!

I'm newbie to C++ once I'm getting into Windows programming, I was really confused and really had to learn a lot more!! Though, I can get the basics pretty fast, though I probably still need to have a lot to learn Pointers as it is part of the most important thing in C++ too!!! Though, C++ is really weird sometimes, I just see tons of unknown variables when I get into Windows programming, DirectX and/or OpenGL do you guys really memorize them? Do you always have template..?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 09-19-2007, 02:26 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 616
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Well if you look at pointers on Pointers you will see all the information you need, from that page alone i think i made 6,7,8+ projects so i could see what they did, because i did so many projects its helped me to memorize it all near enough, that's probably my biggest tip for learning C++, make many projects for each bit and when you get too the later tutorials on things like pointers mix in your if statements and other things you have learnt.

Hope that helped a bit.
Lee.
Reply With Quote
  #3 (permalink)  
Old 09-20-2007, 01:02 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
I'm now moving toward Constructor and Destructor.. I'm still not sure how are they playing an important role in C++. I just know that Destructor is just something to close memory or cleaning up garbage.. kind of...?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 09-21-2007, 06:25 AM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 308
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
Yup, constructor's are the definition on how to create a new object.

Dim Chet as New Person()
I used the New Keyword so it "Instantiated" a new Object of Type Person as Variable name Chet.


Destructors are yup, clean memory, remove itself from memory so Garbage Collector can come sweep it up

Whoops, i shoulid probably look what forum I'm in before I answer in a different programming langauge, hehe

Last edited by Lee : 09-21-2007 at 10:40 AM. Reason: Please edit your last post!!! :)
Reply With Quote
The Following User Says Thank You to ccoonen For This Useful Post:
HelloWorld (09-23-2007)
  #5 (permalink)  
Old 09-23-2007, 04:22 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
what happen if we don't use destructor..?
it seems that the program that the command line program that I made never had destructor (since I'm not even sure on how to destruct..?) so I don't see any difference whether I use destructor or not lol...

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #6 (permalink)  
Old 09-27-2007, 02:39 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 112
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
If you don't need a destructor, don't use one. If you're not sure whether or not you need one right now, then you probably don't need one.

(If you're not using the 'new' or 'delete' keywords anywhere, then thats usually a sign that you don't need a destructor).

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #7 (permalink)  
Old 09-27-2007, 10:14 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
when can I know if I need one though..? Because really soon that I'll be dealing with multi threading programming with C++ sigh... haha, but I'm so excited because we actually going to use C++. I personally still prefer my school to use C++ (we did before..)

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #8 (permalink)  
Old 09-28-2007, 11:16 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 112
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Quote:
Originally Posted by HelloWorld View Post
when can I know if I need one though..? Because really soon that I'll be dealing with multi threading programming with C++ sigh... haha, but I'm so excited because we actually going to use C++. I personally still prefer my school to use C++ (we did before..)
Usually when you have a class that is responsible for handling dynamically created memory with new or new[]. Destructors enable an idiom known as "RAII" (stands for "Resource Acquisition Is Initialisation"... don't ask why its called that, its a terribly bad, misleading name) - Which is essentially a way to prevent classes from causing memory leaks.

For example, imagine a linked list class. The linked list class will be solely responsible for creating links. When the linked list object dies, you want it to automatically destroy all the links, rather than requiring the user to do that for you. The destructor will (hopefully) have a call that cleans up all of the new'ed memory, and prevent that class from causing memory leaks in the program.

Incidentally, one can never, and should never, call a destructor. A destructor is either invoked by a call to delete, delete[] on a dynamically created object (with new or new[])
or the destructor is invoked implicitly when an object goes "out of scope" (Or, for static objects, when the program ends..). You certainly should never have code which looks like my_object.~my_class(); (I believe that's a compiler error, but just in case its not, don't do it!).
For most classes, the default, empty destructor is enough. A destructor cannot be overloaded, nor can it take any arguments. Sometimes, when creating polymorphic base classes, you may wish to create an empty virtual destructor, to ensure that objects used polymorphically can be properly cleaned up



That's about all you need to do. A side note though, (You can stop reading here if you like..). There is a rule known as "the rule of 3" - whereby, if your class needs a destructor, then it probably needs a Copy Constructor and an overloaded Assignment Operator ( = ) too. The reason for this, is that dynamic memory "belonging" to the class does not get copied automatically when an instance of that class is copied.

What would happen if you had no copy constructor/assignment operator, when the object is copied, is that both objects would "point" to the same dynamically created objects.

Sometimes this behaviour is desirable, and is known as a "shallow" copy. More often though, a copy constructor/assignment operator will be used to duplicate all the data along with the copied object - That operation performs a "deep" copy.



In general, its best to leave dynamic memory alone until its really necessary. There's much that you can do with the STL which will allow you to design classes without worrying about new/delete.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Bench : 09-28-2007 at 11:33 AM.
Reply With Quote
Reply


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 03:44 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50