Monday, August 2, 2010

Class Object Example

Here is the header file BankAccount.h

#pragma once
#include <string>
using namespace std;
class BankAccount
{
/************************
* this class will take in an initial balance
* and then allow the user to add deposits
* or make withdrawls
* Steve conger 8/2/2010
*******************************/
public:
BankAccount(void);
BankAccount(double, string account, string name);
~BankAccount(void);
void SetInitialBalance(double);
double GetBalance();
void SetAccount(string);
string GetAccountNumber();
void SetCustomerName(string);
string GetCustomerName();

void MakeDeposit(double);
int MakeWithdrawl(double);

private:
string accountNumber;
string customerName;
double balance;
};

Here is the BankAccount.cpp

#include "BankAccount.h"

BankAccount::BankAccount(void)
{
}

BankAccount::BankAccount(double initialBal, string account, string name)
{
SetInitialBalance(initialBal);
SetAccount(account);
SetCustomerName(name);
}

BankAccount::~BankAccount(void)
{
}

void BankAccount::SetInitialBalance(double initialBalance)
{
balance=initialBalance;
}

double BankAccount::GetBalance()
{
return balance;
}

void BankAccount::SetAccount(string accNumber)
{
accountNumber=accNumber;
}

string BankAccount::GetAccountNumber()
{
return accountNumber;
}

void BankAccount::SetCustomerName(string custName)
{
customerName=custName;
}

string BankAccount::GetCustomerName()
{
return customerName;
}

void BankAccount::MakeDeposit(double deposit)
{
balance += deposit;
}

int BankAccount::MakeWithdrawl(double withdrawl)
{
int result=0;
if (balance <= withdrawl)
{
balance -= withdrawl;
result=1;

}
return result;
}

Here is the program.cpp file

#include <iostream>
#include "BankAccount.h"
using namespace std;

void PauseIt();

int main()
{
cout << "Enter account Number: ";
string acc;
getline(cin, acc);

cout << endl;

cout << "Enter account Name: ";
string n;
getline(cin, n);

cout << endl;

cout << "Enter initial Balance: ";
double ibal;
cin >> ibal;

cout << endl;

BankAccount bacc(ibal,acc,n);

cout << "make a Deposit ";
double dep;
cin >> dep;

bacc.MakeDeposit(dep);

cout << "make a withdrawl ";
double wd;
cin >> wd;
cout << endl;
int result=bacc.MakeWithdrawl(wd);

if (result ==0)
{
cout << "insufficient funds" << endl;

}

cout<<endl;
cout << bacc.GetAccountNumber()<< ", " <<
bacc.GetCustomerName()<< " Your balance is "
<< bacc.GetBalance() << endl;

PauseIt();
}

void PauseIt()
{
cout << "enter any character and press Enter to exit"
<<endl;
char c;
cin >> c;
}

No comments:

Post a Comment