Click to See Complete Forum and Search --> : Need Help Desperately Please


breeze19965
04-15-2002, 02:46 PM
I have a program that I have completed, now the instructor has ask me to add
this to it. 1.Create copy constructor, don't have to implement it yet. 2.
create an overloaded addition operator, that accepts as its parameters two
exsiting "account" objects. try to use the friend function. 3. create another
class that will represent another type of account. this class will inherit
from the account class. don't have to implement it yet either. 2. make the
deposit and withdraw functions in the account class polymorphic and create
the function declaration for them in your new class that you created in #3.
there's more but help with this would be greatly apprieciated. here is a
copy of all my files of what I have already completed. it's lenghtly....
===========================================================================
***.h file****
#if !defined(AFX_BANKACCOUNT_H__3001AFE6_150D_4591_9686_C491594EAC69__INCLUDED_)
#define AFX_BANKACCOUNT_H__3001AFE6_150D_4591_9686_C491594EAC69__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class BankAccount
{
public:
BankAccount(double startingBal);
virtual ~BankAccount();
double getBal() const {return Bal;}

bool deposit (double amount);
bool withdraw (double amount);

bool operator++();
bool operator--();

private:
double Bal;


};


#endif // !defined(AFX_BANKACCOUNT_H__3001AFE6_150D_4591_9686_C491594EAC69__INCLUDED_)
====================================================================
***.cpp file****
#include "BankAccount.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

BankAccount::BankAccount(double startingBal) : Bal (0.0)
{
if (startingBal >0.0)
{
Bal = startingBal;
}

}

BankAccount::~BankAccount()
{
//class objects destoryed

}


bool BankAccount::deposit (double amount)
{
if (amount >= 0.0)
{
Bal += amount;
return true;
}
else
{
return false;
}
}


bool BankAccount::withdraw (double amount)
{
if ((amount >= 0.0) && (amount <= Bal))
{
Bal -= amount;
return true;
}
else
{
return false;
}
}


bool BankAccount::operator++()
{
static const double incrementAmount = 50.00;
return (deposit(incrementAmount));
}


bool BankAccount::operator -- ()
{
static const double decrementAmount = 50.00;
return (withdraw(decrementAmount));
}
=========================================================================
***2nd .h file***
if !defined(AFX_MASTERACCOUNT_H__6B4A5CBA_ED2D_475F_9457_39BA7BE82057__INCLUDED_)
#define AFX_MASTERACCOUNT_H__6B4A5CBA_ED2D_475F_9457_39BA7BE82057__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "BankAccount.h"


class MasterAccount
{
public:
MasterAccount(double checkingstart, double savingsstart);
virtual ~MasterAccount();
BankAccount &getchecking() const {return *checkingAccount;}
BankAccount &getsaving() const {return *savingAccount;}

static int getNoAccounts() {return NoAccounts;}

private:
BankAccount *checkingAccount;
BankAccount *savingAccount;

static int NoAccounts;

};

#endif // !defined(AFX_MASTERACCOUNT_H__6B4A5CBA_ED2D_475F_9457_39BA7BE82057__INCLUDED_)
=======================================================================
*****2nd .cpp file***
include "MasterAccount.h"

//LaVonda

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

int MasterAccount::NoAccounts = 0;

MasterAccount::MasterAccount(double checkingstart, double savingsstart)
:checkingAccount(), savingAccount()
{
checkingAccount = new BankAccount(checkingstart);
savingAccount = new BankAccount(savingsstart);

NoAccounts += 2;

}

MasterAccount::~MasterAccount()
{
delete checkingAccount;
delete savingAccount;

NoAccounts -=2;

}
===================================================================
*** Main .ccp that puts it all together*******
#include "MasterAccount.h"
#include "BankAccount.h"
#include <iostream.h>

MasterAccount *OpenAccts();

void main()
{
MasterAccount *customer = OpenAccts();

int choice = 0;
do
{
cout<<"1. get checking balance"<<endl;
cout<<"2. deposit into checking"<<endl;
cout<<"3. withdraw from checking"<<endl;
cout<<"4. get savings balance"<<endl;
cout<<"5. deposit into savings"<<endl;
cout<<"6. withdraw from savings"<<endl;
cout<<"7. increment savings"<<endl;
cout<<"8. decrement checking"<<endl;
cout<<"9. quit"<<endl;
cout<<endl;
cout<<"enter choice---> ";
cin>>choice;

cout<<endl <<endl;

switch(choice)
{
case 1: //get checking balance
{
BankAccount& account = customer->getchecking();
cout<<"checking balance---> "<<account.getBal();
cout<<endl;
}
break;

case 2: //deposit into checking
{
double depositamount = 0.0;
cout<<"enter deposit amount---> ";
cin>>depositamount;
cout<<endl;

BankAccount& account = customer->getchecking();

if(!account.deposit (depositamount))
cout<<"invalid transaction"<<endl;
}
break;

case 3: //withdraw from checking
{
double withdrawamount = 0.0;
cout<<"enter withdrawal---> ";
cin>>withdrawamount;
cout<<endl;

BankAccount& account = customer->getchecking();

if(!account.withdraw (withdrawamount))
cout<<"invalid transaction---> "<<endl;
}
break;

case 4: //get savings balance
{
BankAccount& account = customer->getsaving();
cout<<"saving balance---> "<<account.getBal();
cout<<endl;
}
break;

case 5: //deposit into savings
{
double depositamount = 0.0;
cout<<"enter deposit amount---> ";
cin>>depositamount;
cout<<endl;

BankAccount& account = customer->getsaving();

if(!account.deposit (depositamount))
cout<<"invalid transaction"<<endl;
}
break;

case 6: //withraw from savings
{
double withdrawamount = 0.0;
cout<<"enter withdrawal---> ";
cin>>withdrawamount;
cout<<endl;

BankAccount& account = customer->getsaving();

if(!account.withdraw (withdrawamount))
cout<<"invalid transaction"<<endl;
}
break;

case 7: //addinterest to savings
{
BankAccount& account = customer->getsaving();

if(!++account)
cout<<"invalid transaction"<<endl;
}
break;

case 8: //subtract service charge from checking
{
BankAccount& account = customer->getchecking();

if(!--account)
cout<<"invalid transaction"<<endl;
}
break;

case 9: //quit
break;
default:
continue;
}
}while (choice!=9);

delete (customer);
}

MasterAccount *OpenAccts()
{
double checking =0.0;
double saving = 0.0;

do
{
cout<<"Enter the starting balance for checking---> ";
cin>>checking;
cout<<endl;

if(checking<= 0.0)
{
cout<<"invalid amount"<<endl;
}

}while(checking <= 0.0);

do
{
cout<<"Enter the starting balance for savings---> ";
cin>>saving;
cout<<endl;

if(saving <= 0.0)
{
cout<<"invalid amount"<<endl;
}

}while(saving<= 0.0);

cout<<endl <<endl;
return (new MasterAccount(checking, saving));
}

==============================================================
Now as for #1. question I thought about putting the "copy constructor" in
the "Main.cpp" file and add it right after the [delete(customer)], and this
is what I thought would work***** BankAccount *begingBal(startingBal);
and for #2 the overload addition operator, I am not sure, but I had thought
something like this**** "BankAccount.h" file in the public section adding
"BankAccount();" then in the private adding *** doubleMoInterest;
friend double getInterest(Bal * MoInterest); then in the "BankAccount.cpp"file
at the bottom put*** double getInterest(Bal * MoInterest){return MoInterest->Interest;}
BankAccount::BankAccount(); {Interest=0.02;}.
I need to know will this work am I on the right track or not and if not will
someone please tell me how to do it... I am desperate, and stressed out to
the max. Thank you ahead of time...

Christophe Brun
04-16-2002, 08:40 AM
Hi !

I guess you misunderstood the question you've been asked.

#1 When you write BankAccount *begingBal(startingBal);
you're not creating a copy constructor, you're just asking for a construction
by copy. I think the question is : write a declaration for a copy constructor
for your class.

#2 I don't understand nothing on your explanation here. But the question
is, after me : write a declaration for operator+(...) for your class. operator+(...)
can be implemented either as a member function or as an independent function
declared as friend by your class. You instructor wants you to declare the
second form.

#3 Just declare a claas that inherits your first one

#4 I think you should think about what is polymorphism. When you've got it,
then you've got the solution for item 4 (which also expects you only to write
the declarations).