SDSU CS 596 OODP
Object Interaction Examples

[To Lecture Notes Index]
San Diego State University -- This page last updated Oct. 2, 1995
----------


Object Interaction - Code Examples

#include <iostream.h>	

class BankAccount 
{	
	public :		
		void 	deposit(float amount);
		void	withdrawl(float amount);
		float	getBalance() const;
		
		BankAccount(float amount = 0.0);
		
	private:
		float balance;
};

BankAccount::BankAccount(float amount)
{
	cout << "New Bank Account" << endl;
	balance = amount;
}

void BankAccount::deposit(float amount)  {	
	balance =  balance + amount;
}


void BankAccount::withdrawl(float amount) {	
	balance =  balance - amount;
}

float BankAccount::getBalance() const
{	
	return balance;
}

Object Inside Object
class Customer {
	public:
		Customer(char* Name, float Amount);
		float CheckingBalance();
	private:
		char* LastName;
		BankAccount Checking;
};

Customer::Customer(char* Name, float Amount)  {	
	cout << "New Customer" << endl;
	LastName = Name;
	BankAccount LocalCopy(Amount);
	Checking = LocalCopy;
}

float Customer::CheckingBalance() {
	return Checking.getBalance();
}

void main() {
	Customer Test("Whitney", 1000 );
	cout << Test.CheckingBalance();
}
Output
New Bank Account New Customer New Bank Account 1000

Object Pointer Inside Object
class Customer {
	public:
		Customer(char* Name, float Amount);
		float CheckingBalance();
	private:
		char* LastName;
		BankAccount* Checking;
};

Customer::Customer(char* Name, float Amount)
{	
	cout << "New Customer" << endl;
	LastName = Name;
	Checking = new BankAccount(Amount);
}

float Customer::CheckingBalance() {
	return Checking->getBalance();
}

void main() {
	Customer Test("Whitney", 1000 );
	cout << Test.CheckingBalance();
}
Output
New Customer New Bank Account 1000

Object Inside Object done Better


class Customer {
	public:
		Customer(char* Name, float Amount);
		float CheckingBalance();
	private:
		char* LastName;
		BankAccount Checking;
};

Customer::Customer(char* Name, float Amount) : Checking(Amount) {
	cout << "New Customer" << endl;
	LastName = Name;
}

float Customer::CheckingBalance() {
	return Checking.getBalance();
}

void main(){
	Customer Test("Whitney", 1000 );
	cout << Test.CheckingBalance();
}
Output
New Bank Account New Customer 1000

Object Points to Object

class Customer {
	public:
		Customer(char* Name, BankAccount& CustomersAccount);
		float CheckingBalance();
	private:
		char* LastName;
		BankAccount* Checking;
};

Customer::Customer(char* Name, BankAccount& CustomersAccount)
{	
		cout << "New Customer" << endl;
		LastName = Name;
		Checking = &CustomersAccount;
}

float Customer::CheckingBalance() {
		return Checking->getBalance();
}

void main()
{
	BankAccount TestsAccount( 1000 );
	Customer Test( "Whitney", TestsAccount );
	cout << Test.CheckingBalance();
}
Output
New Bank Account New Customer 1000

Object Points to Object Points to Object


#include <iostream.h>	

class Customer;

class BankAccount 
{	public :		
		void 	deposit(float amount);
		void	withdrawl(float amount);
		float	getBalance() const;
		
		void 	setOwner( Customer& Owner);
		char* 	name();
		BankAccount(float amount = 0.0);
		
	private:
		float balance;
		Customer* AccountOwner;
};

class Customer {
	public:
		Customer(char* Name, BankAccount& CustomersAccount);
		float CheckingBalance();
		char* name();
	private:
		char* LastName;
		BankAccount* Checking;
};
void 	BankAccount::setOwner( Customer& Owner) {
	AccountOwner = &Owner;
}

char* 	BankAccount::name() {
	return AccountOwner->name();
}
	
char* Customer::name() {
	return LastName;
}

Customer::Customer(char* Name, BankAccount& CustomersAccount)
{	
	cout << "New Customer" << endl;
	LastName = Name;
	Checking = &CustomersAccount;
	Checking->setOwner( *this );
}

float Customer::CheckingBalance() {
	return Checking->getBalance();
}

void main()
{
	BankAccount TestsAccount( 1000 );
	Customer Test( "Whitney", TestsAccount );
	cout << Test.CheckingBalance() << "\t"
		<< TestsAccount.name() << endl;
}

Output
New Bank Account New Customer 1000 Whitney
----------