Hello Everyone,
I'm getting close. This code was compile using Dev-5-cpp and it will compile and run under VS-2008. I spend a lot of time piecing this code together and I have a lot of extra work to do in order to finish up.
I only have "one question" but it may take a minute to understand what I'm asking. Compile the program and go to this lines (use/copy paste/find). It's in two places and it's the ONLY question.
// I need an Extra virtual call to ***getBalance*** from the
// CHECKING_A::CHECKING_A. I need it to replace the call
// **checking_A.earnings()** below, which only return interest
// earned. I need the Balance which is $25.00 using virtual
// base-class reference or pointer. Thanks in advance
Thank you
Code:
// Processing derived-class obj ieach and poly using dynamic binding.
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <string.h>
#include <time.h>
#include <ctime>
#include <sstream>
#include <string>
#include <Cstring>
#include<stdlib.h>
#include <iostream>
#include <iomanip>
#include <limits> // numeric_limits needed by VC
#include <vector>
using namespace std; // for iostream
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::setprecision; // for iomanip
using std::fixed;
// ............................................ cannot instantiate objects
// ............................................ ACCOUNT abstract base class
// .................. pure virtual function makes ACCOUNT abstract base class
class ACCOUNT
{
public:
ACCOUNT( const string & );
void set_ID( const string & );
string get_ID() const;
void credit ( double );
bool debit ( double );
void setBalance ( double );
double getBalance();
virtual double earnings() const = 0; // pure virtual
virtual void print() const; // virtual
private:
double balance;
string line_ID;
};
// ............................................ concrete with intent to override
// ............................................ derived from ACCOUNT
class SAVING_A : public ACCOUNT
{
public:
SAVING_A( const string &, double = 0.0, double = 0.0 );
void setInterestRate( double );
double getInterestRate() const;
void setBalance( double );
double getBalance() const;
virtual double earnings() const;
virtual void print() const;
private:
double balance;
double interestRate;
};
// ............................................ concrete with intent to override
// ............................................ derived from ACCOUNT
class saving_B : public ACCOUNT
{
public:
saving_B( const string &, double = 0.0, double = 0.0 );
void setInterestRate( double );
double getInterestRate() const;
void setBalance( double );
double getBalance() const;
virtual double earnings() const;
virtual void print() const;
private:
double balance;
double interestRate;
};
// ............................................ concrete with intent to override
// ............................................ derived from ACCOUNT
class CHECKING_A : public ACCOUNT
{
public:
CHECKING_A( const string &, double = 0.0, double = 0.0 );
void setInterestRate( double );
double getInterestRate() const;
void setBalance( double );
double getBalance() const;
virtual double earnings() const;
virtual void print() const;
private:
double balance;
double interestRate;
};
// ............................................ derive from CommissionACCOUNT
// .............................. keyword virtual signals intent to override
class checking_B : public CHECKING_A
{
public:
checking_B( const string &,
double = 0.0, double = 0.0, double = 0.0 );
void setCompoundInterest( double );
double getCompoundInterest() const;
virtual double earnings() const; // intent 22 override
virtual void print() const; // intent 22 override
private:
double compoundInterest;
};
// Process ACC derived-class objects individually & poly using dynamic binding
//---------------------------------------------------------- constructor
ACCOUNT::ACCOUNT( const string &_id_0 )
: line_ID( _id_0 )
{
}
//---------------------------------------------------------- _id_0
void ACCOUNT::set_ID( const string &_id_0 )
{ line_ID = _id_0; }
string ACCOUNT::get_ID() const
{ return line_ID; }
//--------------------------------------------------------- not pure virtual
void ACCOUNT::print() const //
{
cout << "\n" << get_ID();
}
// Process ACC derived-class objects individually & poly using dynamic binding
//---------------------------------------------------------- constructor
SAVING_A::SAVING_A(const string &_id_0, double initialBalance, double rate)
: ACCOUNT( _id_0 )
{ setBalance( initialBalance );
setInterestRate( rate ); }
//---------------------------------------------------------- InterestRate
void SAVING_A::setInterestRate( double rate )
{ interestRate = ( ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0 ); }
double SAVING_A::getInterestRate() const
{ return interestRate; }
//---------------------------------------------------------- Balance
void SAVING_A::setBalance( double initialBalance )
{ balance = ( ( initialBalance < 0.0 ) ? 0.0 : initialBalance ); }
double SAVING_A::getBalance() const
{ return balance; }
//--------------------------------------------- override pure virtual in ACCOUNT
double SAVING_A::earnings() const
{ return getInterestRate() * getBalance(); }
//----------------------------------------------- reuse E abstract base-class
void SAVING_A::print() const
{ cout << "SAVING_A: ";
ACCOUNT::print();
cout << "\nBalance: " << getBalance()
<< "; iRate: " << getInterestRate(); }
// Process ACC derived-class objects individually & poly using dynamic binding
//---------------------------------------------------------- constructor
saving_B::saving_B(const string &_id_0, double initialBalance, double rate)
: ACCOUNT( _id_0 )
{ setBalance( initialBalance );
setInterestRate( rate ); }
//---------------------------------------------------------- InterestRate
void saving_B::setInterestRate( double rate )
{ interestRate = ( ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0 ); }
double saving_B::getInterestRate() const
{ return interestRate; }
//---------------------------------------------------------- Balance
void saving_B::setBalance( double initialBalance )
{ balance = ( ( initialBalance < 0.0 ) ? 0.0 : initialBalance ); }
double saving_B::getBalance() const
{ return balance; }
//--------------------------------------------- override pure virtual in ACCOUNT
double saving_B::earnings() const
{ return getInterestRate() * getBalance(); }
//----------------------------------------------- reuse E abstract base-class
void saving_B::print() const
{ cout << "saving_B: ";
ACCOUNT::print();
cout << "\nBalance: " << getBalance()
<< "; iRate: " << getInterestRate(); }
//---------------------------------------------------------- constructor
CHECKING_A::CHECKING_A(const string &_id_0, double initialBalance, double rate)
: ACCOUNT( _id_0 )
{ setBalance( initialBalance );
setInterestRate( rate ); }
//---------------------------------------------------------- InterestRate
void CHECKING_A::setInterestRate( double rate )
{ interestRate = ( ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0 ); }
double CHECKING_A::getInterestRate() const
{ return interestRate; }
//---------------------------------------------------------- Balance
void CHECKING_A::setBalance( double initialBalance )
{ balance = ( ( initialBalance < 0.0 ) ? 0.0 : initialBalance ); }
double CHECKING_A::getBalance() const
{ return balance; }
//--------------------------------------------- override pure virtual in ACCOUNT
double CHECKING_A::earnings() const
{ return getInterestRate() * getBalance(); }
//----------------------------------------------- reuse E abstract base-class
void CHECKING_A::print() const
{ cout << "CHECKING_A: ";
ACCOUNT::print();
cout << "\nBalance: " << getBalance()
<< "; iRate: " << getInterestRate(); }
//---------------------------------------------------------- constructor
checking_B::checking_B( const string &_id_0, double initialBalance,
double rate, double BALANCE )
: CHECKING_A( _id_0, initialBalance, rate )
{ setCompoundInterest( BALANCE ); }
//---------------------------------------------------------- CompoundInterest
void checking_B::setCompoundInterest( double BALANCE )
{ compoundInterest = ( ( BALANCE < 0.0 ) ? 0.0 : BALANCE ); }
double checking_B::getCompoundInterest() const
{ return compoundInterest; }
//----------------------------------------------- override irtual in CHECKING_A
double checking_B::earnings() const
{ return getBalance() + CHECKING_A::earnings(); }
//----------------------------------------------- reuse C abstract base-class
void checking_B::print() const
{ // cout checking_B is OVER-RIDED
CHECKING_A::print();
// cout << "; base BALANCE: " << getCompoundInterest();
}
void virtualViaPointer_1( const ACCOUNT * const ); // prototype
void virtualViaReference_1( const ACCOUNT & ); // prototype
int main()
{
int product;
int quantity;
double product1 = 23.84;
double product2 = 13.5;
double total = 0;
/////////////////////////////
///////////////////////////// step - 1
/////////////////////////////
cout
<< "\nProduct 1 = $ " << product1
<< "\nProduct 2 = $ " << fixed << setprecision( 2) << product2
<< endl << endl;
total = total + ( product1 + product2);
cout << total << endl << endl << endl;
total= 0;
cout << fixed << setprecision( 2 );
// create derived-class objects
SAVING_A saving_A( "SAVING_A:", 10500, .06 );
saving_B saving_b( "saving_B:", 10500, .06 );
CHECKING_A checking_A( "CHECKING_A:", 25, .06 );
checking_B checking_b( "checking_B:", 25, .06 ); // , 300
// .........................................................................
// .........................................................................
cout << " \n\n";
cout << " processed using static binding:\n\n";
saving_A.print();
cout << "\nearned $" << saving_A.earnings() << "\n\n";
saving_b.print();
cout << "\nearned $" << saving_b.earnings() << "\n\n";
checking_A.print();
cout << "\nearned instrest $" << checking_A.earnings() << "\n\n";
checking_b.print();
cout << "\nearned combined $" << checking_b.earnings()
<< "\n\n";
// I need an Extra virtual call to ***getBalance*** from the
// CHECKING_A::CHECKING_A. I need it to replace the call
// **checking_A.earnings()** which only return instrest.
// I need the Balance which is $25.00 . By virtual base-class
// reference or pointer. ...... Thanks in advance
checking_A.print();
cout << "\nearned instrest $" << checking_A.earnings() << "\n\n";
cout << endl << endl;
// .........................................................................
// .........................................................................
vector < ACCOUNT * > accounts( 4 ); // create vector of 4 base-class pointers
accounts[ 0 ] = &saving_A; // initialize vector with accounts
accounts[ 1 ] = &saving_b;
accounts[ 2 ] = &checking_A;
accounts[ 3 ] = &checking_b;
// .........................................................................
// .........................................................................
cout << " dyn-binding > base-class POINTERS:\n\n";
for ( size_t i = 0; i < accounts.size(); i++ )
virtualViaPointer_1( accounts[ i ] );
// .........................................................................
// .........................................................................
cout << " virtual -- base-class references :\n\n";
for ( size_t i = 0; i < accounts.size(); i++ )
virtualViaReference_1( *accounts[ i ] ); // note dereferencing
/////////////////////////////
///////////////////////////// step - 2
/////////////////////////////
// THIS IS THE GOAL by means given here and above...
// I need an Extra virtual call to ***getBalance*** from the
// CHECKING_A::CHECKING_A. I need it to replace the call
// **checking_A.earnings()** which only return instrest.
// I need the Balance which is $25.00 . By virtual base-class
// reference or pointer. ...... Thanks in advance
cout << "Account 1 balance: $25.00 : " << checking_A.earnings() << endl;
cout << "Enter an amount to withdraw from Account 1: ";
while (!(cin >> product))
{
cout << "\nPlease enter ( 1 - 5 ) or to EXIT type -1\n\n" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Enter product number 1 - 5 : ";
}
/////////////////////////////
///////////////////////////// step - 3
/////////////////////////////
while ( product != -1 )
{
switch ( product )
{
case 1:
cout << "\n\nEnter quantity sold1 1 : ";
cin >> quantity;
cout << endl;
total = total + ( product1 * quantity );
break;
case 2:
cout << "\n\nEnter quantity sold 2 : ";
cin >> quantity;
cout << endl;
total = total + ( product2 * quantity );
break;
case 3:
cout << "\n\nEnter quantity sold 3 : ";
cin >> quantity;
cout << endl;
total = total + ( product2 * quantity );
break;
case 4:
cout << "\n\nEnter quantity sold 4 : ";
cin >> quantity;
cout << endl;
total = total + ( product2 * quantity );
break;
case 5:
cout << "\n\nEnter quantity sold 5 : ";
cin >> quantity;
cout << endl;
total = total + ( product2 * quantity );
break;
case '\n':
case '\t':
case ' ':
break;
default: // catch any other characters
cout << "\nPlease enter ( 1 - 5 ) or to EXIT type -1\n\n" << endl;
break;
}
/////////////////////////////
///////////////////////////// step - 4
///////////////////////////// Pick up from "quanity sold" == ERROR correction
cout << "Enter product number 1 - 5 : ";
while (!(cin >> product))
{
cout << "Incorrect entry: Try again\n" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Enter product number 1 - 5 : ";
} //endWHILE
/////////////////////////////
///////////////////////////// step - 5
/////////////////////////////
}
cout << "\n\n\nT O T A L : $" << fixed << setprecision( 2 )
<< total << endl<< endl<< endl;
cout << endl;
cout << endl;
cout << endl;
system("pause");
system ("CLS");
}
///////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
// base-class pointer using dynamic binding
void virtualViaPointer_1( const ACCOUNT * const baseClassPtr )
{
baseClassPtr->print();
cout << "\n earned $" << baseClassPtr->earnings() << "\n\n";
}
///////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
// base-class reference using dynamic binding
void virtualViaReference_1( const ACCOUNT &baseClassRef )
{
baseClassRef.print();
cout << "\n earned $" << baseClassRef.earnings() << "\n\n";
}
///////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
// base-class pointer using dynamic binding
void virtualViaPointer2( const ACCOUNT * const baseClassPtr )
{
baseClassPtr->print();
// cout << "\n combined $" << baseClassPtr->combine() << "\n\n";
}
///////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
// base-class reference using dynamic binding
void virtualViaReference2( const ACCOUNT &baseClassRef )
{
baseClassRef.print();
// cout << "\n combined $" << baseClassRef.combine() << "\n\n";
}