Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 04-05-2003, 07:30 PM   #1 (permalink)
mik0rs
Registered User
 
Join Date: Apr 2003
Posts: 2
mik0rs is on a distinguished road
I'm having some problems with inheritance... <-- newb

BankAccount.h
Code:
//---------------------------------------------------------------------------
#ifndef BankAccountH
#define BankAccountH
//---------------------------------------------------------------------------
//BankAccount: class declaration

//#include "Date.h"
//#include "Transaction.h"
#include "TransactionList.h"

#include <fstream.h>	// for files
#include <iostream.h>
#include <iomanip.h>

class BankAccount {
	friend ostream& operator<<( ostream&, const BankAccount&);	//output operator
	friend istream& operator>>( istream&, BankAccount&);	//input operator
public:
    BankAccount();
    BankAccount( string, string, Date_, float, TransactionList);
    BankAccount( string, string, string, Date_, float, TransactionList);

    string getType() const;
    string getAccountNumber() const;
    string getSortCode() const;
    Date_ getCreationDate() const;
	float getBalance() const;
    TransactionList getTransactions() const;

    virtual void displayAccount() const;
    virtual void viewStatement() const;
    virtual void recordWithdrawal( float);
    virtual void recordDeposit( float);  //modified in savings and child accounts? - by Mike
	virtual void deleteOldTransactions();
	virtual void recordTransferIn( float);   //To BE CHECKED AND COMPLETED
	virtual void recordTransferOut( float);   //To BE CHECKED AND COMPLETED

	virtual bool validateWithdrawal( float) const;

    virtual void readInBankAccountFromFile( string);
	virtual void storeBankAccountInFile( string st) const;
private:
    //data items
    string type;
    string accountNumber;
    string sortCode;
    Date_ creationDate;
	float balance;
    TransactionList transactions;
    void updateBalance( float);
};

#endif
BankAccount.cpp
Code:
//---------------------------------------------------------------------------
#include vcl.h
#pragma hdrstop

#include "BankAccount.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
#include typeinfo.h

//BankAccount: class implementation

//public member functions

BankAccount::BankAccount()
    : accountNumber( "null"),
      sortCode( "null"),
	  balance( 0)
{}
BankAccount::BankAccount( string acctNum, string sCode,
                                Date_ cD, float b,
                                TransactionList trList)
    : accountNumber( acctNum), sortCode( sCode),
      creationDate( cD), balance( b),
      transactions( trList)
{}
BankAccount::BankAccount( string t, string acctNum, string sCode,
                                Date_ cD, float b,
                                TransactionList trList)
    : type(t),
      accountNumber( acctNum), sortCode( sCode),
      creationDate( cD), balance( b),
      transactions( trList)
{}

string BankAccount::getType() const {
    return type;
}
string BankAccount::getAccountNumber() const {
    return accountNumber;
}
string BankAccount::getSortCode() const {
    return sortCode;
}
Date_ BankAccount::getCreationDate() const {
    return creationDate;
}
float BankAccount::getBalance() const {
    return balance;
}
TransactionList BankAccount::getTransactions() const {
    return transactions;
}
bool BankAccount::validateWithdrawal( float amountToWithdraw ) const {
    //check if enough money in account (without allowed overdraft)
    if (( balance >= amountToWithdraw))
        return true;   //enough money
    else
        return false;  //not enough money
}
void BankAccount::recordWithdrawal( float amountToWithdraw) {
    //create a withdrawal transaction
    Transaction aTransaction( "cash_withdrawal_from_ATM", -amountToWithdraw);
    //update bank account
    transactions.addInFront( aTransaction);   //update transactions
    updateBalance( -amountToWithdraw);   //decrease balance
}
void BankAccount::recordDeposit( float amountToDeposit) {
    //create a deposit transaction
	Transaction aTransaction( "cash_deposit_to_ATM", amountToDeposit);
    //update bank account
    transactions.addInFront( aTransaction);   //update transactions
    updateBalance( amountToDeposit);   //increase balance
}
void BankAccount::deleteOldTransactions() {
    //delete old transactions
    if (! transactions.isEmpty())	//Precondition: for function deleteOld
	transactions.deleteOld();   //clear all more than 10 year old transactions
}
void BankAccount::recordTransferOut( float amountToTransfer) {
    //create transfer transactions from active account
    Transaction outTransaction( "cash_transfer_out", amountToTransfer);
//TO BE CHECKED & COMPLETED
}
void BankAccount::recordTransferIn( float amountToTransfer) {
    //create transfer transactions to transfer account
    Transaction inTransaction( "cash_transfer_in", amountToTransfer);
//TO BE CHECKED & COMPLETED
}
void BankAccount::viewStatement() const {
	displayAccount();
}
void BankAccount::displayAccount() const {
    cout << "\nCLASS:           " << typeid(*this).name();	//display account class
    cout << "\n " << type << " ACCOUNT" ;	//display account type (from file)
    cout << "\nACCOUNT NUMBER:  " << accountNumber;	//display account number
	cout << "\nSORT CODE:       " << sortCode;	//display sort code
    cout << "\nCREATION DATE:   " << creationDate;	//display creation date
	cout << "\nLIST OF TRANSACTIONS: \n";                  //display transactions
    TransactionList hTr( transactions);
	if ( hTr.isEmpty())
		cout << "EMPTY!!!";
	else
		hTr.showTransactionList();	//display all transactions, one per line
	cout << "\nBALANCE:         " << (char)156 << balance;	//display balance
}
void BankAccount::readInBankAccountFromFile( string st) {
	ifstream fromFile;
	fromFile.open( st.c_str(), ios::in); 	//open file in read mode
	if ( fromFile.fail())
		cout << "\nAN ERROR HAS OCCURRED WHEN OPENING THE FILE.";
	else
        fromFile >> (*this);  	//read  all info from bank account file
    fromFile.close();			//close file: optional here
}
void BankAccount::storeBankAccountInFile( string st) const {
	ofstream toFile;
	toFile.open( st.c_str(), ios::out);	//open copy file in write mode
	if ( toFile.fail())
		cout << "\nAN ERROR HAS OCCURRED WHEN OPENING THE FILE.";
	else
        toFile << (*this);	//store all info to bank account file
	toFile.close();			//close file: optional here
}

//---------------------------------------------------------------------------
//private member functions
void BankAccount::updateBalance( float amount) {
    balance = balance + amount;   //add/take amount to/from balance
}
//NOT NEEDED WITH ABSTRACT CLASS ???
//---------------------------------------------------------------------------
//friend functions

//ostream - output to a file, writes to a file
ostream& operator<<( ostream& os, const BankAccount& aBankAccount) {
						//output formatted BankAccount
    os << aBankAccount.getType() << "\n";	//store account type
    os << aBankAccount.getAccountNumber() << "\n";	//store account number
	os << aBankAccount.getSortCode() << "\n";	//store sort code
    os << aBankAccount.getCreationDate() << "\n";	//store creation date
	os << aBankAccount.getBalance() << "\n";	//store balance
    TransactionList hTr( aBankAccount.getTransactions());
    while ( ! ( hTr.isEmpty())) 	//while not empty
	{
		os << hTr.first() << "\n";	//copy each transaction (and <Return>) in file
	    hTr.deleteFirst();	//delete transaction from list of transactions
	}
	return os;
}

//istream - input to the program, reads from a file
istream& operator>>( istream& is, BankAccount& aBankAccount) {
						//read in BankAccount details
    is >> aBankAccount.type;	//read account type
    is >> aBankAccount.accountNumber;	//read account number
	is >> aBankAccount.sortCode;	//read sort code
 	is >> aBankAccount.creationDate;	//read creation date
	is >> aBankAccount.balance;	//read balance
	Transaction aTransaction;
	is >> aTransaction;	//read first transaction
    while ( is != 0) 	//while not end of file…
    {
        aBankAccount.transactions.addAtEnd( aTransaction);   //add transaction to history of transactions
        is >> aTransaction;	//read in next transaction
	}
	return is;
}
My 1st derived class
CurrentAccount.h
Code:
//---------------------------------------------------------------------------
#ifndef CurrentAccountH
#define CurrentAccountH
//---------------------------------------------------------------------------
//CurrentAccount: class declaration

#include "Date.h"
//#include "Transaction.h"
#include "TransactionList.h"
#include "BankAccount.h"

#include fstream.h>	// for files
#include iostream.h>
#include iomanip.h>

class CurrentAccount : public BankAccount {
	friend ostream& operator<<( ostream&, const CurrentAccount&);	//output operator
	friend istream& operator>>( istream&, CurrentAccount&);	//input operator
public:
    //constructors
    CurrentAccount();
    CurrentAccount( float);
    CurrentAccount( string, string, string, Date_, float, float, TransactionList);

    //methods
    float getOverdraftLimit() const;

private:
    float overdraftLimit;

};

#endif
CurrentAccount.cpp
Code:
//---------------------------------------------------------------------------
#include vcl.h
#pragma hdrstop

#include "CurrentAccount.h"
#include "BankAccount.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
#include typeinfo.h

//CurrentAccount: class implementation

//public member functions

CurrentAccount::CurrentAccount()
    : overdraftLimit( 0)
{}

CurrentAccount::CurrentAccount( float oL)
    : overdraftLimit( oL)
{}

CurrentAccount::CurrentAccount( string t, string acctNum,
                                string sCode, Date_ cD,
                                float oL, float b,
                                TransactionList trList)
    : BankAccount( t, acctNum, sCode, cD, b, trList),
      overdraftLimit( oL)
{}

float CurrentAccount::getOverdraftLimit() const {
    return overdraftLimit;
}

//---------------------------------------------------------------------------
//friend functions

//ostream - output to a file, writes to a file
ostream& operator<<( ostream& os, const CurrentAccount& aCurrentAccount) {
						//output formatted CurrentAccount
    os << aCurrentAccount.getType() << "\n";	//store account type
    os << aCurrentAccount.getAccountNumber() << "\n";	//store account number
	os << aCurrentAccount.getSortCode() << "\n";	//store sort code
    os << aCurrentAccount.getCreationDate() << "\n";	//store creation date
    os << aCurrentAccount.getOverdraftLimit() << "\n";   //store overdraft limit
	os << aCurrentAccount.getBalance() << "\n";	//store balance
    TransactionList hTr( aCurrentAccount.getTransactions());
    while ( ! ( hTr.isEmpty())) 	//while not empty
	{
		os << hTr.first() << "\n";	//copy each transaction (and <Return>) in file
	    hTr.deleteFirst();	//delete transaction from list of transactions
	}
	return os;
}

//istream - input to the program, reads from a file
istream& operator>>( istream& is, CurrentAccount& aCurrentAccount) {
						//read in BankAccount details
    is >> aCurrentAccount.type;	//read account type
    is >> aCurrentAccount.accountNumber;	//read account number
	is >> aCurrentAccount.sortCode;	//read sort code
    is >> aCurrentAccount.overdraftLimit; //read overdraft limit
 	is >> aCurrentAccount.creationDate;	//read creation date
	is >> aCurrentAccount.balance;	//read balance
	Transaction aTransaction;
	is >> aTransaction;	//read first transaction
    while ( is != 0) 	//while not end of file…
    {
        aCurrentAccount.transactions.addAtEnd( aTransaction);   //add transaction to history of transactions
        is >> aTransaction;	//read in next transaction
	}
	return is;
}


The errors highlighted in CurrentAccount.cpp are all similar to the first one:

"'BankAccount::type' is not accessible"

All caused by the istream method in the CurrentAcount class.

What am I doing wrong that means the function can't access them?
mik0rs is offline   Reply With Quote
Old 04-05-2003, 08:22 PM   #2 (permalink)
palin
Code Monkey
 
palin's Avatar
 
Join Date: Jan 2003
Posts: 57
palin is on a distinguished road
You need the friend operator before the declaration and implementation of the ostream overloaded operators.
palin is offline   Reply With Quote
Old 04-06-2003, 04:57 AM   #3 (permalink)
mik0rs
Registered User
 
Join Date: Apr 2003
Posts: 2
mik0rs is on a distinguished road
I doubt it's that, as it's lifted directly from the base class, which was provided to me, and it compiles fine when there's only the base class
mik0rs is offline   Reply With Quote
Old 04-08-2003, 05:49 PM   #4 (permalink)
saline
I am red.
 
saline's Avatar
 
Join Date: Feb 2003
Location: Cleveland, OH
Posts: 139
saline is on a distinguished road
hmmmmm

It seems like the base class is supposed to be abstract, shouldn't the abstract functions then be set to zero? At least thats how I learned it.

as flanders might say "well, this is a mellon scratcher"
saline is offline   Reply With Quote
Old 04-08-2003, 07:53 PM   #5 (permalink)
palin
Code Monkey
 
palin's Avatar
 
Join Date: Jan 2003
Posts: 57
palin is on a distinguished road
The base class doesn't need to be abstract there is something weird going on I just can't put my finger on it
palin is offline   Reply With Quote
Old 04-08-2003, 11:54 PM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Seems to me that you have more than one error message.
Can you publish the project files to a webspace, so they can be downloaded?

You are using "Date_" but you commented out date.h.
You are using string but I see no string.h.
And so forth.
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 03:03 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting