A
Anon Email
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;
}
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;
}