473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Is this object destroyed?

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
2 1255

"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
Thanks, Ron.
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Martin | last post by:
Bit of a newbie question here (I'm familiar with OO in general, just not in C++ yet). I've seen code example where the general syntax for creating a new object is as follows: Thing a = new...
2
by: CoolPint | last post by:
Standard exception classes in C++ have what() which returns const char * and they have constructors accepting string. Where is that string created and when is the string destroyed? In the...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
4
by: Zork | last post by:
Hi, I am trying to stop object creation (in this case ill use a ball as the object) via use of exceptions. In essence, if the ball does not have an owner, I do not want the ball object created....
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
11
by: Xiaoshen Li | last post by:
Dear Sir, I am a little puzzled about a function returning a class object, for example, suppose I hava a class Money and a method: Money lastYear(Money aMoney) { Money tempMoney; ......
2
by: Jeff | last post by:
Hello, I assigned a new object to a local variable ("req") in a function (see below). The local variable "req" is obviously destroyed when the function exits, but should the object referenced by...
4
by: gg9h0st | last post by:
i worte a simple code below. ------------------------------------------------------------------------------------ #include "stdafx.h" class Object { public: int a;
10
by: Jess | last post by:
Hello, If I create a temporary object using a dynamically created object's pointer, then when the temporary object is destroyed, will the dynamically created object be destroyed too? My guess...
13
by: Mortimer | last post by:
Hi, i'm using PHP 5.1 I have two objects and the second one is using an instance of the first. As displayed in the example below, the Garbage Collector calls the destruct method of the first...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.