Hi Charles,
with this example I start using inheritance. When I read about OOP in the past at this stage I lost the way and everything looked like magic then.
I created a class savings_account which inherits from account and which can yield interest. In the Java example this is done using the keyword 'extends'. For OxygenBasic I used 'has'. There is a problem though:
Java extends the constructor of savings_account using: super(number)' uses constructor of superclass account. I tried to replace this with the three variables:
' constructor of class savings_account
method constructor(int number, double interest)
// Java: super(number)' uses constructor of superclass account
count += 1 'not equivalent
account_num = number
' balance = 0.00
rate_percent = interest
end method
but this seems not to be correct. Is there another way to get count, account_num and balance? (rate_percent is a new attribute).
After all I am not sure if constructor/new and destructor/del is necessary at all for this kind of classes? Maybe I should only use a method initialize?
Roland
$filename "Banking.exe"
'include "$/inc/RTL32.inc"
'include "$/inc/RTL64.inc"
% TryAttachConsole
include "$/inc/console.inc"
SetConsoleTitle "Bank Transactions"
class account
static int count 'class variable
'data encapsulation - information hiding
protected ' or private
' Attributes
int account_num 'instance variables
double balance
public
' initialize
method constructor(int number)
count += 1
account_num = number
balance = 0.00
end method
method destructor()
end method
' method for secure access
method get_account_num() as int
return account_num
end method
' method for secure access
method get_balance() 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: " & account_num & " Balance: " & str(balance,2) & " Euro"
end method
method same_account_num(account *a,*b) as int
sys aa=a.get_account_num
sys bb=b.get_account_num
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.get_account_num()
print " transferred " & amount & " Euro"
print " to Account Number: " & account_Z.get_account_num() & cr
account_A.statement_of_account()
account_Z.statement_of_account()
end method
end class
' --> Inheritance
class savings_account
has account
private
double rate_percent' ' additional attribute
public
' constructor of class savings_account
method constructor(int number, double interest)
// Java: super(number)' uses constructor of superclass account
count += 1 'not equivalent
account_num = number
' balance = 0.00
rate_percent = interest
end method
method destructor()
end method
' new methods for accessing attribute rate_percent
method get_rate_percent() as double
return rate_percent'
end method
method set_rate_percent(double interest)
rate_percent = interest'
end method
' new method
method pay_interest()
double interest_payable = (rate_percent * balance)/100)
printl "Interest payable: " & str(interest_payable,2)
balance += interest_payable ' uses inherited attributes
end method
end class
' Access class variable via class name
printl "Number of Accounts: " & account.count
new account my_account(1234567) ' new instance of type account
printl "Account Number: " & my_account.get_account_num() ' read access
print " Balance: " & my_account.get_balance() & " Euro"
printl "Number of Accounts: " & account.count
new account your_account(2357666) ' new instance of type account
your_account.statement_of_account()
printl "Number of Accounts: " & account.count
' Assigning (coupling by reference)
account our_account at @your_account
'equiv: account *our_account : @our_account = @your_account
printl "Account Number: " & our_account.get_account_num() ' read access
print " Balance: " & our_account.get_balance() & " Euro"
our_account.statement_of_account()
printl "Number of Accounts: " & account.count
printl
if account.same_account_num(my_account, your_account) then
printl "My account and your account are identical."
else
printl "My account and your account are different."
end if
if account.same_account_num(our_account, your_account) then
printl "Our account and your account are identical."
else
printl "Our account and your account are different."
end if
my_account.pay_in(2000.00)
my_account.statement_of_account()
printl
your_account.pay_in(100.00)
your_account.statement_of_account()
printl
our_account.pay_in(200.00)
our_account.statement_of_account()
printl
'transfer some money
account.transfer(30.00, my_account, your_account)
printl
' Inheritance
new savings_account my_savings_account(4444990, 5.2)
'each instance of class savings_account has 3 attributes
printl "Account Number: " & my_savings_account.get_account_num() 'inherited attribute
print " Balance: " & my_savings_account.get_balance() &" Euro"' 'inherited attribute
printl "Rate Percent: " & str(my_savings_account.get_rate_percent(),2)' ' new declared attribute
printl "Number of Accounts: " & account.count
'method pay_in(amount), pay_out(amount) and statement_of_account() can be used
my_savings_account.pay_in(250.00) 'inherited methods
my_savings_account.statement_of_account()'
my_savings_account.pay_out(10.00)'
my_savings_account.statement_of_account()
'instances of class savings_account can use the method pay_interest()
my_savings_account.pay_interest()' ' new method
my_savings_account.statement_of_account()'
printl cr & "Hit Enter ... " : waitkey
del my_account
del your_account 'alias our_account
del my_savings_account
Output:
....
....
Account Number: 4444990 Balance: 0 Euro
Rate Percent: 5.2
Number of Accounts: 2 --> should be 3
Payed in: 250 Euro
Account Number: 4444990 Balance: 250 Euro
Payed out: 10 Euro
Account Number: 4444990 Balance: 240 Euro
Interest payable: 12.48
Account Number: 4444990 Balance: 252.48 Euro
Hit Enter ...