
03-Jun-2006, 20:11
|
|
New Member
|
|
Join Date: May 2006
Location: USA
Posts: 25
|
|
|
Re: How to create an implementation for the overloaded << operator
I made some changes from the last post I made. The problem I am having was when I entered the account number for some reason they don't output the list of the account number I entered. I managed to output the first entry I entered but I can't output the entire list. But it list the total number of entry. I tried to debbug. I don't know where to start or how to fix them. Any idea? Please help
Here is the entire program:
bankAccount.cpp
#include <iostream>
#include <string>
#include "bankAccount.h"
#include "bankList.h"
using namespace std;
int main ()
{
bankList myList;
bankAccount myAcct;
int inputNum = 0, num = 0;
int acctSize = 5;
int mySize;
string keyEnter = "";
if (myList.empty())
{
cout << "Empty List: \n" << myList << endl;
}
mySize = myList.getCapacity();
cout << "Account Size is: " << mySize << endl;
while (keyEnter != "quit")
{
cout << "Please enter an account number (to stop enter '1010'): ";
cin >> inputNum[num];
if (inputNum == 1010)
{
keyEnter = "quit";
}
else
{
myAcct.setAcctNum(inputNum);
myList.insert(myAcct, num);
num++;
}
}
cout << "List are: " << myList << endl;
mySize = myList.getCapacity();
cout << "Account Size is: " << mySize << endl;
}
bankAccount.h
//bankAccount.h - Bank Account class header file
#include <iostream>
#include <string>
using namespace std;
#pragma once
class bankAccount
{
friend class bankList;
public:
//Constructors
bankAccount();
bankAccount(string acctName, string acctType, int acctNum, double amtDeposit, double amtWithdraw, double intRate, double overDraft);
//Accessors
string getAcctName() const;
string getTypeAcct() const;
int getAcctNumber() const;
double getAmtDeposit() const;
double getAmtWithdraw() const;
double getIntRate() const;
double getOvrdraft() const;
//Mutators
void setAcctName(string acctName);
void setAcctType(string acctType);
void setAcctNum(int acctNum);
void setAmtDeposit(double amtDeposit);
void setAmtWithdraw(double amtWithdraw);
void setNewAcctName(string acctName);
void setInterestRate(double intRate);
void setOverdraft(double overDraft);
//Utilities
void displayCheckBal(ostream & out) const;
void displayAcctStatus(ostream & out) const;
double calcInterest();
double calcBalanceAmt();
void display(ostream & out) const;
private:
int accountNumber;
string accountName, accountType;
double amountDeposit, amountWithdraw, interestRate, overdraft;
};
//prototype of output operator
ostream & operator<< (ostream &out, const bankAccount &accNum);
bankList.cpp
#include <new>
#include <cassert>
#include <iostream>
using namespace std;
#include "bankList.h"
bankList::bankList(int maxAccount)
: accountSize(), accountCapacity(maxAccount)
{
myArrayPtr = new(nothrow) bankAccount[maxAccount];
assert(myArrayPtr !=0);
}
//class destructor
bankList::~bankList()
{
delete [] myArrayPtr;
}
//copy constructor
bankList::bankList(const bankList & origList)
: accountSize(origList.accountSize), accountCapacity(origList.accountCapacity)
{
myArrayPtr = new(nothrow) bankAccount[accountCapacity];
if(myArrayPtr != 0) //check if memory available
{
//copy origList's array into this new array
for (int i = 0; i < accountCapacity; i++)
{
myArrayPtr[i] = origList.myArrayPtr[i];
}
}
else
{
cerr << "*** Inadequate memory to allocate stack ***\n";
exit(1);
}
}
//asignment operator
bankList & bankList::operator=(const bankList & origList)
{
if (this != &origList) // check for bankList = bankList
{
accountSize = origList.accountSize;
accountCapacity = origList.accountCapacity;
//allocate a new array if necessary
if (accountCapacity != origList.accountCapacity)
{
delete [] myArrayPtr;
myArrayPtr = new(nothrow) bankAccount[accountCapacity];
if (myArrayPtr == 0) //check if memory available
{
cerr << "*** Inadequate memory to allocate stack ***\n";
exit(1);
}
}
//copy origList's array into this new array
for(int i = 0; i<accountCapacity; i++)
{
myArrayPtr[i] = origList.myArrayPtr[i];
}
}
return *this;
}
//definition of retreiveItem
void bankList::retrieveItem(bankAccount &key, bool &found)
{
int midPoint, first = 0, last = accountSize - 1;
bool moreToSearch = (first <= last);
found = false;
while (moreToSearch && !found)
{
midPoint = (first + last) / 2;
if (key.accountNumber < myArrayPtr[midPoint].accountNumber)
{
last = midPoint -1;
moreToSearch = (first <= last);
}
else
{
if (key.accountNumber > myArrayPtr[midPoint].accountNumber)
{
first = midPoint + 1;
moreToSearch = (first <= last);
}
else
{
found = true;
key.accountNumber = myArrayPtr[midPoint].accountNumber;
}
}
}
}
//definition of isFull
bool bankList::isFull() const
//full if list is at capacity
{
return (accountSize == accountCapacity);
}
//definition of empty()
bool bankList::empty() const
{
return accountSize == 0;
}
//definition of sizeIs()
int bankList::sizeIs() const
{
return accountSize;
}
//maximum Capacity;
int bankList::getCapacity() const
{
if (accountSize == accountCapacity)
{
return accountCapacity;
}
return accountSize;
}
//Definition of insert()
void bankList::insert(bankAccount &newEntry, int pos)
{
if (accountSize == accountCapacity)
{
cerr << "*** No Space for list element -- terminating execution ***\n";
exit(1);
}
if (pos < 0 || pos > accountSize)
{
cerr << "*** Illegal location to insert -- " << pos << ". List unchanged. ***\n";
return;
}
for (int i = accountSize; i > pos; i--)
{
newEntry.accountNumber = myArrayPtr[i - 1].accountNumber;
}
myArrayPtr[pos] = newEntry;
accountSize++;
}
//deleteItem
void bankList::deleteItem(bankAccount &entry, int pos)
{
if (accountSize == 0)
{
cerr << "*** List is empty ***\n";
return;
}
if (pos < 0 || pos >= accountSize)
{
cerr << "Illegal location to delete -- " << pos << ". List unchanged. ***\n";
return;
}
for (int i = pos; i < accountSize; i++)
{
entry.accountNumber = myArrayPtr[i+1].accountNumber;
}
accountSize++;
}
//definition of display()
void bankList::display( ostream & out) const
{
for(int i=0; i < accountSize; i++)
{
out << myArrayPtr[i].accountNumber << " ";
}
}
//definition of out operator
ostream & operator<< (ostream & out, const bankList & accNum)
{
accNum.display(out);
return out;
}
bankList.h
#include <iostream>
#include "bankAccount.h"
#ifndef BANK
#define BANK
typedef bankAccount bankAccount;
class bankList
{
public:
//class constructor
bankList(int maxAccount = 5);
//class destructor
~bankList();
//copy constructor
bankList(const bankList & origList);
//assignment
bankList & operator = (const bankList & origList);
// isFull operation
bool isFull () const;
//empty operation
bool empty() const;
//sizeIs operation
int sizeIs() const;
//getCapacity here
int getCapacity() const;
//retrieve item
void retrieveItem (bankAccount &key, bool &found); // find a "key"
//insertItem
void insert(bankAccount &newEntry, int pos);
//delete item
void deleteItem(bankAccount &entry, int pos);
//dislay
void display(ostream & out) const;
private:
int accountSize;
int accountCapacity;
bankAccount *myArrayPtr;
};
//prototype of output operator
ostream & operator<< (ostream &out, const bankList &accNum);
#endif
implementation Bank
/*Rosalin Rosario-Liam
HW6 - "Bank Account Class"
Due Tuesday, May 9, 2006 by 11:00 pm
*/
#include <iostream>
#include <string>
#include "bankAccount.h"
#include "bankList.h"
using namespace std;
int main ()
{
bankList myList;
bankAccount myAcct;
int inputNum = 0, num = 0;
int acctSize = 5;
int mySize;
string keyEnter = "";
if (myList.empty())
{
cout << "Empty List: \n" << myList << endl;
}
mySize = myList.getCapacity();
cout << "Account Size is: " << mySize << endl;
while (keyEnter != "quit")
{
cout << "Please enter an account number (to stop enter '1010'): ";
cin >> inputNum;
if (inputNum == 1010)
{
keyEnter = "quit";
}
else
{
myAcct.setAcctNum(inputNum);
myList.insert(myAcct, num);
}
}
cout << "List are: " << myList << endl;
mySize = myList.getCapacity();
cout << "Account Size is: " << mySize << endl;
}
|