程序没有逐步完成正确的虚函数。使用虚函数,动态转换和指针时遇到麻烦

时间:2014-05-10 20:25:11

标签: c++ pointers casting operator-overloading virtual-functions

我有一个名为CashPoint的类,它读取文件名并确定它是银行帐户还是当前帐户。在一个案例中创建当前帐户的实例,并将其传递到指向银行帐户的指针,因为当前帐户类继承自银行帐户。指针读入文件的属性,例如balance,accno,transactions,overdraft。这适用于所有但透支,因为虚拟函数getDataFromStream始终从BankAccount而不是CurrentAccount中选择。如何操作程序朝向CurrentAccount虚拟函数?

CashPoint.cpp(案例= 1)

BankAccount* CashPoint::activateBankAccount(  const string& aBAFileName) {
//check the type of the account (already checked for validity)
int accType( checkAccountType( aBAFileName));
//effectively create the active bank account instance of the appropriate class
//& store the appropriate data read from the file
BankAccount* p_BA( nullptr);
switch( accType)
{
    case 0: 
        cout << "\n-------BANK-------\n";
        p_BA = new BankAccount;    //points to a BankAccount object
        p_BA->readInBankAccountFromFile( aBAFileName); //read account details from file
        break;

    **case 1:** 
        cout << "\n-------CURRENT-------\n";
        p_BA = new CurrentAccount;    //points to a CurrentAccount object
        CurrentAccount* p_CA( dynamic_cast<CurrentAccount*>( p_BA));
        p_BA->**readInBankAccountFromFile**( aBAFileName); //read account details from file
        break;
}
//use dynamic memory allocation: the bank account created will have to be released in releaseBankAccount
return p_BA;

}

BankAccount.cpp

void BankAccount::readInBankAccountFromFile( const string& fileName) {
ifstream fromFile;
fromFile.open( fileName.c_str(), ios::in);  //open file in read mode
if ( fromFile.fail())
    cout << "\nAN ERROR HAS OCCURED WHEN OPENING THE FILE.";
else
    fromFile **>>** (*this);    //read  all info from bank account file
fromFile.close();           //close file: optional here
}

BankAccount.cpp中重载的运算符

istream& operator>>( istream& is, BankAccount& aBankAccount) {
//get BankAccount details from stream
return ( aBankAccount.**getDataFromStream**( is));
}

BankAccount虚拟功能

istream& BankAccount::getDataFromStream( istream& is) {
//get BankAccount details from stream
is >> accountType_;                     //get account type
is >> accountNumber_;                   //get account number
is >> sortCode_;                        //get sort code
is >> creationDate_;                    //get creation date
is >> balance_;                         //get balance_
is >> transactions_;                    //get all transactions (if   any)
return is;
}

CurrentAccount虚函数(本例中需要)

istream& **CurrentAccount::getDataFromStream**( istream& is) {
//get BankAccount details from stream   
BankAccount::getDataFromStream(is);
is >> overdraftLimit_;
return is;
}

BankAccount构造函数

BankAccount::BankAccount()
: accountNumber_( "null"),
  sortCode_( "null"),
  balance_( 0.0)
{}
BankAccount::BankAccount( const string& typ, const string& acctNum, const string& sCode,
                      const Date& cD, double b, const TransactionList& trList )
: accountType_(typ),
  accountNumber_( acctNum), sortCode_( sCode),
  creationDate_( cD),
  balance_( b),
  transactions_( trList)
{}
BankAccount::~BankAccount()
{}

CurrentAccount.cpp

#include "CurrentAccount.h"
CurrentAccount::CurrentAccount()
:BankAccount()
{}
CurrentAccount::CurrentAccount(const string& typ, const string& acctNum, const string& sCode,
                      const Date& cD, double b, const TransactionList& trList, double oL) 
: BankAccount(typ, acctNum, sCode, cD, b, trList), overdraftLimit_(oL)
{}  

double CurrentAccount::getOverdraftLimit() const {
return(overdraftLimit_);
}

bool CurrentAccount::canWithdraw(double a) const {
cout << overdraftLimit_;
return (getBalance() + overdraftLimit_ >= a);
}

const string CurrentAccount::prepareFormattedStatement() const {
ostringstream os;
os << BankAccount::prepareFormattedStatement();
os << "\nOverdraft Limit: " << (char)156 << overdraftLimit_;
return(os.str());
}

ostream& CurrentAccount::putDataInStream( ostream& os) const {
//put (unformatted) BankAccount details in stream
BankAccount::putDataInStream(os);
os << overdraftLimit_ <<"\n";               
return os;
}
istream& CurrentAccount::getDataFromStream( istream& is) {
//get BankAccount details from stream   
BankAccount::getDataFromStream(is);
is >> overdraftLimit_;
return is;
}

0 个答案:

没有答案