BankAccount - if ( howMuch <= balance ) { balance =...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Creating a bankAccount class Instantiating multiple bank accounts
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
remember Classes are defined in the classname.h file Classes have properties (variables) and behaviors (functions) Class functions are written in the classname.cpp file Classes are “instantiated” into objects in main Each object is an independent copy of the class
Background image of page 2
What does a bank account do? class bankClass { public: double balance ; // important data // core behavior: bankClass ( double startingFunds ); // other behaviors void addFunds ( double howMuch ); void subtractFunds ( double howMuch ); double getBalance ( ); };
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Filling in the functions (bankAccount.h) bankClass::bankClass( double startingFunds ) { balance = startingFunds; } void bankClass::addFunds( double howMuch ) { balance = balance + howMuch; } void bankClass::subtractFunds( double howMuch ) { balance = balance - howMuch; } double bankClass::getBalance( ) { return balance; } ;
Background image of page 4
Alternate subtractFunds void bankClass::subtractFunds( double howMuch ) {
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if ( howMuch <= balance ) { balance = balance - howMuch; } else { cout << “Sorry, insufficient funds” << endl; } } // see? The functions can control and protect // all activity Using the bankAccounts in main #include <iostream> #include "bankClass.h" using namespace std; void main () { bankClass mikeB ( 200.00 ); // create an object mikeB . addFunds( 50 ); // use the new object cout << mikeB . balance << endl; // balance is public cout << mikeB . getBalance() << endl; } Core behavior? double balance ; bankClass::bankClass( double startingFunds ) { balance = startingFunds; } bankClass mikeB( 100.00 ); bankAccount.h bankAccount.cpp main.cpp Protecting data class bankClass { private: double balance ; // important data public: bankClass( double startingFunds ); void addFunds ( double howMuch ); void subtractFunds ( double howMuch ); double getBalance ( ); };...
View Full Document

This document was uploaded on 12/05/2011.

Page1 / 8

BankAccount - if ( howMuch <= balance ) { balance =...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online