Object Oriented Programming - CS304 Handouts, Lecture notes of Object Oriented Programming

Complete topic

Typology: Lecture notes

2011/2012

Uploaded on 12/14/2012

ddaauudd1
ddaauudd1 🇮🇳

1 document

1 / 349

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object Oriented Programming (CS304) VU
© Virtual University of Pakistan
1
CS304-Handouts
Last Updated: 20/08/2009
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Object Oriented Programming - CS304 Handouts and more Lecture notes Object Oriented Programming in PDF only on Docsity!

© Virtual University of Pakistan

CS304-Handouts

Last Updated: 20/08/

© Virtual University of Pakistan

© Virtual University of Pakistan

LECTURE NO.45 .............................................................................................................................. 343

45.1. RESOURCE MANAGEMENT ............................................................................................. 343

© Virtual University of Pakistan

Lecture No.

01.1. Introduction

Course Objective:

Objective of this course is to make students familiar with the concepts of

object oriented programming. These concepts will be reinforced by their

implementation in C++.

Course Contents:

The main topics that we will study in the 45 lectures of this course are given

below,

  • Object Orientation
  • Objects and Classes
  • Overloading
  • Inheritance
  • Polymorphism
  • Generic Programming
  • Exception Handling
  • Introduction to Design Patterns

Recommended Text Book:

C++ How to Program ( Deitel & Deitel )

Reference Books:

1. Object-Oriented Software Engineering

By Jacobson, Christerson, Jonsson, Overgaard

(For object oriented programming introductory concepts)

2. The C++ Programming Language

By Bjarne Stroustrup

(For better c++ understanding)

© Virtual University of Pakistan

Suppose we want to develop a fee collection system for a school for this we will need

to find out related objects and their interactions as happens in real life.

In this way we can say that object orientation makes it easier for us to solve our real

world problems by thinking solution of the problem in terms of real world objects.

So we can say that in our daily life everything can be taken as an object that behaves in a

certain way and has certain attributes.

In object orientation we move our concentration to objects in contrast to procedural

paradigm in which we simply write our code in functions and call them in our main

program.

01.2. What is a Model?

A model is an abstraction of something real or conceptual.

We need models to understand an aspect of reality.

Model Examples

Teacher Student School Bag

Book Pen Playground

Parents Classroom Library

© Virtual University of Pakistan

Highway maps

Architectural models

Mechanical models

01.3. OO Models:

In the context of programming models are used to understand the problem before

starting developing it.

We make Object Oriented models showing several interacting objects to understand

a system given to us for implementation.

Example 1– Object Oriented Model

Objects

Ali, Car, House, Tree

Interactions

Ali lives in the house

Ali drives the car

Example 2– Object Oriented Model (A School Model)

Ali

Car

House

Tree

lives-in

drives

© Virtual University of Pakistan

Ali is a tangible object, having some characteristics (attributes) and behavior as given

below,

Ali

Characteristics (attributes) Behaviour (operations)

Name

Age

Walks

Eats

We will identify Ali using his name.

Car is also a tangible object having some characteristics (attributes) and behavior

given below,

Car

State (attributes) Behavior (operations)

Color

Model

Accelerate

Start Car

Change Gear

We can identify Car using its registration number

Examples of Intangible Objects (also called as conceptual objects):

Time is an intangible (conceptual) object

Time

State (attributes) Behavior (operations)

Hours

Seconds

Minutes

Set/Get Hours

Set/Get Seconds

Set/Get Minutes

We will assign our own generated unique ID in the model for Time object

Date is also an intangible (conceptual) object

State (attributes)

Year

Day

Month

Behavior (operations)

Set/Get Year

Set/Get Day

Set/Get Month

We will assign our own generated unique ID in the model for Date object.

01.7. Summary:

© Virtual University of Pakistan

  • Model is the abstraction of some real word scenario. It helps us to understand

that scenario.

  • Object oriented model of any scenario (problem) describes that scenario

(problem) in the form of interacting objects.

  • We use Object Orientation because it helps us in mapping real world problem

in a programming language.

  • Object Orientation is achieved using objects and their relationships.
  • Properties of an object are described using its data members and behavior of an

object is described using its functions.

  • Objects may be tangible (physical) or intangible (also called conceptual or

virtual).

  • Generally when we have given a certain problem description, nouns in that

problem description are candidates for becoming objects of our system.

  • There may be more than one aspects of an object
  • It is not necessary that every object has a specific role in implementation of a

problem there may be some objects without any role, like school parking in

our school.

  • It is easier to develop programs using Object Oriented Programming because

it is closer to real life.

© Virtual University of Pakistan

  • All information related to an object is stored within the object
  • It is hidden from the outside world
  • It can only be manipulated by the object itself

Advantages of Information Hiding

Following are two major advantages of information hiding,

It simplifies our Object Oriented Model:

As we saw earlier that our object oriented model only had objects and their

interactions hiding implementation details so it makes it easier for everyone to

understand our object oriented model.

It is a barrier against change propagation

As implementation of functions is limited to our class and we have only given the

name of functions to user along with description of parameters so if we change

implementation of function it doesn’t affect the object oriented model.

We can achieve information hiding using Encapsulation and Abstraction, so we see

these two concepts in detail now,

02.2. Encapsulation

Encapsulation means “we have enclosed all the characteristics of an object in the object

itself”

Encapsulation and information hiding are much related concepts (information

hiding is achieved using Encapsulation)

We have seen in previous lecture that object characteristics include data members

and behavior of the object in the form of functions.

So we can say that Data and Behavior are tightly coupled inside an object and

both the information structure and implementation details of its operations are

hidden from the outer world.

Examples of Encapsulation

Consider the same example of object Ali of previous lecture we described it as

follows,

© Virtual University of Pakistan

You can see that Ali stores his personal information in itself and its behavior is

also implemented in it.

Now it is up to object Ali whether he wants to share that information with

outside world or not. Same thing stands for its behavior if some other object in

real life wants to use his behavior of walking it can not use it without the

permission of Ali.

So we say that attributes and behavior of Ali are encapsulated in it.

Any other object don’t know about these things unless Ali share this information

with that object through an interface,

Same concept also applies to phone which has some data and behavior of

showing that data to user we can only access the information stored in the phone

if phone interface allow us to do so.

Advantages of Encapsulation

The following are the main advantages of Encapsulation,

a. Simplicity and clarity

As all data and functions are stored in the objects so there is no data or function

around in program that is not part of any object and is this way it becomes very

easy to understand the purpose of each data member and function in an object.

b. Low complexity

As data members and functions are hidden in objects and each object has a

specific behavior so there is less complexity in code there will be no such

situations that a functions is using some other function and that functions is

using some other function.

c. Better understanding

Everyone will be able to understand whole scenario by simple looking into object

diagrams without any issue as each object has specific role and specific relation

with other objects.

02.3. Interface

Ali

Characteristics

(attributes)

  • Name
  • Age

Behavior

(operations)

  • Walks
  • Eats

© Virtual University of Pakistan

So it has,

  • Data Structure in the form of Mechanical structure of gear box
  • Functionality mechanism to change gear

b. Address Book in a Phone

Similarly take the example of contact details saved in the SIM of a phone,

In that case we can say physical structure of SIM card as Data Structure

And Read/write operations provided by the phone as Functionality.

02.5. Separation of Interface & Implementation

As discussed earlier we only show interface of an object to outside world and

hide actual implementation from outside world. The benefit of using this

approach is that our object interface to outside word becomes independent

from inside implementation of that interface.

This is achieved through the concepts of encapsulation and information

hiding.

Real Life example of separation of interface and implementations

 Driver has a standard interface to drive a car and using that interface

he drive can drive any car regardless of its model or type whatever

engine type it has or whatever type of fuel it is using.

02.6. Messages

Objects communicate through messages they send messages (stimuli) by

invoking appropriate operations on the target object. The number and kind of

messages that can be sent to an object depends upon its interface

Examples – Messages

A Person sends message (stimulus) “stop” to a Car by applying brakes

A Person sends message “place call” to a Phone by pressing appropriate button

02.7. Summary

  • Information hiding is achieved through encapsulation.
  • Encapsulation and Information Hiding are related to each other.
  • Interface of an object provides us the list of available functions.
  • An object may have more than one interface.
  • Interface and implementation are separated from each other to achieve

Information Hiding.

  • Objects communicate with each other using messages.

© Virtual University of Pakistan

Useful Links:

http://www.alice.org/

A Graphical Programming Environment to teach Computer Programming.