473,809 Members | 2,695 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(i nteger_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_valu ed_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_val ued 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_val ued 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 1263

pa**********@at t.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(i nteger_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_valu ed_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_val ued 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_val ued 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**********@at t.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(i nteger_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_valu ed_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_val ued 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_val ued 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
2485
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 components in the vector) is given by the variable name veclength. That is what I _do_ understand. What I don't understand is the coding for the default constructor which includes vec=0; What does it mean for a pointer to be equal to 0? Presumably...
4
1782
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\" + asmname + ".dll"; Why is this done? What is the difference between using
1
1010
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 access to the enum and I would only have to maintain it in one place. In a C#.NET assembly, where would I put an enum that needs to be accessible by multiple classes and what access modifiers would I need?
5
1185
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 basics at the present time, and seem to be winning. Presently I am developing a Usercontrol for use in my application. To keep it simple, let me give as an example - I have 3 command buttons in the User Control. Each of these will have separate...
2
2114
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? - Is it considered as a COM object I've heard something about its not a real dll because of the iUnknown, but what's that Im sorry for my lack of knowledge, but if someone could give me a quick primer I would be very happ
6
1314
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 users logging into my application and using the user object do they all share the same object or does ASP.NET know that each user's object belongs to them? I'm seeing some strange behavior that leads me to believe when one user dimensions an instance...
18
2404
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 of VBA is a real detriment to me. I saw some posts about VBA classes with a particular vendor and the poster was advised not to take the class because of the likelihood of an unskilled trainer. My question is, would taking a Visual Basic...
21
1512
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 Font(...),...)
7
4477
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 to be checking that the correct data type is used DataAcess sets an abstract class and methods
14
1854
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 logical to have them all inherit from a base class that defines those, but this doesn't work: import tok class code: def __init__( self, start, stop ):
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10639
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10120
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.