🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.comp.lang.learn.c-c++
7 messages
7 total messages Started by Antony Carr Tue, 01 May 2001 16:57
class help
#55
Author: Antony Carr
Date: Tue, 01 May 2001 16:57
276 lines
7504 bytes
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

****************************************************************************
************
FILES
(bank.cpp)
#include <iostream.h>
#include "bank.hpp"
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <fstream.h>
#include <io.h>
#include <process.h>

void main()
{ 
BankAccount bankaccount;
BankDB bankdb;
char option;
clrscr();

do{
clrscr();
cout <<"Welcome to the automated Banking system";
cout <<"\n\nPlease select from the menu below the option that you require:";
cout <<"\n1) Add Account";
cout <<"\n2) Credit Account By Name";
cout <<"\n3) Credit Account By Number";
cout <<"\n4) Debit Account By Name";
cout <<"\n5) Debit Account By Account Number";
cout <<"\n6) Display Accounts";
cout <<"\n0) Administer Database ";
cout <<"\n8) TEST";
cout <<"\n\n7) Quit\nOption: ";
cin  >>option;

if (option=='1'){
cout <<"\nAdd Account";
bankdb.add_a_customer();
getch();
}
if (option=='2'){
cout <<"Credit Account by name";
getch();
}
if (option=='3'){
cout <<"Credit account by number";
bankdb.add_to_balance_by_number();
getch();
}
if (
option=='4'){
cout <<"Debit account by name";
getch();
}
if (option=='5'){
cout <<"Debit account by number";
getch();
}
if (option=='6'){
cout <<"display accounts";
getch();
}
if (option=='8'){
cout <<"test";
bankaccount.addtobalance();
getch();
}
if (option=='0'){
cout <<"DATABASE ADMIN";
bankdb.read_db();
getch();
}
}while(option!='7');
cout <<"Thank you for using this program ( :0)";
}

(bank.hpp)

#include <iostream.h>
#include <fstream.h>
#include <process.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <io.h>
 char name[20];
 char namestr[20];
 float balance;
 float totalbalances;

class BankAccount {
public:

 void addtobalance();
 void subtractballance();
 void return_account_balance();
 void return_account_name();
 void clear_total_of_balances();
 void add_to_total_of_balances();
 void return_total_of_balancess();
 float input;
};
void BankAccount::addtobalance(){

cout <<"Please Enter the amount that you wish to add to the account
of"<<name<<":\n�";
cin>>input;
balance=balance+input;
totalbalances=totalbalances+input;
cout <<"Thank you, the total of the account is now: �"<<balance;
getch();
}
void BankAccount::subtractballance(){
cout <<"Please enter the amount that you wish to subtract from the account
of"<<name<<";\n�";
cin>>input;
balance=balance-input;
totalbalances=totalbalances-input;
cout <<"Thank you, The total of the account is now: �"<<balance;
getch();
}
void BankAccount::return_account_balance(){
cout <<"The account for "<<name<<"has a total balance of:\n\t\t�"<<balance;
getch();
}
void BankAccount::return_account_name(){
cout <<"The account is entered in the name of"<<name;
getch();
}
void BankAccount::clear_total_of_balances(){
cout <<"The total of balances held by this system is now reset to �0";
totalbalances=0;
}
void BankAccount::add_to_total_of_balances(){
cout <<"To add to the total of the balances held in the bank please enter
the amount you wish to add: ";
cout << "�";
cin >> input;
totalbalances=totalbalances+input;
}
void BankAccount::return_total_of_balancess(){
cout <<"The total of balancess held in the bank system is �"<<totalbalances;
}

class BankDB:BankAccount {
public:
 void add_a_customer();
 void add_to_balance_by_number();
 void add_to_balance_by_name();
 void subtract_balance_by_number();
 void subtract_balance_by_name();
 void return_balance_by_number();
 void return_balance_by_name();
 void return_account_name_by_number();
 void show_all_accounts();
 void read_db();
 void write_db();
};
void BankDB::add_a_customer(){
}
void BankDB::add_to_balance_by_number(){
addtobalance();
}
void BankDB::add_to_balance_by_name(){

}
void BankDB::subtract_balance_by_number(){

}
void BankDB::subtract_balance_by_name(){

}
void BankDB::return_balance_by_number(){


}
void BankDB::return_balance_by_name(){

}
void BankDB::return_account_name_by_number(){

}
void BankDB::show_all_accounts(){

}
void BankDB::read_db(){
char filenm[60], accname[20], accbal[20], inc;
 long int acctot = 0;
 int account_count = 0, char_count = 0;
 FILE* infile;

 cout <<"\n\n\t\t\tfilehand.cpp\n\n";

 strcpy (filenm, "g:\\filedata.txt");

 infile = fopen(filenm, "r");

 if(!infile)
 {
  cout<<"input file cannot be opened";
  exit(1);
 }

 accname [0] = '\0';
 do
 {
  char_count = 0;
  while (inc = fgetc(infile))
  {
   if (inc == ',')   file://A comma signifies end of name
    break;
   accname[char_count++] = inc;
  }
  accname[char_count] = '\0';
  cout<<"\n";
  if (inc != EOF)
  {
   char_count = 0;
   while (inc = fgetc(infile))
   {
    if ((inc == '\n') || (inc == EOF)) file://A newline inicates end of
account balance

    break; file://EOF is end of file
   accbal[char_count++] = inc;
   }
   accbal[char_count++] = '\0';

   cout << "\n";
   cout.width(5);
   cout << account_count;
   cout << "     ";
   cout.width(30);
   cout.setf(ios::left);
   cout << accname;
   cout.width (8);
   cout.setf(ios::right);
   cout << accbal;
   account_count++;
   acctot += atol(accbal);   file://Convert balance to a long and add to
total
  }

 }while (inc != EOF);

 cout << "\n\n\nAccounts read = "<< account_count;
 cout << "\nAcounts total = " << acctot;
 getch();
 fclose(infile);


}
void BankDB::write_db(){

}
Re: class help
#56
Author: R. Scott Mellow
Date: Tue, 01 May 2001 18:56
60 lines
3748 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

[snip] and a half

I cannot help you with your specific problem because I am unable to
compile (let alone read) your code. From what I can discern, I have the
following advice:

Rethink your design. For starters, you need more than 2 classes because
you have more than 2 distinct concepts. Also, the inheritance between
BankDB and BankAccount does not make sense. Generally, an inheritance
would imply an is_a relationship. i.e., if class C inherits from class B
then C is a B.

One thing you might want to do as an exercise is to come up with a
BankAccount class that has only what it needs... nothing else. Do the
same for the other 'objects' in your program. When you have each
object's interface written down, then work on how they interact with
each other. What I often do is create the class with only data and
constructors. Once all my classes are written this way, I start trying
to use them in a program. The interface (public methods) almost writes
itself.

When you are to the point of implementing your new design, ask here
about any specific problems you have with the code. You should read the
faq
     http://snurse-l.org/acllc-c++/faq

before you do because that can also give you ideas on how to solve your
problems.

hth
R.
Re: class help
#57
Author: John Harrison
Date: Tue, 01 May 2001 20:34
61 lines
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
Re: class help
#58
Author: Ron Natalie
Date: Tue, 01 May 2001 20:41
22 lines
1977 bytes

John Harrison wrote:

>     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.

No, BankDB holds exactly 100 BankAccount objects, not upto.
If you want a dynamic number try:
	vector<BankAccount> the_accounts;

> 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.

At his skill level, he should be learning to use the standard containers
rather than screwing around with the suboptimal array type in C++.  I
recommend Koenig and Moo's Accelerated C++ as a text that teaches you
the good habits first, rather than propagating C ugliness into C++.
Re: class help
#59
Author: John Harrison
Date: Tue, 01 May 2001 21:04
40 lines
2753 bytes
"Ron Natalie" <ron@spamcop.net> wrote in message
news:3AEF1F74.C963F0B7@spamcop.net...
>
>
> John Harrison wrote:
>
> >     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.
>
> No, BankDB holds exactly 100 BankAccount objects, not upto.

I think that depends on your point of view. Notionally the BankDB may hold
upto 100 accounts, even if 'physically' its always holding exactly 100.
Proabably that distinction was why I put 100 in brackets.

> If you want a dynamic number try:
> vector<BankAccount> the_accounts;

Yep, if the OP wants to go for this then by all means.

>
> > 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.
>
> At his skill level, he should be learning to use the standard containers
> rather than screwing around with the suboptimal array type in C++.  I
> recommend Koenig and Moo's Accelerated C++ as a text that teaches you
> the good habits first, rather than propagating C ugliness into C++.

Again I couldn't agree more, I felt however that the OP was unlikely to take
this advice. Just an intuition.

john
Re: class help
#60
Author: Antony Carr
Date: Wed, 02 May 2001 19:31
8 lines
1278 bytes
Thanks for the feedback ... i am just trying to get my head aroud this
collage project ... i guess its time for a re-think and start again .... :-)
just one thing ... it does compile honest !! Well it does in borland c++
anyway ...



Antony CArr
Re: class help
#61
Author: Regina Momnie
Date: Sun, 06 May 2001 04:19
299 lines
8419 bytes
Heres a thought dude, why dont you have BankAccount have a variable which
holdes the name in which the account is held.  Even a 13year old could
figure that one out buddy.



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
>
>
****************************************************************************
> ************
> FILES
> (bank.cpp)
> #include <iostream.h>
> #include "bank.hpp"
> #include <stdio.h>
> #include <conio.h>
> #include <ctype.h>
> #include <string.h>
> #include <fstream.h>
> #include <io.h>
> #include <process.h>
>
> void main()
>

> BankAccount bankaccount;
> BankDB bankdb;
> char option;
> clrscr();
>
> do{
> clrscr();
> cout <<"Welcome to the automated Banking system";
> cout <<"\n\nPlease select from the menu below the option that you
require:";
> cout <<"\n1) Add Account";
> cout <<"\n2) Credit Account By Name";
> cout <<"\n3) Credit Account By Number";
> cout <<"\n4) Debit Account By Name";
> cout <<"\n5) Debit Account By Account Number";
> cout <<"\n6) Display Accounts";
> cout <<"\n0) Administer Database ";
> cout <<"\n8) TEST";
> cout <<"\n\n7) Quit\nOption: ";
> cin  >>option;
>
> if (option=='1'){
> cout <<"\nAdd Account";
> bankdb.add_a_customer();
> getch();
> }
> if (option=='2'){
> cout <<"Credit Account by name";
> getch();
> }
> if (option=='3'){
> cout <<"Credit account by number";
> bankdb.add_to_balance_by_number();
> getch();
> }
> if (
> option=='4'){
> cout <<"Debit account by name";
> getch();
> }
> if (option=='5'){
> cout <<"Debit account by number";
> getch();
> }
> if (option=='6'){
> cout <<"display accounts";
> getch();
> }
> if (option=='8'){
> cout <<"test";
> bankaccount.addtobalance();
> getch();
> }
> if (option=='0'){
> cout <<"DATABASE ADMIN";
> bankdb.read_db();
> getch();
> }
> }while(option!='7');
> cout <<"Thank you for using this program ( :0)";
> }
>
> (bank.hpp)
>
> #include <iostream.h>
> #include <fstream.h>
> #include <process.h>
> #include <string.h>
> #include <stdio.h>
> #include <conio.h>
> #include <stdlib.h>
> #include <io.h>
>  char name[20];
>  char namestr[20];
>  float balance;
>  float totalbalances;
>
> class BankAccount {
> public:
>
>  void addtobalance();
>  void subtractballance();
>  void return_account_balance();
>  void return_account_name();
>  void clear_total_of_balances();
>  void add_to_total_of_balances();
>  void return_total_of_balancess();
>  float input;
> };
> void BankAccount::addtobalance(){
>
> cout <<"Please Enter the amount that you wish to add to the account
> of"<<name<<":\n�";
> cin>>input;
> balance=balance+input;
> totalbalances=totalbalances+input;
> cout <<"Thank you, the total of the account is now: �"<<balance;
> getch();
> }
> void BankAccount::subtractballance(){
> cout <<"Please enter the amount that you wish to subtract from the account
> of"<<name<<";\n�";
> cin>>input;
> balance=balance-input;
> totalbalances=totalbalances-input;
> cout <<"Thank you, The total of the account is now: �"<<balance;
> getch();
> }
> void BankAccount::return_account_balance(){
> cout <<"The account for "<<name<<"has a total balance
of:\n\t\t�"<<balance;
> getch();
> }
> void BankAccount::return_account_name(){
> cout <<"The account is entered in the name of"<<name;
> getch();
> }
> void BankAccount::clear_total_of_balances(){
> cout <<"The total of balances held by this system is now reset to �0";
> totalbalances=0;
> }
> void BankAccount::add_to_total_of_balances(){
> cout <<"To add to the total of the balances held in the bank please enter
> the amount you wish to add: ";
> cout << "�";
> cin >> input;
> totalbalances=totalbalances+input;
> }
> void BankAccount::return_total_of_balancess(){
> cout <<"The total of balancess held in the bank system is
�"<<totalbalances;
> }
>
> class BankDB:BankAccount {
> public:
>  void add_a_customer();
>  void add_to_balance_by_number();
>  void add_to_balance_by_name();
>  void subtract_balance_by_number();
>  void subtract_balance_by_name();
>  void return_balance_by_number();
>  void return_balance_by_name();
>  void return_account_name_by_number();
>  void show_all_accounts();
>  void read_db();
>  void write_db();
> };
> void BankDB::add_a_customer(){
> }
> void BankDB::add_to_balance_by_number(){
> addtobalance();
> }
> void BankDB::add_to_balance_by_name(){
>
> }
> void BankDB::subtract_balance_by_number(){
>
> }
> void BankDB::subtract_balance_by_name(){
>
> }
> void BankDB::return_balance_by_number(){
>
>
> }
> void BankDB::return_balance_by_name(){
>
> }
> void BankDB::return_account_name_by_number(){
>
> }
> void BankDB::show_all_accounts(){
>
> }
> void BankDB::read_db(){
> char filenm[60], accname[20], accbal[20], inc;
>  long int acctot = 0;
>  int account_count = 0, char_count = 0;
>  FILE* infile;
>
>  cout <<"\n\n\t\t\tfilehand.cpp\n\n";
>
>  strcpy (filenm, "g:\\filedata.txt");
>
>  infile = fopen(filenm, "r");
>
>  if(!infile)
>  {
>   cout<<"input file cannot be opened";
>   exit(1);
>  }
>
>  accname [0] = '\0';
>  do
>  {
>   char_count = 0;
>   while (inc = fgetc(infile))
>   {
>    if (inc == ',')   file://A comma signifies end of name
>     break;
>    accname[char_count++] = inc;
>   }
>   accname[char_count] = '\0';
>   cout<<"\n";
>   if (inc != EOF)
>   {
>    char_count = 0;
>    while (inc = fgetc(infile))
>    {
>     if ((inc == '\n') || (inc == EOF)) file://A newline inicates end of
> account balance
>
>     break; file://EOF is end of file
>    accbal[char_count++] = inc;
>    }
>    accbal[char_count++] = '\0';
>
>    cout << "\n";
>    cout.width(5);
>    cout << account_count;
>    cout << "     ";
>    cout.width(30);
>    cout.setf(ios::left);
>    cout << accname;
>    cout.width (8);
>    cout.setf(ios::right);
>    cout << accbal;
>    account_count++;
>    acctot += atol(accbal);   file://Convert balance to a long and add to
> total
>   }
>
>  }while (inc != EOF);
>
>  cout << "\n\n\nAccounts read = "<< account_count;
>  cout << "\nAcounts total = " << acctot;
>  getch();
>  fclose(infile);
>
>
> }
> void BankDB::write_db(){
>
> }
>
>
>
>
>
>
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads