chapter 1 object oriented programming. oop revolves around the concept of an objects. objects are...

14
Chapter 1 Object Oriented Programming

Upload: buddy-clark

Post on 18-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques

Chapter 1

Object Oriented Programming

Page 2: Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques

OOP revolves around the concept of an objects.

Objects are crated using the class definition.

Programming techniques may include features such as Data abstraction.Encapsulation.Polymorphism.Inheritance.

Many modern programming languages now support OOP.

Page 3: Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques

Abstract data typesAn outline of the program containing its requirements should

precede the coding process for a project. Then, in the later stage, the implementation may start with specific data structure. First, specify each task in terms of inputs and outputs.Be concerned with what the program should do. For example, if

an item is needed to accomplish some tasks, the item is specified by the operations performed on it not by its structure.

When the implementation starts, it decides which data structure to use to make the execution more efficient.

An item specified in terms of operations is called an abstract data type.

ADT- An abstract data type can be defined by the operations that are done on the data regardless of its type.

ADT- A set of data values and associated operations that are precisely specified independent of any particular implementation.

ADT- Mathematical description of an object with set of operations on the object.

Page 4: Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques

Some information before startingYou already learned that addresses of variables were

assigned to pointers.However, pointers can refer to unknown locations that are

accessible only through their addresses not by names. These locations must be set by the memory manager

dynamically during the run of the program.To dynamically allocate and de-allocate memory, two

functions are used: 1. new – takes from memory as much space as needed to store

an object. Ex : p = new int;

2. delete – return the space that is accessible from p and is no longer needed.

Ex : delete p; Note : an address has to be assigned to a pointer, if it can’t

be the address of any location, it should be a null address, which is simply 0.

Page 5: Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques

Chapter 3

Linked Lists

Page 6: Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques

Linked listAn array is a very useful data structure in

programming languages . However, it has at least two limitations that can be overcome by using linked structure. It is a collection of nodes storing data and links

(using addresses) to other nodes.A linked list is a data structure that consists of a

sequence of data records such that in each record there is a field that contains a reference (i.e., a link) to the next record in the sequence.

The most flexible linked structure’s implementation is by using pointers.

Linked lists are among the simplest and most common data structures; they provide an easy implementation for several important abstract data structures, including stacks, queues and arrays.

Page 7: Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques

3.1 Singly Linked Lists Linked list – a data structure that composed

of nodes, each node holding some information and a pointer to another node in the list.

Singly linked list – a node has a link only to its successor in the sequence of nodes in the list.

Note :Only one variable is used to access any node in

the list.The last node on the list can be recognized by

the null pointer.

Page 8: Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques

Example :

class IntSLLNode{ public:

IntSLLNode() { next = 0; }

IntSLLNode(int i, IntSLLNode *ptr = 0) { info = i; next = ptr; }

int info; IntSLLNode *next;

};

Page 9: Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques

Now, how to create the linked list 1. Create a new node by executing the declaration and

assignment : IntSLLNode *p = new IntSLLNode(10); This statement create a node on the list and make p

points to it. The constructor assigns the number 10 to the info

member of this node. The constructor assigns null to its next member.

2. Then any new node is included in the list by making the next member of the first node a pointer to the new node.

3. The second node is created by : p -> next = new IntSLLNode (8);

Page 10: Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques
Page 11: Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques

Problem :The longer the linked list, the longer the chain

of nexts to access the nodes at the end of the list when using pointers.1. If you missed a node in the chain, then a

wrong assignment is made 2. The flexibility of using linked lists is

diminished . So, other ways of accessing nodes are

needed. One of them is : To keep two pointers: one to the first

node(Head), and one to the last(Tail).

Page 12: Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques

3.1.1 Insertion

1. inserting a new node at the beginning of a singly linked list

Page 13: Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques

3.1.1 Insertion (cont’)

2. inserting a new node at the end of a singly linked list

Page 14: Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques

Insert At the beginning Insert at the end

addToTail(int el) {

if (tail != 0) { // if list not empty; tail->next = new IntSLLNode(el); tail = tail->next; } else head = tail = new IntSLLNode(el); }

addToHead(int el) {

head = new IntSLLNode(el,head);

if (tail == 0) tail = head; }