GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Jun-2006, 18:38
febrarierose febrarierose is offline
New Member
 
Join Date: May 2006
Location: USA
Posts: 25
febrarierose is on a distinguished road

How to create an implementation for the overloaded << operator


Hi

Help please. I'm having problems how to create an implementation for List's overloaded << operator. I don't get how to use them.

here are my code:

the .cpp
CPP / C++ / C Code:
//definition of display()
void bankList::display(ostream & out) const
{
	for(int i=0; i < accountSize; i++)
	{
		out << myArrayPtr[i] << " ";
	}
}
//definition of out operator
ostream & operator<< (ostream & out, const bankList & accNum)
{
	accNum.display(out);
	return out;
}



.h file

CPP / C++ / C Code:

class bankList
{
public:

	//dislay
	void display(ostream & out) const;

private:
	int accountSize;
	int accountCapacity;
	bankAccount *myArrayPtr;
};

//prototype of output operator
ostream & operator<< (ostream &out, const bankList &accNum);




Textbook was not helpful. Please help...

thank you
  #2  
Old 03-Jun-2006, 19:39
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: How to create an implementation for the overloaded << operator


What's the problem?
  #3  
Old 03-Jun-2006, 20:11
febrarierose febrarierose is offline
New Member
 
Join Date: May 2006
Location: USA
Posts: 25
febrarierose is on a distinguished road

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
CPP / C++ / C Code:
#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
CPP / C++ / C Code:
//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
CPP / C++ / C Code:

#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
CPP / C++ / C Code:
#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
CPP / C++ / C Code:
/*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;


}
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help overloading operator << nikp C++ Forum 2 09-Jan-2006 13:46
C++ PhoneBook marita C++ Forum 46 12-Jun-2005 12:10
problem with the << operator overload clander C++ Forum 2 29-Apr-2005 11:06
I need help implementing kjc_13 C++ Forum 0 14-Feb-2005 16:00

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:08.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.