CLASS ACCOUNT:
// file account.h
// account class definition
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account {
public:
Account( const char *, const char *, int = 0 ); // constructor
~Account(); // destructor
void setBalance(int); // set the accoutn balance to a certain ammount
void deposit(int); // deposit money to account balance
void withdraw(int); // withdraw money from account balance
const char *getFirstName() const; // return first name
const char *getLastName() const; // return last name
void getBalance(); // return account balance
// static member function
static int getAccountNumber(); // return account number
private:
char *firstName;
char *lastName;
int balance;
// static data member
static int accountNumber; // number of accounts instantiated
}; // end class account
CLASS ACCOUNTIMPLEMENTATION:
#endif
// file employee2.cpp
// Member-function definitions for class Account
#include <iostream>
#include <new> // C++ standard new operator
#include <cstring> // strcpy and strlen prototypes
#include "account.h" // account class definition
using namespace std;
// define and initialize static data member
int Account::accountNumber = 0;
// define static member function that returns number of
// account objects instantiated
int Account::getAccountNumber()
{
return accountNumber;
} // end static function getAccountNumber
// constructor dynamically allocates space for
// first and last name and uses strcpy to copy
// first and last names into the object
Account::Account( const char *first, const char *last, int ammount )
{
firstName = new char[ strlen( first ) + 1 ];
strcpy( firstName, first );
lastName = new char[ strlen( last ) + 1 ];
strcpy( lastName, last );
Account::setBalance(ammount);
++accountNumber; // increment static count of account number
cout << "Account constructor for " << firstName
<< ' ' << lastName << " called." << endl;
} // end Eaccount constructor
// destructor deallocates dynamically allocated memory
Account::~Account()
{
cout << "~Account() called for " << firstName
<< ' ' << lastName << endl;
delete [] firstName; // recapture memory
delete [] lastName; // recapture memory
--accountNumber; // decrement static count of account number
} // end destructor ~Account
void Account::setBalance(int ammount)
{
balance =+ ammount;
}
void Account::deposit(int ammount)
{
if(ammount > 0)
balance += ammount; // add balance with ammount
}
void Account::withdraw(int ammount)
{
if(ammount > 0)
balance -= ammount; // substract balance with ammount
}
// return first name of account
const char *Account::getFirstName() const
{
// const before return type prevents client from modifying
// private data; client should copy returned string before
// destructor deletes storage to prevent undefined pointer
return firstName;
} // end function getFirstName
// return last name of account
const char *Account::getLastName() const
{
// const before return type prevents client from modifying
// private data; client should copy returned string before
// destructor deletes storage to prevent undefined pointer
return lastName;
} // end function getLastName
void Account::getBalance()
{
cout << "Your account balance is: " << balance << endl;
} // end function getBalance
MY MAIN:
// file myMain.cpp
// Driver to test class account
#include <iostream>
#include <new> // C++ standard new operator
#include "account.h" // account class definition
using namespace std;
int main()
{
Account susan( "Susan", "Baker", 100 );
susan.withdraw(50);
} // end main[/inlinecode]