4 Replies - 564 Views - Last Post: 05 August 2012 - 06:09 PM Rate Topic: -----

#1 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Return Bool in a Derived Class , problem redefining functions

Posted 05 August 2012 - 12:40 PM

Hello , I having a problem with the last part of my code. I want to redifine the debit function in the derived class checking out to return a bool value to say if any money was taken out. The problem is that I dont know how to use the bool value here, do I make a new integer in the redifined function? There are 2 redefined classes in my code, the last one is the one I am having problems with. Thank you.

//class Account 
#include <iostream>
using namespace std;



//accounut
class Account
{
private:
	double balance;
public:
	Account( double bal = 0.0);
	void credit(double amt);
	virtual void debit(double amt);
	double getBalance() const;
};


// Savings Account 
class SavingsAccount : public Account
{
private: 
		double interest;
public:
	SavingsAccount( double bal = 0.0 , double inte = 0.0);
	double calculateInterest(double totalint = 0.0);
};


//  checking account
class checkingAccount : public Account
{
private:
	double fee;
public:
	checkingAccount( double bal = 0.0, double fe = 0.0);
	void credit(double amt);
	virtual void debit(double amt);
};


//formatting needed
typedef std::ios_base::fmtflags format;
typedef std::streamsize precis;
format setFormat();
void restore(format f, precis p);


// constructor

Account::Account(double bal)
{
	using namespace std;
	;
	if (bal < 0)
	{
		cout << "Balance cannot be negative";
		balance = 0;
	}
	else 
		balance = bal;

}

void Account::credit(double amt)
{
	if (amt < 0)
		cout << "Negative Deposit now alloed;"
		<< "deposit cancelled.\n";
	else 
		balance += amt;
}

void Account::debit(double amt)
{
	format initialState = setFormat();
	precis prec = cout.precision(2);

	if (amt < 0)
		cout << "You cannot withdraw negative;"
		<< "your transaction has ended.\n";
	else if (amt <= balance)
	balance -= amt;
	else 
		cout << "Debit amount exceed account balance"
		<< "This transaction is over.\n";
		restore(initialState, prec);
}

double Account::getBalance() const
{
	return balance;
}

//contructor for savingsaccount
SavingsAccount::SavingsAccount(double bal, double inte ): Account(bal)
{
	interest = inte;
}


double SavingsAccount::calculateInterest(double totalinte)
{
	double bal = getBalance();
	totalinte = interest * bal;
	cout << " Yout total interest will be :" << totalinte << endl;
}


// constuctor for checking account

checkingAccount::checkingAccount(double bal,double fe)
{
	fee=fe;
}

//redefining 


void checkingAccount::debit(double amt);
{

	double bal = getBalance();
	if (amt = 0.0)
		Account::debit(amt);
	else 
	{

void checkingAccount::credit(double amt);// redefine credit function not yet started
{


}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Return Bool in a Derived Class , problem redefining functions

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: Return Bool in a Derived Class , problem redefining functions

Posted 05 August 2012 - 01:05 PM

Why can't you let debit return a boolean in your base class?
Was This Post Helpful? 0
  • +
  • -

#3 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Re: Return Bool in a Derived Class , problem redefining functions

Posted 05 August 2012 - 02:04 PM

View PostCTphpnwb, on 05 August 2012 - 01:05 PM, said:

Why can't you let debit return a boolean in your base class?


Because debit works differently in the Base Class? How could I do this?

This post has been edited by osu1: 05 August 2012 - 02:05 PM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: Return Bool in a Derived Class , problem redefining functions

Posted 05 August 2012 - 02:44 PM

It's not being used at all in the base class. That's why it's virtual, no?

You could do something like this:
#include <iostream>
using namespace std;

class Account {
protected:
  double balance;
public:
  Account() {
    balance = 0;
  }
  void debit(double amt);
  double getBalance();
};

class checking_acc : Account {
public:
  bool debit(double amt) {
    if (balance >= amt) {
      balance -= amt;
      return true;
    }
    return false;
  }
  
  void deposit(double amt) {
    if (amt > 0) {
      balance += amt;
    }
  }
};

int main(int argc, const char * argv[])
{
  checking_acc test;
  
  if (test.debit(22)) {
    cout <<"Debit successful"<< endl;
  } else {
    cout << "Debit unsuccessful" << endl;
  }
  
  test.deposit(50);
  
  if (test.debit(22)) {
    cout <<"Debit successful"<< endl;
  } else {
    cout << "Debit unsuccessful" << endl;
  }

  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Re: Return Bool in a Derived Class , problem redefining functions

Posted 05 August 2012 - 06:09 PM

View PostCTphpnwb, on 05 August 2012 - 02:44 PM, said:

It's not being used at all in the base class. That's why it's virtual, no?

You could do something like this:
#include <iostream>
using namespace std;

class Account {
protected:
  double balance;
public:
  Account() {
    balance = 0;
  }
  void debit(double amt);
  double getBalance();
};

class checking_acc : Account {
public:
  bool debit(double amt) {
    if (balance >= amt) {
      balance -= amt;
      return true;
    }
    return false;
  }
  
  void deposit(double amt) {
    if (amt > 0) {
      balance += amt;
    }
  }
};

int main(int argc, const char * argv[])
{
  checking_acc test;
  
  if (test.debit(22)) {
    cout <<"Debit successful"<< endl;
  } else {
    cout << "Debit unsuccessful" << endl;
  }
  
  test.deposit(50);
  
  if (test.debit(22)) {
    cout <<"Debit successful"<< endl;
  } else {
    cout << "Debit unsuccessful" << endl;
  }

  return 0;
}




Thank you I will try to implement this into my code and see if I can continue with it. Can I reply again incase I have still have trouble? I am going to struggle more with it. Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1