| | |
Need help with an exercise program (probably too easy for you guys)
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2008
Posts: 13
Reputation:
Solved Threads: 0
I need help where it say /* write code to withdrawal money from account1 */.. i think the member function is written right but im not sure.
Im a true begginer taking this online course and I think i have most of it right, can someone please help guide me.. here what i got
[//Lab 2 Account.h
Im a true begginer taking this online course and I think i have most of it right, can someone please help guide me.. here what i got
[//Lab 2 Account.h
C++ Syntax (Toggle Plain Text)
//Definition of the Account class [class Account { public: Account( int ); // constructor initializes balance void credit( int ); // add an amount to the account balance void debit( int ); // subtract an amount to the account balance int getBalance(); // return the account balance private: int balance; // data member that stores the balance }; // end class Account]
C++ Syntax (Toggle Plain Text)
// Lab 2: Account.cpp // Member-function definition for class Account. #include <iostream> using std::cout; using std::endl; #include "Account.h" // include the definition of class Account // Account constructor initializes data member balance Account::Account ( int initialBalance ) { balance = 0; // assume that the balance begins at 0 // if initial value is greater than 0, set this value as the // balance of the Account; otherwise, balance remains 0 if ( initialBalance > 0 ) balance = initialBalance; // if initial balance is negative, print error message if ( initialBalance < 0 ) cout << "Error: Initial balance cannot be negative.\n" << endl; } // end Account constructor // credit (add) an amount to the account balance void Account::credit(int amount) { balance = balance + amount; // add amount to balance } // end function credit // debit (subtract) an amount to the account balance void Account::debit(int amount) { if ( amount > balance ) // if the withdrawal amount is more than // the current balance cout << "Debit \"" << amount << "\" exceeded account balance. \n"; if ( amount <= balance ) balance = balance - amount; } // end function debit // return the account balance int Account::getBalance() { return balance; // gives the value of balance to the calling function } // end function getBalance]
C++ Syntax (Toggle Plain Text)
// Lab 2: AccountTest.cpp // Create and manipulate Account objects #include <iostream> using std::cout; using std::cin; using std::endl; // include definition of class Account from Account.h #include "Account.h" // function main begins program execution int main() { Account account1( 50 ); // create Account object Account account2( 0 ); //create Account object // display initial balance of each object cout << "account1 balance: $" << account1.getBalance() << endl; cout << "account2 balance: $" << account2.getBalance() << endl; int withdrawalAmount; // stores withdrawal amount stored from user cout << "\nEnter withdrawal amount for account1: "; // prompt cin >> withdrawalAmount; // obtain user input cout << "\nsubtracting " << withdrawalAmount << " from account1 balance\n\n"; /* write code to withdrawal money from account1 */ //display balances cout << "account1 balance: $" << account1.getBalance() << endl; cout << "account2 balance: $" << account2.getBalance() << endl; cout << "\nEnter withdrawal amount for account2: "; // prompt cin >> withdrawalAmount; // obtain user input cout << "\nsubtracting " << withdrawalAmount << " from account2 balance\n\n"; /* write code to withdrawal money from account2 */ //display balances cout << "account1 balance: $" << account1.getBalance() << endl; cout << "account2 balance: $" << account2.getBalance() << endl; return 0; // indicate successful termination } // end main
Last edited by Ancient Dragon; Jun 12th, 2008 at 8:08 pm. Reason: add code tags
•
•
Join Date: Jun 2008
Posts: 13
Reputation:
Solved Threads: 0
Sorry about that!! hope this is right
//Lab 2 Account.h
//Definition of the Account class
// Lab 2: Account.cpp
// Member-function definition for class Account.
// Lab 2: AccountTest.cpp
// Create and manipulate Account objects
//Lab 2 Account.h
//Definition of the Account class
C++ Syntax (Toggle Plain Text)
class Account { public: Account( int ); void credit( int ); void debit( int ); int getBalance(); private: int balance; };
// Lab 2: Account.cpp
// Member-function definition for class Account.
C++ Syntax (Toggle Plain Text)
#include <iostream> using std::cout; using std::endl; #include "Account.h" Account::Account ( int initialBalance ) { balance = 0; if ( initialBalance > 0 ) balance = initialBalance; if ( initialBalance < 0 ) cout << "Error: Initial balance cannot be negative.\n" << endl; } void Account::credit(int amount) { balance = balance + amount; } void Account::debit(int amount) { if ( amount > balance ) cout << "Debit \"" << amount << "\" exceeded account balance. \n"; if ( amount <= balance ) balance = balance - amount; } int Account::getBalance() { return balance; }
// Lab 2: AccountTest.cpp
// Create and manipulate Account objects
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "Account.h"
int main()
{
Account account1( 50 );
Account account2( 0 );
cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
int withdrawalAmount;
cout << "\nEnter withdrawal amount for account1: ";
cin >> withdrawalAmount;
cout << "\nsubtracting " << withdrawalAmount
<< " from account1 balance\n\n";
/* write code to withdrawal money from account1 */
cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
cout << "\nEnter withdrawal amount for account2: ";
cin >> withdrawalAmount;
cout << "\nsubtracting " << withdrawalAmount
<< " from account2 balance\n\n";
/* write code to withdrawal money from account2 */
cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
return 0;
} To withdraw money you will need to add another method to the Account class
//Definition of the Account class
[class Account
{
public:
Account( int ); // constructor initializes balance
void credit( int ); // add an amount to the account balance
void debit( int ); // subtract an amount to the account balance
int getBalance(); // return the account balance
void withdraw(int amount);
private:
int balance; // data member that stores the balance
}; // end class Account] Last edited by Ancient Dragon; Jun 12th, 2008 at 8:18 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Other Threads in the C++ Forum
- Previous Thread: error: initial value of reference to non-const must be an lvalue
- Next Thread: I need help with a save function in my game.
Views: 404 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






