By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Is this object destroyed?

P: n/a
In the code below, when the member function AddInput is called, a new
object aNum is created, of type InputNum. Actually, AddInput is called
twice, thus creating two separate aNum objects. I gather that after
AddInput is executed, each separate aNum object is destroyed? Is no
destructor required?

Cheers,

Deets
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

class InputNum
{
public:
InputNum (char msg [])
{
cout << msg;
cin >> _num;
}

int GetValue () const { return _num; }

void AddInput (char msg [])
{
InputNum aNum (msg);
_num = GetValue () + aNum.GetValue ();
cout << aNum.GetValue ();
}
private:
int _num;
};

char SumString[] = "The sum is ";

int main()
{
InputNum num ("Enter number ");
num.AddInput ("Another one ");
num.AddInput ("One more ");
cout << SumString << num.GetValue () << endl;
}
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Anon Email" <an********@fastmail.fm> wrote in message
news:83*************************@posting.google.co m...
In the code below, when the member function AddInput is called, a new
object aNum is created, of type InputNum. Actually, AddInput is called
twice, thus creating two separate aNum objects. I gather that after
AddInput is executed, each separate aNum object is destroyed? Is no
destructor required? Correct. It is created on the stack. On returning from the subroutine the
destructor for aNum is called. Since nothing special is done the default
constructor suffice

So nothing has to be done.

Regards, Ron AF Greve.

Cheers,

Deets
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

class InputNum
{
public:
InputNum (char msg [])
{
cout << msg;
cin >> _num;
}

int GetValue () const { return _num; }

void AddInput (char msg [])
{
InputNum aNum (msg);
_num = GetValue () + aNum.GetValue ();
cout << aNum.GetValue ();
}
private:
int _num;
};

char SumString[] = "The sum is ";

int main()
{
InputNum num ("Enter number ");
num.AddInput ("Another one ");
num.AddInput ("One more ");
cout << SumString << num.GetValue () << endl;
}

Jul 22 '05 #2

P: n/a
Thanks, Ron.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.