• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

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)
0
pgmerLA
Asked:
pgmerLA
  • 9
  • 3
  • 2
1 Solution
 
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?
0
 
pgmerLAAuthor Commented:
Here is a class example, I am wondering if I am using constructor and desctructor correctly.

class exClass
{
  private:
.
.
.//mumber function

public:

int num;
string sentence:
};

//constructor
exClass::exClass
{num =0;
sentence="              ";//initializing int and string variables
}

//destructor
exClass::~exClass
{
//???????????????????what do I put here?
]

0
 
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)

exClass::~exClass()
{
    cout << "I am the destructor for exClass()!" << endl;
}

Open in new window


Does this help?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Hugh McCurdyCommented:
The point is that I don't think you need a destructor for the class you have as an example.
0
 
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.
0
 
pgmerLAAuthor Commented:
My question is how do I deallocate rescources that was allocated to member variable num and sentence.
0
 
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.
0
 
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.
0
 
Hugh McCurdyCommented:
You might want to read the Constructor/Destructor writeup on this page   http://www.cplusplus.com/doc/tutorial/classes/
0
 
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.
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6
0
 
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?
0
 
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?
0
 
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.
0
 
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 9
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now