Docsity

Object-Oriented Programming with C++: Understanding Objects, Classes, and Bank Accounts - , Study notes of Engineering

Partial preview of the text

Download Object-Oriented Programming with C++: Understanding Objects, Classes, and Bank Accounts - and more Study notes Engineering in PDF only on Docsity!

CSE294B

Object-Oriented C++ Programming for EngineersObject-Oriented C++ Programming for Engineers

Lecture

Jeffrey Miller, Ph.D.

Object Analogy

-^

To drive a car, you need the accelerator pedal, thebrake pedal, the steering wheel, and the ignition(at minimum)(at minimum)

-^

But what actually happens when you turn the keyin the ignition?

M

d i

d^

k^

b^

h^

d^

d

  • Most drivers do not know, but they do not need to

know to be able to drive the car

-^

This is the idea behind objects

j

  • The code that uses the object knows

what

the function

will do, but does not need to know

how

it does it

Object Descriptions

• Objects (and in turn, classes) can be

described by two key features

y

y

  • Data– Functions

Functions

• The functions of an object operate on the

data that is part of the object or data that isdata that is part of the object or data that ispassed into the function (possibly inconjunction with the data of the object)conjunction with the data of the object)

Bank Account Object

•^

What is the data that is associated with a bank account?–

NameA

t N

b

-^

Account Number

-^

Balance

-^

PIN

•^

What are the functions that can be performed on a bankaccount?

Deposit

-^

Deposit

-^

Withdrawal

-^

Transfer

-^

Check Balance

-^

Change PIN

Access Specifiers

•^

There are two access specifiers in C++–

public

i^

t

-^

private

•^

If a variable or function is declared public, any other pieceof code can access the variable or function

-^

If a variable or function is declared private, only the classin which that variable or function is declared can access it

l^

f^

h^

b^

id

h^

d^

d^

h

•^

Rule of Thumb – Hide the data and expose the necessaryfunctions

Encapsulation

• A class is considered

encapsulated

if all of

the data is declared private

p

• A class is considered

fully encapsulated

if

all of the data is declared private AND youall of the data is declared private AND youprovide an accessor (getter) andmanipulator (setter) function for each piecemanipulator (setter) function for each pieceof data

Manipulators are also called mutators

  • Manipulators are also called mutators

Homework

• Homework #3 is posted!