Article View: alt.comp.lang.learn.c-c++
Article #57Re: class help
From: John Harrison
Date: Tue, 01 May 2001 20:34
Date: Tue, 01 May 2001 20:34
61 lines
3253 bytes
3253 bytes
"Antony Carr" <antony.carr@btinternet.com> wrote in message
news:9cms6r$emm$1@neptunium.btinternet.com...
> Right heres a question for you all, i am a little stuck and hope someone
can
> help me...
> The best way of describing my problem is by showing you an example of what
i
> am trying to do, so here it is...
> I have a two files, a .cpp file containing the main() etc, and a header
file
> (bank.hpp).
> inside the header file there are two classes BankAccount and BankDB.
> BankAccount contains the account name, & balance. BankDB amongst other
> things creats for each instance of BankAccount the name in which the
account
> is held. Now heres the problem, i can for a single instance of BankAccount
> set the name & Balance indervidually , however how would you go about
> calling from the class of BankDB an instance of BankAccount ??? Whenever i
> try i get error messagess generated. I have attatched to this message the
> two files concerned. If any one could help i would be eternally greatfull
> :-)
>
> Antony Carr
> antony.carr@btinternet.com
>
You need something like this
class BankAccount
{
// whatever
};
class BankDB
{
public:
// whatever
private:
int num_of_accounts;
BankAccount the_accounts[100];
};
You see? The BankDB object holds (upto) 100 BankAccount objects. This is how
BankDb 'calls' an instance of BankAccount. E.g.
void BankDB::credit_by_name(const char *a_name, int an_amount)
{
int n = lookup_name(a_name);
the_accounts[n].credit(an_amount);
}
lookup_name is some function to work out which account we are interested in
from the name. Then you use the_account[n].credit to credit the actual
account.
This approach has the limitation that you can only have a maximum of 100
account in BankDB, there are various ways around that but at your level of
skill I'd stick with that for now.
john
Technical Details
Message-ID:
<S2FH6.13187$Kt2.1288005@news6-win.server.ntlworld.com>Path:
arc1-ca.pugleaf.net!newsfeed.google.com!newsfeed.stanford.edu!news.tele.dk!212.74.64.35!colt.net!diablo.netcom.net.uk!netcom.net.uk!nntp.news.xara.net!xara.net!gxn.net!news5-gui.server.ntli.net!ntli.net!news6-win.server.ntlworld.com.POSTED!not-for-mailReferences:
<9cms6r$emm$1@neptunium.btinternet.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d4ba,bcfa6c5c8a6717c0 X-Google-Attributes: gid10d4ba,public X-Google-Thread: 114592,bcfa6c5c8a6717c0 X-Google-Attributes: gid114592,public X-Google-Thread: 109fba,bcfa6c5c8a6717c0 X-Google-Attributes: gid109fba,public X-Google-ArrivalTime: 2001-05-01 13:37:07 PST Path: arc1-ca.pugleaf.net!newsfeed.google.com!newsfeed.stanford.edu!news.tele.dk!212.74.64.35!colt.net!diablo.netcom.net.uk!netcom.net.uk!nntp.news.xara.net!xara.net!gxn.net!news5-gui.server.ntli.net!ntli.net!news6-win.server.ntlworld.com.POSTED!not-for-mail Reply-To: "John Harrison" <jahhaj@bigfoot.com> From: "John Harrison" <jahhaj@bigfoot.com> Newsgroups: alt.comp.lang.learn.c-c++,att.lang.c++,comp.lang.c++ References: <9cms6r$emm$1@neptunium.btinternet.com> Subject: Re: class help Lines: 64 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 Message-ID: <S2FH6.13187$Kt2.1288005@news6-win.server.ntlworld.com> Date: Tue, 1 May 2001 21:34:48 +0100 NNTP-Posting-Host: 213.105.21.40 X-Complaints-To: abuse@ntlworld.com X-Trace: news6-win.server.ntlworld.com 988749106 213.105.21.40 (Tue, 01 May 2001 21:31:46 BST) NNTP-Posting-Date: Tue, 01 May 2001 21:31:46 BST Organization: ntlworld News Service