adavanced data structures unit- i part 1

17
ADVANCED DATA STRUCTURES Veera Shekar G UNIT - I Page 1 UNIT - I INTRODUCTION For a C progr ammer it is eas y for hi m to move towards C++ because syntacticall y there is no big difference between C and C++. But you should note that conceptually there is a l ot of difference. C is based on traditi onal approach that is procedure o ri entation but C++ is based on modern approach that is object orientation. Object oriented programming was developed to reduce the inherent limitations of traditional programming languages. Large programs written in traditional programming languages are complex and prone to errors . Traditional languages like BASIC, Pascal, and C etc. are procedural languages. Programs written in these languages consist of set of instructions. Each instruction tells the computer to do something, read data from keyboard, do some computation, print some output on screen etc. Small programming tasks could be managed with programs consisti ng o f a singl e sequence o f instructions DIFF ERENT STRATEGIES FO R PROBLEM SOLVING The ability of software to do millions of different things is one of reasons for its downfall. Software crisis is result of various factors like less qualitative projects, customer dissat isfaction, over budg et, schedul es no met etc. Software crisi s pred icted three and half decades (first identified in 1968 by NATO) back but never materialized. A general scenario can be seen here that is software failures receive more publicity than successful projects. But truly speaking success rate for software projects is only 10%. The major reasons are unpredictability about time and budget. Response to software crisis is laid foundation for construction of software development models, different programming pri ncip les li ke structured, object o rientation comes in to pictures. There are di fferent strategies for problem so l vin g. Some o f the strategies are 1. Mo noli thi c Programmin g 2. Proc edural Programmin g 3. Structured Programmin g 4. Object Ori ented Programming

Upload: veera-shekar

Post on 06-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

8/3/2019 Adavanced Data Structures UNIT- I Part 1

http://slidepdf.com/reader/full/adavanced-data-structures-unit-i-part-1 1/16

ADVANCED DATA STRUCTURES

Veera Shekar G UNIT - I Page 1

UNIT - I

INTRODUCTION

For a C programmer it is easy for him to move towards C++ because syntactically

there is no big difference between C and C++. But you should note that conceptually

there is a lot of difference. C is based on traditional approach that is procedure orientation

but C++ is based on modern approach that is object orientation.

Object oriented programming was developed to reduce the inherent limitations of

traditional programming languages. Large programs written in traditional programming

languages are complex and prone to errors .

Traditional languages like BASIC, Pascal, and C etc. are procedural languages.

Programs written in these languages consist of set of instructions. Each instruction tells

the computer to do something, read data from keyboard, do some computation, printsome output on screen etc. Small programming tasks could be managed with programs

consisting of a single sequence of instructions

DIFFERENT STRATEGIES FOR PROBLEM SOLVING

The ability of software to do millions of different things is one of reasons for its

downfall. Software crisis is result of various factors like less qualitative projects, customer

dissatisfaction, over budget, schedules no met etc. Software crisis predicted three and

half decades (first identified in 1968 by NATO) back but never materialized. A generascenario can be seen here that is software failures receive more publicity than successfu

projects. But truly speaking success rate for software projects is only 10%. The major

reasons are unpredictability about time and budget. Response to software crisis is laid

foundation for construction of software development models, different programming

principles like structured, object orientation comes into pictures.

There are different strategies for problem solving. Some of the strategies are

1. Monolithic Programming

2.  Procedural Programming

3.  Structured Programming

4.  Object Oriented Programming

8/3/2019 Adavanced Data Structures UNIT- I Part 1

http://slidepdf.com/reader/full/adavanced-data-structures-unit-i-part-1 2/16

ADVANCED DATA STRUCTURES

Veera Shekar G UNIT - I Page 2

Global data that is shared by entire program without any subprograms concept is

called as Monolithic Programming. Traditional BASIC is the best example for monolithic

programming.

To solve a big problem, it is better to divide into smaller pieces and then start

solving it is key fact behind procedural programming. Large programs are divided into subprograms and each sub program is responsible for solving a particular task of the main

problem. All these sub programs are usually share same global data. COBOL is the best

example for procedural programming.

Grouping of related tasks into single unit is called module, group of these modules

solve the large program. This concept is base for structured programming. These modules

will share global data. Pascal and C programming languages are best examples of

Structured Programming.

8/3/2019 Adavanced Data Structures UNIT- I Part 1

http://slidepdf.com/reader/full/adavanced-data-structures-unit-i-part-1 3/16

ADVANCED DATA STRUCTURES

Veera Shekar G UNIT - I Page 3

If we closely observe monolithic, procedural and structured Programming

methodologies, we can identify a common factor that is global data. In monolithic

programming language global data is shared by the entire program. In procedura

language global data is shared by all sub programs. In structural programming language,

modules share the global data. There is no restriction on this global data. Global data is

freely moves throughout all the modules or subprograms. This was the major drawback of

all these programming languages strategies. So there is a need for new strategy called

Object Oriented Programming.

NEED FOR OBJECT ORIENTED PROGRAMMING

A new way of problem solving technique is Object Oriented Programming. The

entire above said problem solving strategies are given importance to procedures or

algorithms. Those strategies were forgotten about basic element of the problem that is

data. The job of computers id processing of data and produces processed data that is

information. Object oriented concept given importance to data. According to some authors

difference between procedure oriented and object oriented is very simple, which is shown

in the form of following equation.

8/3/2019 Adavanced Data Structures UNIT- I Part 1

http://slidepdf.com/reader/full/adavanced-data-structures-unit-i-part-1 4/16

ADVANCED DATA STRUCTURES

Veera Shekar G UNIT - I Page 4

+ = POPS 

Procedure Oriented

+ = OOPS 

Object Oriented

According to above equation in procedure oriented Programming structures first

preference given to algorithms and then only focus is given to Data. Where as in object

oriented programming structures first preference is given to Data, then algorithms takencare. The following table shows types of programming techniques and their main

abstractions.

Programming Model Abstractions

Procedure Oriented

Structured Programming

Object-Oriented

Logic -Oriented

Rule-Oriented

Algorithms

Subprograms

Classes and Objects

Goals

If-then-else rules

AlgorithmsDATA

DATA Algorithms

8/3/2019 Adavanced Data Structures UNIT- I Part 1

http://slidepdf.com/reader/full/adavanced-data-structures-unit-i-part-1 5/16

ADVANCED DATA STRUCTURES

Veera Shekar G UNIT - I Page 5

Object oriented programming language the name implies total language based on

objects. An object is any real world entity which is having state, identity and behavior. Fo

example, consider mouse as an object, its current state may be moving, or working , or

picking, or dropping etc. its identity may be its color, company, identification number,

three-button or rolling etc. Mouse behavior is it used for picking and pointing objects onthe screen. State represents current condition of the object, identity refers how it differs

from similar kind of objects, and behavior tells the working principle of that particular

object.

PARADIGMS OF PROGRAMMING LANGUAGES

The term paradigm describes a set of techniques, methods. Theories and standards

that together represent a way of thinking for problems solving according to [wegner

1988], paradigms are “pattern of thought for problem solving”. 

Language paradigms were associated with classes of languages. First the paradigmsare defined. Thereafter, programming languages according to different are classified.

The language paradigms are divided into two parts

1.  Imperative

2.  Declarative

Paradigms as shown in below figure Imperative languages can be further classified into

Procedural and Object oriented approach. Declarative languages can be classified into

functional and logical languages. In figure the examples of languages in each category are

shown in figure.

C, Pascal C++, small talk, java Lisp Prolog

Language paradigms

Imperative paradigm Declarative paradigm

Procedural Object oriented LogicFunctional

8/3/2019 Adavanced Data Structures UNIT- I Part 1

http://slidepdf.com/reader/full/adavanced-data-structures-unit-i-part-1 6/16

ADVANCED DATA STRUCTURES

Veera Shekar G UNIT - I Page 6

Imperative paradigm

The meaning of imperative is expressing a command or order. So the programming

languages in this category specify the step-by-step explanation of command. Imperative

programming languages describe the details of how the results are to be obtained, in

terms of the underlying machine model. The programs specify step by step the entire set

of translations that the program goes through. The program starts from an initial state

goes through the translations and reaches a final state ,within this paradigm we have the

Procedural approach and Object o riented approach.

Procedural paradigm 

Procedural languages are statement oriented with variables holding values

In this language the execution of a program is modeled as a series of statements of

variable locations. We have two kinds of statements

Non-executable statements allocate memory , bind symbolic names to

absolute memory locations, and initialize memory.

Executable statements like computation, control flow and input/output

statements.

The popular programming languages in this category are Ada, Fortran, Basic,

Algol, Pascal, Cobol, Modula, C etc.

Object oriented paradigm 

The Object oriented paradigm is centered on the concept of the object.

Everything is focused on objects. In this language program consists of two

things, first a set of objects and second the way they interact with each other.

Computation in this paradigm is viewed as the simulation of real world entities.

The popular programming languages of this paradigm are C++, simula, small talk

and java.

8/3/2019 Adavanced Data Structures UNIT- I Part 1

http://slidepdf.com/reader/full/adavanced-data-structures-unit-i-part-1 7/16

ADVANCED DATA STRUCTURES

Veera Shekar G UNIT - I Page 7

OOPS CONCEPTS

Major features of OOPS concepts are as follows:

1.  Encapsulation

2.  Data Abstraction3.  Hierarchy or Inheritance

4.  Polymorphism

Minor features of OOPS are as follows

1.  Message Passing

2.  Persistence

3.  Delegation

4.  Genericity

Before elaborating these concepts we should know about the difference betweenobject based and object oriented programming languages. Programming that supports

only encapsulation it is called as Object based programming language . An object based

programming language can be called as Object oriented programming language when it

supports inheritance and polymorphism too. Simply

OOPS

Major features

1. Encapsulation

2. Abstraction

3. Inheritance

4. Polymorphism

Minor features

1. Message passing

2. Persistence

3. Delegation

4. Genericity

8/3/2019 Adavanced Data Structures UNIT- I Part 1

http://slidepdf.com/reader/full/adavanced-data-structures-unit-i-part-1 8/16

ADVANCED DATA STRUCTURES

Veera Shekar G UNIT - I Page 8

Object based programming language = Object Concept + Encapsulation

Object Oriented Programming = Object Based + Inheritance + Polymorphism

In the material previous paragraph we have seen different problem solving

strategies and the free flow of global data. In object oriented programming language this

problem will not appear. Here each object has its own data and functions. Sharing data

between objects done through message passing mechanism. Generally global data is truly

restricted to object. The following diagram shows the object oriented model.

DATA

Function

Function

DATA

Function

Function

DATA

Function

Function

Ob ect B

Object C

Ob ect A

Message

passing

Message

passing

Message

passing

Data shared only

for Object A

8/3/2019 Adavanced Data Structures UNIT- I Part 1

http://slidepdf.com/reader/full/adavanced-data-structures-unit-i-part-1 9/16

ADVANCED DATA STRUCTURES

Veera Shekar G UNIT - I Page 9

OVERVIEW OF OOPS PRINCIPLES

Class:

Class is a blueprint for an object. Similar objects with their common behavior are

grouped together is called as Class. For example peacock, parrot, hen , robin, duck all areobject grouped together is called as birds class. The set of data and its code can be made

as a user defined type with the help of class. Once class is defined, you can create as

many numbers of objects as you want. A class is collection of objects of similar type.

Consider another example a fruit class. Objects under fruit class are mango, banana,

apple and orange. Object of fruit class is declared as follows;

Fruit apple;

More objects at a time if you want to declare you can,

Fruit mango,banana,orange

Data hiding:

Hiding the implementation details from the user is called as data hiding. To drive a

car you should know how to drive it. There is no need of knowing manufacturing details

like how many nuts and bolts used in its manufacturing.

For example, you created an object for employee class as follows.

Employee emp;

There is a function called calGross to calculate the gross salary of an employee. You

can call this function simply as follows

emp.calGross();

Implementation details and calculation details is not required for the user. Just his

purpose is to use that function and get the result. Hiding of this kind of implementation

details is called as a Data hiding.

Major features of OOPS are

1.  Encapsulation2.  Data Abstraction

3.  Inheritance

4.  Polymorphism

8/3/2019 Adavanced Data Structures UNIT- I Part 1

http://slidepdf.com/reader/full/adavanced-data-structures-unit-i-part-1 10/16

ADVANCED DATA STRUCTURES

Veera Shekar G UNIT - I Page 10

1.  Data abstraction

The essential characteristics of an object that distinguish it from all other kinds of

objects are called data abstraction. With help of data abstractions we can create new data

types that are suitable for applications. In C++,class used for creating user defined data

type that is called as Abstract Data Type(ADT). 

Essential characteristics (data and functions) depend on perspective of the viewer.

For example, a technician view of the car is different from its owner. Technician required

characteristics for a car in general working of engine, battery, condition etc. Owners view

is cost, mileage, model etc.

Simply consider a student object. A faculty view for student object is rollno, name,

date of joining, fees particulars etc.

2.  Encapsulation

Combination of data and its related functions is called encapsulation. The process

for implementing data abstractions in a programming language comes under as

encapsulation. In C++, this is supported by class. An instance of a class is known as

object. The following equation represents encapsulation

Encapsulation = Data + Operations

In more specific way ,

Encapsulation = [private] Data + [public] Operations

3.  Inheritance

The main purpose of object orientation is its reusability that can be achieved

through inheritance. Extending a existing class is called Inheritance. With the help of

inheritance, child class can get all the operations of parent class. In addition to the

existing details a child may have additional data members and operations. Concept of

inheritance is also known as class hierarchy. Inheritance allows the extension and reuse of

existing code without developing from scratch.

For example consider a player class. Further you have different player classes, like

cricket player class, football player class etc. Also cricket player class can be extended toBowler class, Batsman class , Wicket keeper class etc. Player class contains common

things required for all players like player name, country etc. Whereas cricket player class

additional parameters like runs, wickets, catches, average run-rate etc. Football player

class contains additional parameters like goals, points etc.

8/3/2019 Adavanced Data Structures UNIT- I Part 1

http://slidepdf.com/reader/full/adavanced-data-structures-unit-i-part-1 11/16

ADVANCED DATA STRUCTURES

Veera Shekar G UNIT - I Page 11

The following diagram shows this inheritance relationship.

4.  Polymorphism

Another important feature of object orientation is polymorphism. In

polymorphism, poly represents many and morph represents forms. Many forms of an

entity called polymorphism. This allows an operator or function responding in differentmanners. For example area() function may compute area of rectangle if you provide

two parameters, or compute area of circle if you have single parameter. Hence, same

area() function works in two different ways. This is called as function overloading

Other forms of polymorphism are operator overloading, dynamic binding (virtua

functions).

Data hiding, data abstraction, encapsulation, inheritance and polymorphism are

major of object orientation.

There are other minor features given below.

1.  Message passing

2.  Delegation

3.  Persistence

4.  Genericity

Player class

Cricket player Football player

BowlerBatsman

Wicket Keeper

Base Class

Derived Class

Further

DerivedClass

8/3/2019 Adavanced Data Structures UNIT- I Part 1

http://slidepdf.com/reader/full/adavanced-data-structures-unit-i-part-1 12/16

ADVANCED DATA STRUCTURES

Veera Shekar G UNIT - I Page 12

1.  Message passing

Process of invoking an operation of an object is called message passing. In

response to the given message the respective unction or operation is invoked.

2. Delegation

Communication between two objects is made possible through delegation. Here two

objects are involved in handling request, one object delegates operations to its delegate.

Delegation is realized with the help of object composition. The relationship between these

two objects is called as has-a relationship or containership.

3.  Persistence

Storing the details of object for further retrieval is called as persistence. Object data

outlives the program execution time and exists between executions of a program. An

object is called persistent object that exists after the process a thread that created it hasceased to exist.

4.  Genericity

Technique for defining functions or classes that have more than one interpretation

depending on the data type of parameters is called Genericity. Templates are helpfu

creating generic programming. These are two types of templates in C++,class templates

and function templates. 

ADVANTAGES OF OBJECT ORIENTED PROGRAMMING

1.  Simplicity: Clear view of the problem can be seen through objects. Complexity of

the programs is reduced and you can view more clear program structure in simple

diagrams.

2.  Reusability: Once  objects are defined they can reused in different programs. Fo

example calendar object is defined then it can be used in Student Application,

Administrator Application and Faculty Application etc. 

3.  Reliability: A pre-tested object is more useful and it provides more reliability.

Instead of building from the scratch, you can use tested objects so that programsreliability is high.

4.  Maintainability: Easy to maintain the entire program. Objects can be maintained

separately and very easy to identify errors. Fixing of problems is easy. 

8/3/2019 Adavanced Data Structures UNIT- I Part 1

http://slidepdf.com/reader/full/adavanced-data-structures-unit-i-part-1 13/16

ADVANCED DATA STRUCTURES

Veera Shekar G UNIT - I Page 13

Today there are several languages that are implementing object oriented features

Smalltalk is considered as pure object oriented language because in that language

everything is object. In a pure object oriented language all actions are produced by

passing message. Messages are requests made by another object or same object through

operations. C++ can be considered as hybrid language because it has both procedureoriented features along with the object oriented features. There are two reasons for

popularity of C++, one in C++ provides several constructs which fully support object

oriented features. Second reason in C++ is an extension of popular standardized language

that is C.

HISTORY OF C++

Most of traditional approaches for problem solving failed at the later stages of the

project. Once we started solving a problem, if the problem is solved within 6 months toone year then we can’t face major drawbacks of the programming language. Same thing

happens to c programming language. When it was introduced in 1970s everybody thought

that it is a general purpose programming language so it can handle any type of problem.

No other programming language grabbed popularity in the market for a decade (1973-

1983). Major problem encountered when long run large projects based on c failed in the

middle or last stages of project development. It was very difficult for programmers to

modify the entire code after identifying their flaw. So there is a need of alternative

programming language which can handle large projects. In 1970s there were two

important languages in the market with less popularity. They are simula 67 and algol 68.

In Simula 67 and Algol 68 languages key features were very important but the

problem was their basic syntax. Bjarne Strousstrup at AT & T Bell laboratories identified

the solution to find new programming language. Popular programming language C syntax

was already familiar throughout the world, so adding the concepts or key ideas of simula

67 and algol 68 with C results to that new programming language. Many of us knows that

C language was based on B language. So what should be the name of the new language?

Is it D? Strousstrup was not modifying the C language; he was adding other concepts to C

language that means it was an extension language of C. Earlier versions of this language

known as “C with classes”. He wants a new name , a new name came into his mind it was

C++. The reason behind not to put C+ language is C plus is a syntax error in the C

language. Next number to C(according to short circuit operator C= C+1) is C++, so he

confirmed the name as C++. Following gives the hierarchy of development of C++

language.

8/3/2019 Adavanced Data Structures UNIT- I Part 1

http://slidepdf.com/reader/full/adavanced-data-structures-unit-i-part-1 14/16

ADVANCED DATA STRUCTURES

Veera Shekar G UNIT - I Page 14

CLASSES

A structure groups together data elements of different types. A function groups

together program statements that perform a specific action into a named unit. A classgroups together data elements and functions that act on these data elements into a single

logical unit. A class is perhaps the most important user defined data type in C++.

A class description consists of two parts:

a)  Class definition of the component members both data and function members.

b)  Class implementation or definition of class member functions.

Class is a blueprint for an object. Similar objects with their common behavior are

grouped together is called as Class. For example peacock, parrot, hen, robin, duck all are

object grouped together is called as birds class. The set of data and its code can be madeas a user defined type with the help of class. Once class is defined, you can create as

many numbers of objects as you want.

Algol 68

CPL

BCPL

B

CC with

Classes

Simula 67

C++

8/3/2019 Adavanced Data Structures UNIT- I Part 1

http://slidepdf.com/reader/full/adavanced-data-structures-unit-i-part-1 15/16

ADVANCED DATA STRUCTURES

Veera Shekar G UNIT - I Page 15

CLASS DEFINITION

A class is a set of operations (public) and a set of data (private) representing abstract

model. C++ structures also provide the facility for holding operations along with access

specifiers. Difference between C++ structures and C++ classes is default access specifier

in C++ structure is public and default access specifier in C++ class is private. Generally if

you want to represent traditional structures use the C kind of structure model. To

represent object oriented model encapsulation it is recommended to use class way of

definition. Syntax of class is as follows:

class ClassName

{

access-specifier:

data

operations

access-specifier:

data

operations

-----

------

};

The class definition starts with keyword that follows name of the class. The body of

a class enclosed in curly braces followed by semicolon. Body of class contains data and

operations collectively known as members. Data is defined as variables and those are

called as data members and functions are defined in the body of the class are called asmember functions. All these members are headed under access-specifiers. There are three

different access specifiers in C++;they are public, private and protected.

These access specifiers define the visibility of members.

The private data members can be accessed only with in the class. The public data

members can be accessed with in the class as well as outside the class. The default access

8/3/2019 Adavanced Data Structures UNIT- I Part 1

http://slidepdf.com/reader/full/adavanced-data-structures-unit-i-part-1 16/16

ADVANCED DATA STRUCTURES

Veera Shekar G UNIT - I Page 16

specifier in the body of a class is private. So, if any access specifier is not provided the

followed members are considered as private members. At least one public operation is

required for manipulating private data in the class. A class which is totally private is

hidden from the other classes and there is no purpose of such class. There is another

access specifier that is protected that provides facility to access data members from thederived classes also.

The following declaration is an example for class Date.

class MyDate{

int day;int month;int year;

public:void setDate(int d,int m,int y)

{day=d;month=m;year=y;

}void dispDate()

{cout<<day<<” - “<<month<<” - “<<year<<endl;

}};

Generally class should be given meaningful name. MyDate represents user defined

date type. MyDate is considered as anew data type identifier, Just like int data type you

can use MyDate as a data type. Variables that are declared using MYdate class are called

as Objects. In above In above MyDate class data members’ day, month, and year are

private data members because by default class considers private members. There are two

functions defined in the class setDate() and dispDate() to initialize date and display date

respectively.

Data member names and function names in different classes may be same; always

they referred through the object instance.

Same class name can’ t be used in same program.

Data members of that class can’ t be initialized during their declaration.