Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ array of my own object! Please help! ;)

Status
Not open for further replies.

cj92713696

Programmer
Nov 23, 2000
105
US
I really need some help

I've created an object called SavingsBank; It works
great! It has a constructor of SavingsBank(0, "")
where 0 is my initial balance and "" is the account
name.

I've created a second object called Array1; Array1
will instantiate objects of type SavingsBank and will
trap all errors if I try to go out of bounds of the
array element. It has a constructor of arraySize.

For instance ...

Array1 Jack(5);

Jack[0] through Jack[4] will be normal, but if I try
to access Jack[5] my Array1 class should trap that
error. I've overloaded my [] through class Array1.

The only area I've having problems w/is how to
instantiate an array of my own object. Obviously if I
wanted to have a 5 element array of type int, I could
simple do a ...

int *savingsAccountPtr;
savingsAccountPtr = new int[5];

... and that would work fine, but the only way I know
how to instantiate SavingsBank is as follows ...

SavingsBank *savingsAccountPtr;
savingsAccountPtr = new SavingsBank(0, "");

... If I try to add the brackets to SavingsBank it
gives me an error. I imagine I have to overload the
[] in SavingsBank, but I'm still in the same dilemma -
arrays of my own object. Can you help me out here?
:) I have also done this ...

SavingsBank *savingsAccountPtr;
savingsAccountPtr = new SavingsBank(0, "");

for (int intCounter = 0; intCounter < 5; intCounter++)
{
savingsAccountPtr++;
savingsAccountPtr = new SavingsBank(0, &quot;&quot;);
}

... the problem w/that though is that the memory
locations are not consecutive ... C++ will just find
an area of memory that is free and put a single
instance of my object.

Your help is much appreciated .........

CJ

 
Hi

The following code would create an array of objects that have a void constructor.

SavingsBank savingsBank[12];

You could then create a method called

initAccount(int bal, char* name)

Then you could call this method in a loop to init each account. e.g.

for(int i=0; i < 12; i++)
savingsAccount.initAccount(12, &quot;name&quot;);

Hope that is of some help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top