20
CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

Embed Size (px)

Citation preview

Page 1: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

CS212: Object Oriented Analysis and Design

Lecture 4: Objects and Classes - I

Page 2: 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

Page 3: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

3

A CRC card: SnackItem

Class name: SnackItem

Responsibility Collaborator

Knows its price and calories

Page 4: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

4

A CRC card: Vending Machine

Class name: VendingMachine

Responsibility Collaborator

Maintains a collection of SnackItems.

Allows addition and removal of SnackItems

Page 5: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

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

Page 6: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

Disadvantages

• It’s threatening to some developers

• It’s hard to get users together

• CRC cards are limited

Page 7: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

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.

Page 8: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

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.

Page 9: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

Example: GradeBook

A gradebook is one which an instructor uses to keep student test score.

Page 10: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

Creating the blue print in C++

• Defining a class

Access specifier

Function header

Page 11: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

Executing the blue print

Page 12: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

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

Page 13: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

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. 

Page 14: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

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

Page 15: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

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

Page 16: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

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

Page 17: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

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

Page 18: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

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

Page 19: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

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

Page 20: CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I

Thank youNext Lecture: Classes and Object - II