C Class and OBject - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

C Class and OBject

Description:

Account(string initID, string initPIN, double initBalance); void Deposit(double amount) ... end of while // to be continued //to continue. int login(Account acc) ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 38
Provided by: yxie
Category:
Tags: class | double | ended | object

less

Transcript and Presenter's Notes

Title: C Class and OBject


1
C Class and OBject
  • Dr. Ying Xie

2
Motivation
  • Facilitate Solving real life problems.

3
Task to simulate a simple ATM system
4
Task Using computer to simulate a simple ATM
system
Welcome to CSIS3401 Bank -------------------------
----------------------- w withdrawal b
check balance d deposit q
logout -------------------------------------------
----- gtw
5
Welcome to CSIS3401 Bank How much would you like
to withdraw? gt50
6
Welcome to CSIS3401 Bank 50.00 is withdrawn
from your account. Do you need other services? Y
yes N no gtY
7
Welcome to CSIS3401 Bank -------------------------
----------------------- w withdrawal b
check balance d deposit q
logout -------------------------------------------
----- gtb
8
Welcome to CSIS3401 Bank Your current balance is
1350.00. Do you need other services? Y yes N
no gtY
9
Welcome to CSIS3401 Bank -------------------------
----------------------- w withdrawal b
check balance d deposit q
logout -------------------------------------------
----- gtd
10
Welcome to CSIS3401 Bank How much will you be
depositing? gt1000
11
Welcome to CSIS3401 Bank 1000.00 is deposited
to your account. Do you need other services? Y
yes N no gtN
12
(No Transcript)
13
Identifying Objects from the Task
ATM System
BANK ----------------------- LoadAccounts S
aveAccounts VeryfyAccount MakeDeoposit MakeWithdra
wal GetBalance
ATM ----------------------- admin_ID admi
n_PIN ----------------------- PowerOn Login ServeC
ust Deposit Withdraw CheckBalance Shutdown
ACCOUNT ----------------------- id pin balance
----------------------- Deposit Withdraw GetBalan
ce GetID GetPIN
14
Relations between Objects
ATM System
BANK ----------------------- LoadAccounts S
aveAccounts VeryfyAccount MakeDeoposit MakeWithdra
wal GetBalance
ATM ----------------------- admin_ID admi
n_PIN ----------------------- PowerOn Login ServeC
ust Deposit Withdraw CheckBalance Shutdown
ACCOUNT ----------------------- id pin balance
----------------------- Deposit Withdraw GetBalan
ce GetID GetPIN
15
How to Implement ATM System
  • Where to start?
  • It seems we dont know what programming elements
    in C can be used to describe the real-life
    objects.
  • C allows users to define their own CLASSES as
    customized data types to model real-life objects.

16
ACCOUNT ----------------------- ID PIN Balance
----------------------- Deposit Withdraw GetBalan
ce getID getPIN
  • Class Declaration
  • include ltstringgt
  • class Account
  • public //member functions
  • Account()
  • Account(string initID, string initPIN,
    double initBalance)
  • void Deposit(double amount)
  • void Withdraw(double amount)
  • double GetBalance()
  • string GetID()
  • string GetPIN()
  • private //data members
  • double balance
  • string id
  • string pin

17
  • Class Declaration Syntax
  • class Classname
  • public
  • int function1()
  • void function2(int)
  • char function3(char)
  • private
  • int member1
  • string member2
  • public member access specifier. Any data member
    or member function declared after public is
    accessible to the client program.
  • private member access specifier. Any data member
    or member function declared after private is ONLY
    accessible to member functions of this class.

18
Class and Objects
account1
Deposit
ID 12345 PINAAAA Balance 100.00
Withdraw
GetBalance
Account ----------------------- ID PIN Balance
----------------------- Deposit Withdraw GetBalan
ce getID getPIN
getID
getPIN
account2
Deposit
ID 56789 PINzzzzz Balance 88.00
Withdraw
GetBalance
getID
getPIN
19
  • Class Implementation
  • AccountAccount()
  • balance 0
  • id ""
  • pin ""
  • AccountAccount(string initID, string initPIN,
    double initBalance)
  • id initID
  • pin initPIN
  • balance initBalance
  • void AccountDeposit(double amount)
  • balance amount
  • void AccountWithdraw(double amount)

account1
Deposit
ID 12345 PINAAAA Balance 100.00
Withdraw
GetBalance
getID
getPIN
Generally, member functions operate on the
objects private data members. Public member
functions define the ways to access and modify
the data members from outside of the object.
20
Class Implementation Syntax
  • void Classnamefunction1()
  • is called binary scope resolution operator.
  • Classname ties the following member function
    to the corresponding class declaration.
  • void function1()
  • Without Classname, function1 is called free
    function. It can not access the private members
    of Classname.

21
Class declaration -- data member declarations
-- member function prototypes
C Class
Class implementation -- member function
definitions
22
//Account.h include ltstringgt class
Account public //member functions
Account() Account(string initID,
string initPIN, double initBalance)
void Deposit(double amount) int
Withdraw(double amount) double GetBalance()
string GetID() string GetPIN() private
//data members double balance
string id string pin
AccountAccount() balance 0 id
"" pin "" AccountAccount(string initID,
string initPIN, double initBalance) id
initID pin initPIN balance
initBalance void AccountDeposit(double
amount) balance amount void
AccountWithdraw(double amount) balance
- amount double AccountGetBalance()
return balance
23
Separating interface from implementation
  • The client program (driver) only needs to know
    the interface of a class in order to use it.
  • Place the class declaration in a header file
    (.h), which forms the interface of this class
  • Place the definitions of the class member
    functions in a source file (.cpp). This forms the
    implementation of the class.
  • A client program only needs to include the header
    file in order to access this class.

24
  • //Account.h
  • ifndef ACCOUNT_H
  • define ACCOUNT_H
  • include ltstringgt
  • using namespace std
  • class Account
  • public
  • //member functions
  • Account()
  • Account(string initID, string initPIN,
    double initBalance)
  • void Deposit(double amount)
  • int Withdraw(double amount)
  • double GetBalance()
  • string GetID()
  • string GetPIN()

25
  • //Account.cpp
  • include"Account.h
  • AccountAccount()
  • balance 0
  • id ""
  • pin ""
  • AccountAccount(string initID, string initPIN,
    double initBalance)
  • id initID
  • pin initPIN
  • balance initBalance
  • void AccountDeposit(double amount)
  • balance amount
  • int AccountWithdraw(double amount)
  • if (balance gt amount)
  • balance - amount
  • return 0
  • else
  • return -1
  • double AccountGetBalance()
  • return balance
  • string AccountGetID()
  • return id

26
  • ifndef ACCOUNT_H
  • define ACCOUNT_H
  • .
  • endif
  • Use ifndef, define, and endif preprocessor
    directives to prevent header files from being
    included more than once in a program.
  • Generally, use the name of the header file with
    period replaced by an underscore in the ifndef
    and define preprocessor directives of a header
    file.

27
  • //Client.cpp
  • includeltiostreamgt
  • include "Account.h"
  • char showMenu()
  • int login(Account)
  • int main()
  • double amount
  • char request
  • Account account1("00001", "aaaaa", 600.00)
  • while (true)
  • if (login(account1) 0)
  • request showMenu()
  • while(request! 'T')
  • switch(request)
  • case 'S'
  • break

case 'D' cout
ltlt"PLease input the amount " cin gtgt
amount account1.Deposit(amount) break
case 'C' cout ltlt"\nYour current balance
is " ltlt account1.GetBalance() break
Default cout ltlt "Incorrect function
code entered. " break
//end of switch request
showMenu() //end of
while(request!T) //end of if
else cout ltlt "\n\nLogin failed, try
again! " ltlt endl //end of while //
to be continued
28
  • //to continue
  • int login(Account acc)
  • string id, pin
  • cout ltlt "\n\n"
  • cout ltlt "Please type your id " ltlt endl
  • cin gtgt id
  • cout ltlt "Please type your pin " ltlt endl
  • cin gtgt pin
  • if ((acc.GetID() id)(acc.GetPIN()
    pin))
  • return 0
  • else
  • return -1
  • char showMenu()
  • char request
  • cout ltlt "\n\n------------function
    Menu----------------"


29
  • Account account1(00001, aaaaa, 600.00)

AccountAccount(string initID, string initPIN,
double initBalance) id initID
pin initPIN balance initBalance
Constructor has the same name as the class name
no return type.
30
  • Account account2

Account Account () id
pin balance 0
The constructor with empty parameter list is
called default constructor.
31
  • //Client.cpp
  • includeltiostreamgt
  • include "Account.h"
  • char showMenu()
  • int login(Account)
  • int main()
  • double amount
  • char request
  • Account account1("00001", "aaaaa", 600.00)
  • while (true)
  • if (login(account1) 0)
  • request showMenu()
  • while(request! 'T')
  • switch(request)
  • case 'S'
  • break

case 'D' cout
ltlt"PLease input the amount " cin gtgt
amount account1.Deposit(amount) break
case 'C' cout ltlt"\nYour current balance
is " ltlt account1.GetBalance() break
Default cout ltlt "Incorrect function
code entered. " break
//end of switch request
showMenu() //end of
while(request!T) //end of if
else cout ltlt "\n\nLogin failed, try
again! " ltlt endl //end of while //
to be continued
32
  • While (true)
  • if (login(account1) 0) //login
    succeeds
  • // show menu and serve customer
  • else
  • cout ltlt "\n\nLogin failed, try
    again! " ltlt endl

int login(Account acc) string id, pin
cout ltlt "\n\n" cout ltlt "Please type your
id " ltlt endl cin gtgt id cout ltlt
"Please type your pin " ltlt endl cin gtgt
pin if ((acc.GetID() id)(acc.GetPIN()
pin)) return 0 else
return -1
33
  • While (true)
  • if (login(account1) 0) //login
    succeeds
  • request showMenu()
  • switch(request)
  • //process the users
    request
  • else
  • cout ltlt "\n\nLogin failed, try
    again! " ltlt endl

char showMenu() char request cout
ltlt "\n\n------------function Menu----------------"
cout ltlt "\n\n" cout ltlt "Please
choose the function\n" cout ltlt "S show
menu again" ltlt endl cout ltlt "C check the
balance" ltlt endl cout ltlt "W withdraw"
ltltendl cout ltlt "D Deposit" ltlt endl
cout ltlt "T Terminate" ltlt endl cout ltlt
"\n\n requestgt " cin gtgt request
return request
34
  • switch(request)
  • case 'S'
  • break
  • case 'W'
  • cout ltlt "Please input the amount "
  • cin gtgt amount
  • if(account1.Withdraw(amount)0)
  • cout ltlt "withdraw succeeds!"
  • else
  • cout ltlt "withdraw fails."
  • break
  • case 'D'
  • cout ltlt"PLease input the amount "
  • cin gtgt amount
  • account1.Deposit(amount)
  • break
  • case 'C'
  • cout ltlt"\nYour current balance is "
  • ltlt account1.GetBalance()

35
Our 4th C program
  • Our first OO program in C
  • Our first multi-file project
  • Class declaration account.h
  • Class implementation account.cpp
  • Client program client.cpp

36
  • How to create executable application for
    multi-file project?

account.cpp (Class Implementation
account.h (Class Declaration)
client.cpp (Client program)
Complier
Complier
account.o Object file
client.o Object file
linker
Final executable file
37
  • g -c account.cpp
  • g -c client.cp
  • g -o client client.o account.o
Write a Comment
User Comments (0)
About PowerShow.com