473,396 Members | 1,864 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,396 software developers and data experts.

basic classes question

Suppose I have a class of objects which take an integer parameter.

I can easily create an object with the required parameter as follows:

name_of_class variable_name(integer_value);

For clarity, suppose I have a bank-account class and each object falls
into one of 5 distinct categories.

Then I might have code like:

bank_account object_1(5);

bank account object_2(3);
But if I have lots of objects of the class, I don't know what the
correct syntax is for doing such assignments (creating the objects) in
a loop.

Would the following work? (By using a giant leap of imagination, let
my_integer_valued_function denote an integer_valued function.)

object_array = new bank_account[n];

for (i=0; i<n; i++)

bank_account object_array[i] (my_integer_valued function of i);
Or perhaps it should be

object_array = new bank_account[n];

for (i=0; i<n; i++)

object_array[i] (my_integer_valued function of i);

Thanks for all your help and advice. I have received a lot of help
from this group.

Paul Epstein

Dec 9 '05 #1
2 1251

pa**********@att.net wrote:
Suppose I have a class of objects which take an integer parameter.

I can easily create an object with the required parameter as follows:

name_of_class variable_name(integer_value);

For clarity, suppose I have a bank-account class and each object falls
into one of 5 distinct categories.

Then I might have code like:

bank_account object_1(5);

bank account object_2(3);
But if I have lots of objects of the class, I don't know what the
correct syntax is for doing such assignments (creating the objects) in
a loop.

Would the following work? (By using a giant leap of imagination, let
my_integer_valued_function denote an integer_valued function.)

object_array = new bank_account[n];

for (i=0; i<n; i++)

bank_account object_array[i] (my_integer_valued function of i);

No

Or perhaps it should be

object_array = new bank_account[n];

for (i=0; i<n; i++)

object_array[i] (my_integer_valued function of i);


No.
When you use operator new[], all objects get constructed using the
no-arg constructor.
If you want to construct all objects with a different initial value,
use array of pointers, and allocate in a loop.

const int n = 100;
bank_account* object_array[n];

for(int i=0; i < n; i++)
{
object_array[i] = new bank_account(i); // initailze with i.
}

Also, prefer std::vector over arrays.

Hope this helps.

Dec 9 '05 #2

Neelesh Bodas wrote:
pa**********@att.net wrote:
Suppose I have a class of objects which take an integer parameter.

I can easily create an object with the required parameter as follows:

name_of_class variable_name(integer_value);

For clarity, suppose I have a bank-account class and each object falls
into one of 5 distinct categories.

Then I might have code like:

bank_account object_1(5);

bank account object_2(3);
But if I have lots of objects of the class, I don't know what the
correct syntax is for doing such assignments (creating the objects) in
a loop.

Would the following work? (By using a giant leap of imagination, let
my_integer_valued_function denote an integer_valued function.)

object_array = new bank_account[n];

for (i=0; i<n; i++)

bank_account object_array[i] (my_integer_valued function of i);


No

Or perhaps it should be

object_array = new bank_account[n];

for (i=0; i<n; i++)

object_array[i] (my_integer_valued function of i);


No.
When you use operator new[], all objects get constructed using the
no-arg constructor.
If you want to construct all objects with a different initial value,
use array of pointers, and allocate in a loop.

const int n = 100;
bank_account* object_array[n];

for(int i=0; i < n; i++)
{
object_array[i] = new bank_account(i); // initailze with i.
}

Also, prefer std::vector over arrays.

Thanks.

I find const int n = 100 to be a bit inflexible.

I can't get this to work in the case of variable n. Since arrays need
to have constant integer bounds, it seems I need a pointer to a
pointer.

I somehow can't get this to work.

Thanks for your continued help.

Paul Epstein

Dec 19 '05 #3

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

Similar topics

6
by: pauldepstein | last post by:
I am reading Grimshaw and Ortega's "C++ and Numerical Methods." They construct a vector class which contains the variable vec, a float* variable where the length of the array (number of...
4
by: Mark Fox | last post by:
Hello, I've noticed in some sample code that sometimes people use the @ before a string when concatenating them. Example: string filePath = Appl.Request.PhysicalApplicationPath + @"bin\" +...
1
by: Hans De Schrijver | last post by:
Another basic question. In VB6, when I needed an enum that was used by multiple classes and/or modules, I would put it in a module and set it as public enum, so the entire project would have...
5
by: Paul Bromley | last post by:
I have written a similar enquiry to this newsgroup, but had no responses - hence I will rephrase it with the hope that someone will answer. I am new to using Classes, but trying hard to get the...
2
by: Janus | last post by:
Hell This is a very basic question i know :O Formerly I developed in VisualBasic 6.0 and when a project is started you can select different project types. But what exactly is an ActiveX DLL? -...
6
by: pinorama123 | last post by:
I have an ASP.NET application that contains a few classes that I have built. One of my objects is a user object. I have a pretty basic question about how this would work. If I have multiple...
18
by: Ann Scharpf via AccessMonster.com | last post by:
I am not sure which would be the best place to post this question, so I'm posing it here with Access general questions. I have reached the point many times in Word and in Access where my ignorance...
21
by: Roland | last post by:
The following issue is puzzling me: There are 2 ways of writing the code below: .... Dim fnt as Font = New Font(...) DrawString(myText, fnt,...) fnt.dispose(). or DrawString(myText, New...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
14
by: MartinRinehart | last post by:
Working on parser for my language, I see that all classes (Token, Production, Statement, ...) have one thing in common. They all maintain start and stop positions in the source text. So it seems...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.