OOP- CLASSES

A few questions...:

Under what circumstances do I need a destructor?

What can I create from a class that would cause me to have to use a destructor (and why)?

When/how should I use Punlic, Private, and Procected declarations?

__________________________________________________________________________

I want to start getting into classes, and, needless to say, reading examples/descriptions online does not help me visualise it well...

I have woked with data structures very often, so I understand how the declaration of a class in a [foriegn] function carries its variables until the function ends, etc...

I get a feel of how I could make 'objects' with it... I just not sure exactly what I would use it for...

*************************************************************************

(After thinking upon the prospect... the OP gets an idea:)
Could I use a class to create an object which:

needs to be both a string, or an int (so mabey: myobj.int() +5 = 6?, or mabey if(myobj.string() == DEFINED_RETURN_VALUE){return "stuff"}?

That would make my life LOADS easier (as far as data handling is concerned)
Under what circumstances do I need a destructor?

The compiler provides one for you if you don't. If your class has any dynamic memory then you'll definitely need to provide one to make sure that all gets cleaned up. Other than that, it just depends on if you need something to happen when an object dies (decrement some counter maybe).

When/how should I use Punlic, Private, and Procected declarations?

These are pretty basic. You should know these if you know anything about classes. Public will be available for use from anywhere. Private can only be used from within the class, and protected can be used from within the class and also from its derived classes.

As for when to use classes, that's entirely up to you as the programmer. No one is forcing you to use classes (unless your employer is, in which case you are going to). They can be useful, but they are not the perfect solution to all problems.
If your class has any dynamic memory


Example?

Also:

If I make a class, how can I put it into a header so that I can easily use it in many areas of my code?
Last edited on
If your class contains a pointer you will need to deallocate any memory that has been allocated to it. Also note that you will also need a copy constructor in that situation.
May be you find answers onyour questions at http://www.cplusplus.com/doc/tutorial/? You'll get lots of examples there.
Topic archived. No new replies allowed.