CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I.
-
Upload
gregory-perry -
Category
Documents
-
view
223 -
download
2
Transcript of CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I.
CS212: Object Oriented Analysis and Design
Lecture 4: Objects and Classes - I
Recap to Lecture 3
• Introduction to objects and classes
• Identification of classes and object
• Class Responsibility and Collaboration
3
A CRC card: SnackItem
Class name: SnackItem
Responsibility Collaborator
Knows its price and calories
4
A CRC card: Vending Machine
Class name: VendingMachine
Responsibility Collaborator
Maintains a collection of SnackItems.
Allows addition and removal of SnackItems
The Advantages of CRC Model
• The experts do the analysis
• User participation increased.
• Breaks down communication barriers
• It’s simple and straightforward
• It’s non-threatening to users
• It’s inexpensive and portable
• It goes hand-in-hand with prototyping
• It leads directly into class diagramming
Disadvantages
• It’s threatening to some developers
• It’s hard to get users together
• CRC cards are limited
Case study 2: ATM
An Automated Teller Machine (ATM) allows bank customers to perform a number of financial transactions: to withdraw and deposit funds to an account, query the balance of any account. The ATM offers an user interface with a display screen, keypad, cash dispenser, deposit slot and a card reader.
Once a customer’s card is verified, the customer can query to see the balance in all her account (s), deposit, withdraw or transfer money from one account into another.
Case study 2: ATM
An Automated Teller Machine (ATM) allows bank customers to perform a number of financial transactions: to withdraw and deposit funds to an account, query the balance of any account. The ATM offers an user interface with a display screen, keypad, cash dispenser, deposit slot and a card reader.
Once a customer’s card is verified, the customer can query to see the balance in all her account (s), deposit, withdraw or transfer money from one account into another.
Example: GradeBook
A gradebook is one which an instructor uses to keep student test score.
Creating the blue print in C++
• Defining a class
Access specifier
Function header
Executing the blue print
Data Members, get and set functions
• Attributes are represented as variables – data members
• Each object of a class maintains its own copy of attributes in memory
• Get function: used to obtain the value of a member variable, e.g. getStudentName(int id)
• Set function: used to assign a value to a member variable, e.g. setStudentName(string sName) mutators
accessors
Access Specifiers
Public
PrivateProtected
Class members declared under public will be available to everyone
No one can access the class members declared
private outside that class.
It makes class member inaccessible outside the class, except its
subclass.
Access specifiers in C++ class defines the access control rules.
Best practices
• Data members to be declared as private
• Member functions to be declared as public
• Access specifiers must not be mixed
• Explicit use of access specifiers
• Easy to localize the error while debugging
Software engineering with get and set function
• Private access specifier facilitates data hiding
• Public set and get function allows clients to access data, but not indirectly
• Class may store data in one way, however shows to the client in a different way
• Get and set function helps the client to interact with the object
• The private data member remains safely encapsulated
Reusability
• When packaged properly, classes can be reused by programmers
Function ‘int main(void)’ already has a body
Redefinition of ‘int main()’
• Placing the main in the same file where the class is defined prevents reuse
• Use of header files
Software engineering issues
• The entire implementation of the class is revealed to the clients
• To access object of a class, client should only know,• Which function to call• What are the parameters to be passes• What is the expected return type
• How the function is implemented is not important to the client
Class Interface
• Header files supports reusability
• Interface defines and standardize the way in which things such as people and system interacts with each other
• Interface of a class defines what services a client can use and how to request those services
• However, NOT how the class carry out those services
• A classes interface consists of the public member functions
Separating Interface from Implementation
• It is better software engineering to define member functions
outside the class definition
• Their implementation can be hidden from the clients code
• This practice ensures that programmers do not write client code
that depends on the class’s implementation
Thank youNext Lecture: Classes and Object - II