class, constuctor and destructor

Hi, I have a question  about constructor and destructor within a class:

How I can use a destructor if my private member variables are int type and string type?

to use a desctructor, do my private member variables have to be pointers?

(this for a class project)
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hugh McCurdyCommented:
I'm not sure I understand the question.

You can use a destructor regardless of the private variables.

Practical uses of destructors include closing a file or printing the contents of the object.

If none of those address your concern, could you please restate your question?
pgmerLAAuthor Commented:
Here is a class example, I am wondering if I am using constructor and desctructor correctly.

class exClass
.//mumber function


int num;
string sentence:

{num =0;
sentence="              ";//initializing int and string variables

//???????????????????what do I put here?

Hugh McCurdyCommented:
You don't necessarily need an explicit destructor.  In fact, I'd say  it is rare that you'd actually use one.
You also don't need an explicit constructor.  However, explicit constructors aren't as rare.

However, it appears you need some assistance and direction.

I would normally put the constructor and destructor inside the class.  But that's my preference.

The constructor and destructor are functions, so they need ().

I think it will help you to at least see the destructor do something.  Try this (untested)

    cout << "I am the destructor for exClass()!" << endl;

Open in new window

Does this help?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Hugh McCurdyCommented:
The point is that I don't think you need a destructor for the class you have as an example.
Hugh McCurdyCommented:
If the professor has insisted you have a destructor, you could have it say "goodbye" or something.  This works pretty well if you only create one object.

If the professor suggested you consider a destructor, you could just leave it out.  But perhaps it's better to have one that doesn't do much so that he sees that you understand the syntax.
pgmerLAAuthor Commented:
My question is how do I deallocate rescources that was allocated to member variable num and sentence.
Hugh McCurdyCommented:
I just looked up constructor and destructor in the textbook I used for C++.  This book is by Alireza Ebrahimi, PhD.  On page 268 -- "A compiler makes its own default constructor and destructor if you do not make one."  

Just confirmation that you don't have to have either, if you don't have use for one or the other or both.
Hugh McCurdyCommented:
My question is how do I deallocate rescources that was allocated to member variable num and sentence.

The compiler takes care of it for you.
Hugh McCurdyCommented:
You might want to read the Constructor/Destructor writeup on this page
evilrixSenior Software Engineer (Avast)Commented:
>>  how do I deallocate rescources that was allocated to member variable num and sentence.
You only have to take care of cleaning up memory if it was allocated on the heap (using new). Since you are not allocating memory on the heap there is no memory to release (using delete). So, as hmccurdy has already said, currently your class requires no explicit constructor.

BTW: your comment that says "initializing int and string variables" is actually wrong. You are assigning to them. The difference is important. By the time you get to the first line in your constructor all member variables have all ready been initialised. To actually explicitly initialise a member you need to do so in the constructor initialisation list.
Hugh McCurdyCommented:
evilrix did a good job of explaining in an alternate fashion.  IMHO, multiple approaches are important to the teaching/learning process.

Do you have any remaining questions?
pgmerLAAuthor Commented:
my teacher docked 2 points out of 20 for not using a constructor and a destructor.
should I then write an empty destructor and an empty constructor?
evilrixSenior Software Engineer (Avast)Commented:
I guess the point of the exercise was to do something that utilised the d_tor and c_tor but in general you wouldn't implement them unless they are required; the compiler will synthersize default ones for you.
Hugh McCurdyCommented:
It might help to post the text of future homework assignments.

I don't know how to answer your question since I don't have the text of the assignment.

I can say, in regular production programs, we don't include constructors or destructors if we don't need them.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.