0% found this document useful (0 votes)
121 views30 pages

CSCI 1120 Introduction To Computing Using C++: Chenguang ZHENG SHB 122A

This document describes using inheritance to model different bank account types. A base Account class is defined with deposit and withdraw functions. SavingsAccount and CheckingAccount subclasses inherit from Account, with SavingsAccount adding interest calculation and CheckingAccount modifying deposit and withdraw to include transaction fees. Test programs for each class are outlined to verify the inheritance hierarchy works as intended. Inheritance allows common account functionality to be defined once in the base class and reused across specific account types.

Uploaded by

Wu Chun Ricardo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views30 pages

CSCI 1120 Introduction To Computing Using C++: Chenguang ZHENG SHB 122A

This document describes using inheritance to model different bank account types. A base Account class is defined with deposit and withdraw functions. SavingsAccount and CheckingAccount subclasses inherit from Account, with SavingsAccount adding interest calculation and CheckingAccount modifying deposit and withdraw to include transaction fees. Test programs for each class are outlined to verify the inheritance hierarchy works as intended. Inheritance allows common account functionality to be defined once in the base class and reused across specific account types.

Uploaded by

Wu Chun Ricardo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

CSCI 1120 Introduction to

Computing Using C++

Tutorial 10: Account Inheritance Hierarchy

Chenguang ZHENG
SHB 122A
cgzheng@cse.cuhk.edu.hk

1
Outline
• Case Study: Scenario
• Problem Analysis
• Inheritance Hierarchy
• Superclass and Subclass
• Test and Client Program
• Summary

2
Case Study
• A bank try to efficiently represent customers’ bank a
ccount by using OOP.
• Customers’ account usually has two operations
– Deposit money (called credit)
– Withdraw money (called debit)
• We discuss two specific types of accounts
– Savings accounts
– Checking accounts
• Should we define a class for each account?
– Customer account, savings account, and checking accoun
t 3
Outline
• Case Study: Scenario
• Problem Analysis
• Inheritance Hierarchy
• Superclass and Subclass
• Test and Client Program
• Summary

4
Problem analysis (a)
• We find that the member functions and variable
s among the accounts are overlapped.
• When designing two or more classes that are di
fferent but share some common features, we us
e inheritance.
• The customer account can be viewed as the bas
e class (or, superclass), then the savings account
and checking account can be viewed as the deri
ved classes (or, subclass).
5
Problem analysis (b)
• Base class: Account
– Data member: (type double) to represent the account
balance
– Constructor: to initialize the account balance; and also
check the initial balance >= 0, if not satisfied, display a
n error message
– Three member functions:
• credit: add an amount to the current balance
• debit: withdraw money and ensure that the debit amount s
hould not exceed the current balance. If it does, the balanc
e should be unchanged and display some error message.
• getBalance: return the current balance 6
Problem analysis (c)
• Derived class: SavingsAccount
– Inherit the functionality of Account
• credit
• debit
• getBalancce
– New data member: (type double) interest rate
• Because save money in bank
– New public member function: calculateInterest()
• return a double indicating the amount of interest earned
by an account.

7
Problem analysis (d)
• Derived class: CheckingAccount
– Inherit the functionality of Account
• getBalancce
– Further consider transactions fee in accounts
• New data member: (type double) represent the fee charg
ed per transaction
– Redefine credit and debit to subtract fee
• credit: cost fee each time
• debit: cost fee when withdraw succeeds

8
Outline
• Case Study: Scenario
• Problem Analysis
• Inheritance Hierarchy
• Superclass and Subclass
• Test and Client Program
• Summary

9
Inheritance Hierarchy

Account
credit debit getBalance

credit debit getBalance Redefine Redefine


credit debit
SavingsAccount CheckingAccount
calculateInterest getBalance

10
Source files in VS project columns

Header Files
(Class headers)
Account.h
CheckingAccount.h
SavingsAccount.h

Source Files
(Class files)
Account.cpp
CheckingAccount.cpp
SavingsAccount.cpp
(client program)
client.cpp

11
Outline
• Case Study: Scenario
• Problem Analysis
• Inheritance Hierarchy
• Superclass and Subclass
• Test and Client Program
• Summary

12
Superclass: header file
• The header file can be shown as follows.
• Two operations
– Save money
– Withdraw money
• Get account balance

13
Superclass: class file (a)
• Constructor: check the initialization value

Account::accountBalance and accountBalance are both acceptable


• Save money

14
Superclass: class file (b)
• Withdraw money: check if there is enough money

Why bool function?


 to check whether withdraw succeeds or not

15
Superclass: class file (b)
• Get account balance

16
Subclass-saving: header file
• The header file should add superclass header file.
• The member functions in superclass are all inheri
ted by default.
• Add some new method and data member

17
Subclass-saving: class file
• The class file should add subclass header file.
• New constructor

from superclass

18
Subclass-saving: class file
• Calculate the interest

Can we use accountBalance or Account::accountBalance here?


 No. Because subclass cannot access the private members in
superclass.

19
Subclass-checking: header file
• The header file should add superclass header file.
• You can also redefine some member functions ba
sed on the superclass.

20
Subclass-checking: class file
• The class file should add subclass header file.
• New constructor

21
Subclass-checking: class file
• How to update the account balance with fee?

By using the Account::credit() in superclass.

By using the Account::debit() in superclass.

22
Outline
• Case Study: Scenario
• Problem Analysis
• Inheritance Hierarchy
• Superclass and Subclass
• Test and Client Program
• Summary

23
Test - 1
• Base class – a
– Include header of base class

24
Test - 2
• Base class – b
– Include header of base class

25
Test - 3
• SavingsAccount
– Include header of SavingsAccount

26
Test - 4
• CheckingAccount
– Include header of CheckingAccount

27
Outline
• Case Study: Scenario
• Problem Analysis
• Inheritance Hierarchy
• Superclass and Subclass
• Test and Client Program
• Summary

28
Summary
• For inheritance, firstly you should be clear what is the
base class.
• The relationship between superclass and subclass sho
uld be analyzed, e.g., via diagram.
• For the subclass, you should know what is new and w
hat can be inherited via superclass.
• You are recommended to debug/test the superclass a
nd then subclass.
• Use the same function name in subclass as in the supe
rclass.
• Inheritance makes programming efficiently.
29
Thank You!

30

You might also like