Tải bản đầy đủ (.ppt) (50 trang)

Object oriented programming with C++ - Session 1 - Basic Object Oriented Concepts doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (696.38 KB, 50 trang )


Object Oriented Programming with C++/ Session 1/ 1 of 50
Basic Object Oriented Concepts
Session 1
Object Oriented
Programming with C++/
Session 1/ 2 of 50
Session Objectives

Discuss the following:

The Object-Oriented approach

Drawbacks of traditional programming

Object-Oriented programming

Discuss basic Object-Oriented concepts such as:

Objects

Classes

Properties
Object Oriented
Programming with C++/
Session 1/ 3 of 50
Session Objectives (Contd.)

Methods


Abstraction

Inheritance

Encapsulation

Polymorphism

Compare Classes with Structures

Describe Private and Public sections of Classes
Object Oriented
Programming with C++/
Session 1/ 4 of 50
Session Objectives (Contd.)

Define Member functions

Use the Objects and Member functions of a Class

Define Objects

Access Member Functions

Pass and return Objects

Discuss briefly the features of C++ and another OO language
(Smalltalk)
Object Oriented
Programming with C++/

Session 1/ 5 of 50
The Object-Oriented approach

Can classify living beings as objects just as we classify things we use
as objects of different types.

Any application can be defined in terms of entities or objects so that
the process replicates human thought process as closely as
possible.
Object Oriented
Programming with C++/
Session 1/ 6 of 50
Departments as objects in an organisation

People in each department control and operate on that
department's data.
Sales
Personnel Accounts
Object Oriented
Programming with C++/
Session 1/ 7 of 50
Traditional programming: Drawbacks

Unmanageable programs

Traditional programs: List of instructions written in a
language that tells the computer to perform some
actions.

When programs become larger they become

unmanageable.

Functions/procedures/subroutines adopted to make
programs more comprehensible.

As programs grow larger and more complex, even use
of functions can create problems.
Object Oriented
Programming with C++/
Session 1/ 8 of 50
Problems in modification of data

Data plays a crucial role in traditional programming
techniques.

Adding new data items means modifying all the tasks or
functions that access the data.
• As program size increases it is difficult to locate and to
modify all functions which use the data

Restrict access to the data so that only a few critical
functions act upon it.

Next to impossible to separate parts of a program from
revisions made in another part.
Object Oriented
Programming with C++/
Session 1/ 9 of 50
Difficulty in implementation


Focus of traditional programming approach: On implementation
details.

Focus of a person’s thinking: In terms of things or entities, their
properties and actions.

OOP techniques help to correspond real-world entities and actions to
functions and data at the programming level.
Object Oriented
Programming with C++/
Session 1/ 10 of 50
Introduction to OOP

OOP allows for analysis and design of an application in terms of
entities or objects.

Process replicates the human thought process as closely as
possible.

Code and data are merged into a single indivisible thing an object.

Close match between objects in the programming sense and objects
in the real world.
Object Oriented
Programming with C++/
Session 1/ 11 of 50
Data and Functions of an Object
Accounts
Data:
Number of employees

Salary statements
Bills
Vouchers
Receipts
Petty cash records
Banking data
Functions:
Calculate salary
Pay salary
Pay bills
Tally accounts
Transact with banks
Object Oriented
Programming with C++/
Session 1/ 12 of 50
Objects

Represent an entity in the real world.

A concept or thing with defined boundaries that is relevant to the
problem we are dealing with.

Objects serve two purposes:

They help to understand the real world

They provide a practical basis for a computer
application
Object Oriented
Programming with C++/

Session 1/ 13 of 50
Objects (Contd.)

Each object has its own properties or characteristics that describe
what it is or does.

Vehicles in a traffic-monitoring application

Menus

The mouse and the keyboard

A personnel file

A table of marks relating to an examination

Time

Complex numbers
Object Oriented
Programming with C++/
Session 1/ 14 of 50
Different Objects
Name: Jack
Age: 28
Weight: 65 kgs
Model: Ferrari
Colour: Red
Year: 1995
Actions:

Walk
Talk
Sleep
Actions:
Start
Stop
Accelerate
Object Oriented
Programming with C++/
Session 1/ 15 of 50
Classes

Grouping of objects that have the same properties, common
behaviour and common relationships.

The term class is an abbreviation of “class of objects”.

Example, A class of persons, class of animals, class of
processes.

Each object is said to be an instance of its class.
Object Oriented
Programming with C++/
Session 1/ 16 of 50
Objects and Classes
Abstract
into
Polygon class
Properties:
Vertices

Border colour
Fill colour
Methods:
Draw
Erase
Move
Polygon objects
Object Oriented
Programming with C++/
Session 1/ 17 of 50
Property/Attribute

A characteristic required of an object or entity when represented in a
class is called a property.

A class is a prototype and not an actual specimen of the entity.

Each instance of the class or object has its own value for each of its
properties but it shares the property names or operations with other
instances of the class.
Object Oriented
Programming with C++/
Session 1/ 18 of 50
Method

An action required of an object or entity when represented in a class
is called a method.

In a polygon class "draw", "erase" and "move" are
examples of the methods that are part of the class.


Object is a "black box" which receives and sends messages.
Object Oriented
Programming with C++/
Session 1/ 19 of 50
Method (Contd.)

The black box actually contains
code (sequences of computer
instructions) and data
(information which the
instructions operates on).

Information passed to and
retrieved from each department
either by inter-departmental
memos or verbal instructions
are the messages between
objects.

These messages can be
translated to function calls in a
program.
Sales
Accounts
What is the
salary of
Jack?
What is the
salary of

Jack?
J
a
c
k
'
s

s
a
l
a
r
y

i
s

$
2
0
0
0
J
a
c
k
'
s


s
a
l
a
r
y

i
s

$
2
0
0
0
Object Oriented
Programming with C++/
Session 1/ 20 of 50
Abstraction

Process of examining certain aspects of a problem.

An abstract specification tells us what an object does independent of
how it works.

Data Abstraction

Process of examining all the available information
about an entity to identify information that is relevant
to the application.

Object Oriented
Programming with C++/
Session 1/ 21 of 50
Data Abstraction

Used to identify properties and methods of each
object as relevant to the application at hand.

By grouping objects into classes, we are doing
data abstraction of a problem.

Common definitions are stored once per class
rather than once per instance of the class.

Methods can be written once for a class, so that
all the objects in a class benefit from code reuse.
Object Oriented
Programming with C++/
Session 1/ 22 of 50
Inheritance

It is the property that allows the reuse of an existing class to build a
new class.

A class of animals can be divided into mammals,
amphibians, insects, reptiles, etc.

The superclass is the class from which another class inherits its
behaviour.


The class that inherits the properties and methods of another class is
called the subclass.
Object Oriented
Programming with C++/
Session 1/ 23 of 50
Inheritance (Contd.)

Each subclass shares common properties with the class from which
it is derived.

For example, all vehicles in a class may share similar
properties of having wheels and a motor

Subclass may have its own particular characteristics.

For example, a bus may have seats for people, while
trucks have space for carrying goods.
Object Oriented
Programming with C++/
Session 1/ 24 of 50
Class Animals and its subclasses

Animals
Insects Mammals
Reptiles
Amphibians
Object Oriented
Programming with C++/
Session 1/ 25 of 50
Encapsulation


Providing access to an object only through its messages, while
keeping the details private is called information hiding. An equivalent
buzzword is encapsulation.

A class has many properties and methods. It is not
necessary for a user to have access to all of them.

All communication to an object is done via messages.
Messages define the interface to the object.

×