1. Trang chủ
  2. » Công Nghệ Thông Tin

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

50 814 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Object Oriented Programming with C++ - Session 1 - Basic Object Oriented Concepts
Thể loại lecture notes
Định dạng
Số trang 50
Dung lượng 523 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Session Objectives Contd. Compare Classes with Structures  Describe Private and Public sections of Classes...  Define Member functions  Use the Objects and Member functions of a C

Trang 1

Basic Object Oriented

Concepts

Session 1

Trang 2

Session Objectives

 Discuss the following:

• The Object-Oriented approach

• Drawbacks of traditional programming

Trang 3

Session Objectives (Contd.)

 Compare Classes with Structures

 Describe Private and Public sections

of Classes

Trang 4

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)

Trang 5

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

Trang 6

Departments as objects in an

organisation

 People in each department control and operate

SalesPersonnel Accounts

Trang 7

Traditional programming:

Drawbacks

• 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.

Trang 8

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

Trang 9

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

Trang 10

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

Trang 11

Data and Functions of an

Trang 12

 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

Trang 13

Objects (Contd.)

 Each object has its own properties or

characteristics that describe what it is or

Trang 14

Different Objects

Name: Jack Age: 28 Weight: 65 kgs

Model: Ferrari Colour: Red Year: 1995

Actions:

Walk Talk Sleep

Actions:

Start Stop Accelerate

Trang 15

 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

Trang 16

Objects and Classes

Abstract into

Polygon class

Properties:

Vertices Border colour Fill colour

Methods:

Draw Erase Move

Polygon objects

Trang 17

 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

Trang 18

 An action required of an object or entity when represented in a class

is called a method

"move" are examples of the methods that are part of the class

 Object is a "black box" which

receives and sends messages

Trang 19

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 departmental memos or verbal instructions are the messages between objects

inter-• 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?

Jack's salary is

$2000

Jack's salary is

$2000

Trang 20

• Process of examining all the available

information about an entity to identify

information that is relevant to the

application

Trang 21

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

Trang 22

 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

Trang 23

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.

Trang 24

Class Animals and its

subclasses

Animals

Insects Mammals Reptiles Amphibians

Trang 25

 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

Trang 26

Encapsulation (Contd.)

 When we properly encapsulate some code we achieve two objectives:

• We build an impenetrable wall to

protect the code from accidental

corruption due to the silly little errors

that we are all prone to make

• We also isolate errors to small sections

of code to make them easier to find and fix

Trang 27

 Object-oriented development allows:

• Information to be shared within an application

• Reuse of designs and code on future projects

 Programs are broken down into reusable objects These objects can then be

grouped together in different ways to

form new programs

 Inheritance also promotes reuse

Trang 28

Felines and Subclasses

Make sounds Eat/drink Hunt prey

Cats Actions:

Mew Drink milk Hunt mice

Roar Eat flesh Hunt big game

Trang 29

 Polymorphism means that the

same functions may behave

differently on different classes

 The existing object stays the same, and any changes made are only

additions to it

Trang 30

 Polymorphism promotes encapsulation The Draw

method is implemented for different cases but how it

Class:

Shape

Methods:

Draw Move Initialise

Subclasses

Trang 32

Classes and Structures

 A structure contains one or more data items

(called members) which are grouped together

as a single unit

 Class consists of not only data elements but

also functions, which operate on the data

elements

 All the variables and the functions declared in a class, whether in the public or private section, are the members of the class

 In a structure all elements are public by default

Trang 33

Public and Private

 Class members can be declared in the public or the private sections of a class

 To prevent data from unrestricted access, the data members of a class are normally declared

in the private section

 The public part constitutes the interface to

objects of the class

 The data members and functions declared in

the public section, can be accessed by any

function in the outside world (outside of the

Trang 34

Private and Public (Contd.)

 Private data is not available outside the class

Class

Data or functions

Data or functions

Trang 35

Member functions

 In C++, a function declared as a member of a

class is called a member function

• Member functions are usually put in the public part of the class because they have to be called outside the

class either in a program or in a function

 Declaration of a member function must define its return value as well as the list of its arguments

• For example, the member function

void setdate(int, int, int) has no return value or

a void return value, and has three integer arguments

Trang 36

Member functions (Contd.)

 Member functions can be more

complex as they can have local

variable, parameters etc

 Should not have the same name as

a data member

Trang 37

Using the class

Trang 38

Using the class (Contd.)

Trang 39

Defining Objects

 exampleclass object1,object2; defines two objects, object1 and

object2, of class exampleclass

 The definition actually creates

objects that can be used by the

program

 When we define an object, space is set aside for it in memory

Trang 40

Calling member functions

 We communicate with objects by

calling their member functions

Trang 41

Calling member functions

 The general syntax for accessing a member function of a class is

class_object.function_member()

 The dot operator is called the class member operator

Trang 42

Two objects with different values

exampleclass class

specifier

object_data 200

object_data 200

object1

object_data 350

Trang 43

Passing Objects

 Objects can be passed to a function

and returned back just like normal

variables

 The compiler creates another object as

a formal variable in the called

function It copies all the data

members from the actual object

int function1(exampleclass obj1)

Trang 44

Passing Objects (Contd.)

using a function call such as,

int variable =

object1.function1(object2);

actual argument

it is given access to the object of

Trang 45

Returning Objects

 A return statement in a function is

considered to initialise a variable of the returned type

exampleclass object3 =

object1.function1(object2);

 Passing and returning of objects is

not very efficient since it involves

passing and returning a copy of the data members

Trang 46

Object-Oriented Languages

 C++ , Smalltalk , Eiffel, CLOS, Java

 Vary in their support of

object-oriented concepts

 No single language that matches

all styles and meets all needs

Trang 47

An introduction to C++

 Developed by Bjarne Stroustrup at

AT&T Bell Laboratories

 Derived from the C language

 It is compatible with C (it is actually

a superset) Most important

elements added to C to create C++ are concerned with classes, objects and object-oriented programming

Trang 48

An introduction to C++

(Contd.)

but it sacrifices some flexibility in

order to remain efficient

Provides high run-time efficiency

and small code size, but it trades

off some of the power to reuse

classes

Trang 49

 Pure object-oriented language

 While C++ makes some practical

compromises to ensure fast execution and small code size, Smalltalk makes none

 Uses run-time binding, which means that

nothing about the type of an object need be known before a Smalltalk program is run

 Significantly faster to develop than C++

programs

Trang 50

 Has a rich class library that can be

easily reused via inheritance

 Has a dynamic development

environment

 It is not explicitly compiled, like C++

 Smalltalk generally takes longer to

master than C++ It is syntactically

very simple

Ngày đăng: 16/03/2014, 01:20

TỪ KHÓA LIÊN QUAN