Hi Charles,
by using OxygenBasic I learned so many things which I as an autodidact did not understand before. I incorporated your counter method and used constructor again.
This last example deals with overriding methods. It will complete my exploring the basic principles of OOP and I will then try some own simple apps. For the notation I changed the naming a little bit and used camel case most of the time
There are the classes account which is initialized with number and the class savingsAccount which has account and is initialized with number and interest rate.
I created a class giroAccount which has account and is initialized with number and credit limit. It uses a method pay_out which overrides the method of account. I used this method:
' --> reimplementation
method pay_out(double amount)
if amount <= (balance + limit) then
' account.pay_out(amount) ' does not work for statement_of_account
balance -= amount
printl "Payed out: " str(amount) & " Euro"
else
printl "Exceeded Limit!"
end if
end method
Would it be possible to use the method pay_out() of class account in some way? When I try account.pay_out then myGiroAccount.statement_of_account() does not show the correct result. Therefore I used the equivalent statements.
Hopefully it is ok that I append the full code. (many statements were done before). It contains everything I learned about OOP so far and how it can be done with OxygenBasic. So it could be interesting for other people too who would like to start with OOP.
Roland
' Account7.o2bas
$filename "Banking.exe"
'include "$/inc/RTL32.inc"
'include "$/inc/RTL64.inc"
% TryAttachConsole
include "$/inc/console.inc"
SetConsoleTitle "Bank Transactions"
'accessing the base class by specified name
class counter
private static sys counterVal
method constructor()
counterVal++
end method
method destructor()
end method
method count() as int
return counterVal
end method
end class
class account
has counter num 'give a name to the base class counter
'data encapsulation - information hiding
protected ' or private
' Attributes
int accountNumber 'instance variables
double balance
public
' initialize
method constructor(int number)
num.constructor
accountNumber = number
balance = 0.00
end method
method destructor()
num.destructor
end method
' methods for secure access
method getAccountNumber() as int
return accountNumber
end method
method getBalance() as double
return balance
end method
method pay_in(double amount)
balance += amount
printl "Payed in: " str(amount) & " Euro"
end method
method pay_out(double amount)
balance -= amount
printl "Payed out: " str(amount) & " Euro"
end method
method statement_of_account()
printl "Account Number: " & accountNumber & " Balance: " & str(balance,2) & " Euro"
end method
method sameAccountNumber(account *a,*b) as int
sys aa=a.getAccountNumber
sys bb=b.getAccountNumber
if aa=bb then return -1
end method
method transfer(double amount, account *account_A, *account_Z)
account_A.statement_of_account()
account_Z.statement_of_account()
account_A.pay_out(amount)
account_Z.pay_in(amount)
printl "From Account Number: " & account_A.getAccountNumber()
print " transferred " & amount & " Euro"
print " to Account Number: " & account_Z.getAccountNumber() & cr
account_A.statement_of_account()
account_Z.statement_of_account()
end method
end class
' --> Inheritance
class savingsAccount
has account
private
double ratePercent ' additional attribute
public
' constructor of class savingsAccount
method constructor(int number, double interest)
num.constructor()
accountNumber = number
balance = 0.00
ratePercent = interest
end method
method destructor()
num.destructor()
end method
' new methods for accessing attribute ratePercent
method getRatePercent() as double
return ratePercent'
end method
method setRatePercent(double interest)
ratePercent = interest'
end method
' new method
method pay_interest()
double interest_payable = (ratePercent * balance)/100)
printl "Interest payable: " & str(interest_payable,2)
balance += interest_payable ' uses inherited attributes
end method
end class
' --> overriding, reimplementation
class giroAccount
has account
private
double limit
public
method constructor(int number, double bankLine)
num.constructor()
accountNumber = number
limit = abs(bankLine)
end method
method destructor()
num.destructor()
end method
method getLimit() as double
return limit
end method
' method for access, check if arg is positiv
method setLimit(double limit)
limit = abs(limit)
end method
' --> reimplementation
method pay_out(double amount)
if amount <= (balance + limit) then
' account.pay_out(amount) ' does not work for statement_of_account
balance -= amount
printl "Payed out: " str(amount) & " Euro"
else
printl "Exceeded Limit!"
end if
end method
end class
' Access class variable via class name
printl "Number of Accounts: " & account.num.count
new account myAccount(1234567) ' new instance of type account
printl "Account Number: " & myAccount.getAccountNumber() ' read access
print " Balance: " & myAccount.getBalance() & " Euro"
printl "Number of Accounts: " & account.num.count
new account yourAccount(2357666) ' new instance of type account
yourAccount.statement_of_account()
printl "Number of Accounts: " & account.num.count
' Assigning (coupling by reference)
account ourAccount at @yourAccount
'equiv: account *ourAccount : @ourAccount = @yourAccount
ourAccount.statement_of_account()
printl "Number of Accounts: " & account.num.count
printl
if account.sameAccountNumber(myAccount, yourAccount) then
printl "My account and your account are identical."
else
printl "My account and your account are different."
end if
if account.sameAccountNumber(ourAccount, yourAccount) then
printl "Our account and your account are identical."
else
printl "Our account and your account are different."
end if
printl
myAccount.pay_in(2000.00)
myAccount.statement_of_account()
printl
yourAccount.pay_in(100.00)
yourAccount.statement_of_account()
printl
ourAccount.pay_in(200.00)
ourAccount.statement_of_account()
printl
'transfer some money
account.transfer(30.00, myAccount, yourAccount)
printl
' Inheritance
new savingsAccount mySavingsAccount(4444990, 5.2)
'each instance of class savingsAccount has 3 attributes
printl "Account Number: " & mySavingsAccount.getAccountNumber() 'inherited attribute
print " Balance: " & mySavingsAccount.getBalance() &" Euro"' 'inherited attribute
printl "Rate Percent: " & str(mySavingsAccount.getRatePercent(),2)' ' new declared attribute
printl "Number of Accounts: " & account.num.count
'method pay_in(amount), pay_out(amount) and statement_of_account() can be used
mySavingsAccount.pay_in(250.00) 'inherited methods
mySavingsAccount.statement_of_account()'
mySavingsAccount.pay_out(10.00)'
mySavingsAccount.statement_of_account()
'instances of class savingsAccount can use the method pay_interest()
mySavingsAccount.pay_interest()' ' new method
mySavingsAccount.statement_of_account()'
printl
' Reimplementation
myAccount.pay_out(1700.00) ' method pay_out(amout) of class account
myAccount.statement_of_account()
printl
printl "Trying to draw 5000 Euro."
myAccount.pay_out(5000.00) ' method pay_out(amout) of class account
myAccount.statement_of_account()
printl
new giroAccount myGiroAccount(1733065,100)
myGiroAccount.statement_of_account()
printl "Bank Line = " & myGiroAccount.getLimit()
printl "Number of Accounts: " & account.num.count
myGiroAccount.pay_in(400.00)
myGiroAccount.statement_of_account()
printl "Trying to draw 450 Euro."
myGiroAccount.pay_out(450.00) ' method pay_out(amout) of class giroAccount
myGiroAccount.statement_of_account()
printl "Trying to draw 350 Euro."
myGiroAccount.pay_out(350.00) ' method pay_out(amout) of class giroAccount
myGiroAccount.statement_of_account()
printl cr & "Hit Enter ... " : waitkey
del myAccount
del yourAccount 'alias ourAccount
del mySavingsAccount
del myGiroAccount
Output:
...
...
Payed out: 1700 Euro
Account Number: 1234567 Balance: 270 Euro
Trying to draw 5000 Euro.
Payed out: 5000 Euro
Account Number: 1234567 Balance: -4730 Euro
Account Number: 1733065 Balance: 0 Euro
Bank Line = 100
Number of Accounts: 4
Payed in: 400 Euro
Account Number: 1733065 Balance: 400 Euro
Trying to draw 450 Euro.
Payed out: 450 Euro
Account Number: 1733065 Balance: -50 Euro
Trying to draw 350 Euro.
Exceeded Limit!
Account Number: 1733065 Balance: -50 Euro
Hit Enter ...