Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,319 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,101 people online right now. Registration is fast and FREE... Join Now!




virtual function and inheritance

 
Reply to this topicStart new topic

virtual function and inheritance, This keeps given me all kinds of errors, can someone pls show me where

chuboy
post 13 Mar, 2008 - 12:18 AM
Post #1


New D.I.C Head

*
Joined: 11 Mar, 2008
Posts: 2

// BankAccount.cpp : main project file.

#include "stdafx.h"

//using namespace System;

//*************************************************************
//
// Accounts.cpp
//
// This program contains a BankAccount class and two derived
// classes - MoneyMarketAccount and CDAccount - and a function
// for transferring from one account to another. The three
// classes have different rules for withdrawing funds; hence,
// the withdraw function is virtual. The main function tests
// the classes and the transfer function.
//
//*************************************************************

#include <iostream>
#include <string>

using namespace std;

//
// Constants to indicate status after withdrawal attempt
//
const int OK = 0;
const int INSUFFICIENT_FUNDS = -1;

//=================================
// BankAccount Class Declaration
//=================================
class BankAccount
{
public:
BankAccount(string name, double balance);
// Sets up a BankAccount object with the given name and balance

string getName() const;
// Returns the name on the BankAccount

double getBalance() const;
// Returns the balance on the BankAccount

void deposit(double amt);
// Precondition: amt is a nonnegative number
// Postcondition: amt has been added to the account balance

virtual int withdraw(double amt);
// Precondition: amt is a nonnegative number
// Postcondition: amt has been subtracted from the balance if
// there were sufficient funds (balance >= amt)
// Returns 0 (OK) if the withdrawal was completed; -1 (INSUFFICIENT_FUNDS)
// if there were insufficient funds.

protected:
string acctName;
double acctBalance;
};


//================================
// MoneyMarketClass Declaration
//================================
class MoneyMarketAccount: public BankAccount
{
public:
MoneyMarketAccount(string name, double balance);
// Sets up a MoneyMarketAccount with the given name and balance

virtual int withdraw(double amt);
// Precondition: amt is a nonnegative integer
// Postcondition: If numWithdrawals < FREE_WITHDRAWALS and
// amt <= acctBalance, amt is subtracted from acctBalance and
// numWithdrawals is incremented; if numWithdrawals >= FREE_WITHDRAWALS
// and amt + WITHDRAWAL_FEE <= acctBalance, amt + WITHDRAWAL_FEE is
// subtracted from acctBalance.
// Returns 0 (OK) if either of the above occurred; otherwise returns
// INSUFFICIENT_FUNDS (-1).

int getNumWithdrawals() const;
// Returns the number of withdrawals

private:
int numWithdrawals;
static const int FREE_WITHDRAWALS = 2;
static const double WITHDRAWAL_FEE = 1.50;
};


//==========================
// CDAccount Declaration
//==========================
class CDAccount: public BankAccount
{
public:
CDAccount(string name, double balance, double rate);
// Sets up a CDAccount object with the name and balance and interestRate
// equal to rate/100 (rate converted to decimal form)

virtual int withdraw(double amt);
// Precondition: amt is a nonnegative number
// Postcondition: If amt + the penalty (PENALTY*interestRate*acctBalance)
// is less than or equal to the account balance, the amount and the
// penalty are subtracted from the balance.
// Returns OK (0) if there were sufficient funds for the withdrawal and
// returns INSUFFICENT_FUNDS (-1) otherwise.

private:
double interestRate;
static const double PENALTY = 0.25; //25% of interest
};


void transfer (double amt, BankAccount& fromAcct, BankAccount& toAcct);
// Precondition: amt is a nonnegative number, fromAcct and toAccount
// exist.
// Postcondition: If there are sufficient funds in the fromAccount
// to cover amt plus any charges (withdrawal fee or penalties, depending
// on the type of account), amt plus any charges are subtracted from
// fromAcct and amt is added to toAcct.


//====================
// main function
//====================
int main()
{
string name;
double balance;
double interestRate;
double amount;
int choice;
char moreTransfers;

cout << endl;
cout << "Transferring Money..." << endl;
cout << "First set up three accounts - a basic account, a " << endl;
cout << "Money Market account, and a Certificate of Deposit account." << endl;
cout << endl;
cout << "Enter the name of owner of the accounts: ";
cin >> name;
cout << "Enter the opening balance in the basic account: ";
cin >> balance;
BankAccount basicAcct(name, balance);
cout << "Enter the opening balance in the Money Market Account: ";
cin >> balance;
MoneyMarketAccount moneyMarket(name, balance);
cout << "Enter the opening balance for the CD: ";
cin >> balance;
cout << "Enter the interest rate for the CD (for example, 2 for 2%): ";
cin >> interestRate;
CDAccount CD(name, balance, interestRate);
cout << endl;

do
{
cout << "Choose a direction to transfer funds: " << endl;
cout << "1. From the basic account to the Money Market Account" << endl;
cout << "2. From the basic account to the CD" << endl;
cout << "3. From the Money Market Account to the basic account" << endl;
cout << "4. From the Money Market Account to the CD" << endl;
cout << "5. From the CD to the basic account" << endl;
cout << "6. From the CD to the Money Market Account" << endl;
cout << "Enter the number of your choice: ";
cin >> choice;
cout << endl;

cout << "Enter the amount to transfer: ";
cin >> amount;
cout << endl;

switch (choice)
{
case 1:
transfer(amount, basicAcct, moneyMarket);
break;
case 2:
transfer (amount, basicAcct, CD);
break;
case 3:
transfer (amount, moneyMarket, basicAcct);
break;
case 4:
transfer (amount, moneyMarket, CD);
break;
case 5:
transfer (amount, CD, basicAcct);
break;
case 6:
transfer (amount, CD, moneyMarket);
break;
default:
cout << "Invalid choice." << endl;
}

cout << endl;
cout << "Current Balances: " << endl;
cout << "Basic Account: " << basicAcct.getBalance() << endl;
cout << "Money Market Account: " << moneyMarket.getBalance() << endl;
cout << "CD Account: " << CD.getBalance() << endl;

cout << "Transfer more money? (y/n): ";
cin >> moreTransfers;
cout << endl;
}
while (moreTransfers == 'y' || moreTransfers == 'Y');

return 0;

}


//===========================
// BankAccount Definitions
//===========================

BankAccount::BankAccount(string name, double balance)
{
acctName = name;
acctBalance = balance;
}


string BankAccount::getName() const
{
return acctName;
}


double BankAccount::getBalance() const
{
return acctBalance;
}


void BankAccount::deposit(double amt)
{
if (amt < 0)
{
cout << "Attempt to deposit negative amount - program terminated." << endl;
exit(1);
}

acctBalance += amt;
}


int BankAccount::withdraw(double amt)
{
int status = OK;

if (amt < 0)
{
cout << "Attempt to withdraw negative amount - program terminated." << endl;
exit(1);
}

if (amt <= acctBalance)
acctBalance -= amt;
else
{
cout << "Amount exceeds balance - withdrawal denied." << endl;
status = INSUFFICIENT_FUNDS;
}
return status;
}


//=======================================================================
//Derived MoneyMarketAccount class definition
//======================================================================

MoneyMarketAccount::MoneyMarketAccount(string name, double balance):BankAccount(std::string name, double balance),numWithdrawals(0)
{
// Intentionally left blank
}

int MoneyMarketAccount::withdraw(double amt)
{
int status = OK;

if (amt >= 0.0)
{
if (numWithdrawals <= FREE_WITHDRAWALS) && ((amt + WITHDRAWAL_FEE) <= acctBalance)
{
acctBalance -= (amt + WITHDRAWAL_FEE);
numWithdrawals++;
}
else
{
cout<< "amount exceeds balance"<<endl;
status = INSUFFICIENT_FUNDS;
}
return status;
}
}

int MoneyMarketAccount::getNumWithdrawals() const
{


return numWithdrawals;
}

//============================================================================
//CDAccount class definitions
//============================================================================

CDAccount::CDAccount(string name, double balance, double rate):BankAccount(std::string name, balance), interestRate(rate/100)
{
// Intentionally left blank
}

int CDAccount::withdraw(double amt)
{
int status = OK;
if (amt >= 0)
{
double penalty = PENALTY * interestRate * acctBalance;
if ((amt + penalty) <= acctBalance)
{
acctBalance -= (amt + penalty);

}
else
{
status = INSUFFICIENT_FUNDS;
}

}
return status;
}

//==============================================
//Transfer class definition
//==============================================

void transfer (double amt, BankAccount& fromAcct, BankAccount& toAcct)
{
// Precondition: amt is a nonnegative number, fromAcct and toAccount
// exist.
// Postcondition: If there are sufficient funds in the fromAccount
// to cover amt plus any charges (withdrawal fee or penalties, depending
// on the type of account), amt plus any charges are subtracted from
// fromAcct and amt is added to toAcct.
if (amt >= 0.0) && (fromAcct != null) && (toAcct != null)
{
double money = amt + WITHDRAWAL_FEE + penalty;
if (fromAcct.acctBalance >= money)
{
fromAcct.acctBalance -= money;
toAcct.acctBalance += money;
}

}
}

This post has been edited by chuboy: 13 Mar, 2008 - 12:20 AM
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 13 Mar, 2008 - 12:20 AM
Post #2


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,328



Thanked 57 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


code.gif

Also, can you please post what errors that you are getting...
User is offlineProfile CardPM

Go to the top of the page

chuboy
post 13 Mar, 2008 - 12:24 AM
Post #3


New D.I.C Head

*
Joined: 11 Mar, 2008
Posts: 2

QUOTE(no2pencil @ 13 Mar, 2008 - 01:20 AM) *

code.gif

Also, can you please post what errors that you are getting...



thanks for you prompt reply, the error I am getting are as follows:
------ Build started: Project: BankAccount, Configuration: Debug Win32 ------
Compiling...
BankAccount.cpp
.\BankAccount.cpp(88) : error C2864: 'MoneyMarketAccount::WITHDRAWAL_FEE' : only static const integral data members can be initialized within a class
.\BankAccount.cpp(112) : error C2864: 'CDAccount::PENALTY' : only static const integral data members can be initialized within a class
.\BankAccount.cpp(275) : error C2144: syntax error : 'std::string' should be preceded by ')'
.\BankAccount.cpp(275) : error C2612: trailing 'type' illegal in base/member initializer list
.\BankAccount.cpp(275) : error C2512: 'BankAccount' : no appropriate default constructor available
.\BankAccount.cpp(275) : error C2082: redefinition of formal parameter 'name'
.\BankAccount.cpp(275) : error C2062: type 'double' unexpected
.\BankAccount.cpp(275) : error C2059: syntax error : ')'
.\BankAccount.cpp(276) : error C2143: syntax error : missing ';' before '{'
.\BankAccount.cpp(281) : error C2601: 'MoneyMarketAccount::withdraw' : local function definitions are illegal
.\BankAccount.cpp(301) : error C2601: 'MoneyMarketAccount::getNumWithdrawals' : local function definitions are illegal
.\BankAccount.cpp(311) : error C2275: 'std::string' : illegal use of this type as an expression
C:\Program Files\Microsoft Visual Studio 8\VC\include\xstring(2128) : see declaration of 'std::string'
.\BankAccount.cpp(311) : error C2146: syntax error : missing ')' before identifier 'name'
.\BankAccount.cpp(311) : error C2059: syntax error : ')'
.\BankAccount.cpp(312) : error C2143: syntax error : missing ';' before '{'
.\BankAccount.cpp(317) : error C2601: 'CDAccount::withdraw' : local function definitions are illegal
.\BankAccount.cpp(341) : error C2601: 'transfer' : local function definitions are illegal
.\BankAccount.cpp(368) : fatal error C1004: unexpected end-of-file found
Build log was saved at "file://c:\Users\John\Documents\Visual Studio 2005\Projects\BankAccount\Debug\BuildLog.htm"
BankAccount - 18 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 02:27AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month