introduction to object oriented programming 1.1 structured...

9
CS1050 --------------------------------Gayani Gupta---------------------------------------------------------------------1 Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTION Structured programming (known as modular programming) is a subset of procedural programming that enforces a logical structure in the programming being written, to make it more efficient and easier to understand and modify. Structured programming frequently employs a top-down design model, in which developers map out the overall program structure into separate subsections. A defined function or a set of similar functions coded in separate modules can be reused in other programs. After a module has been tested individually, it is then integrated with other modules into the overall program structure. Program flow follows a simple hierarchical model that employs looping constructs such as 'for,' 'repeat' and 'while.' Use of the 'Go To' statement is discouraged in structured programming. Structured programming was first suggested by the mathematicians Corrado Bohm and Guiseppe Jacopini. They demonstrated that any computer program can be written with just three structures: decision, sequences and loops. In structured programming coders break larger pieces of code into shorter subroutines (functions, procedures, methods, blocks or otherwise) that are small enough to be understood easily. In general, programs should use local variables and take arguments by either value or reference. These techniques help to make isolated small pieces of code easier to understand the whole program at once. PASCAL, Ada and C are some of the examples of structured programming languages.

Upload: buidung

Post on 01-Aug-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTIONgayanigupta.com/Notes/Java/61.pdf · Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTION

CS1050 --------------------------------Gayani Gupta---------------------------------------------------------------------1

Introduction to Object Oriented Programming

1.1 STRUCTURED INTRODUCTION

Structured programming (known as modular programming) is a subset of procedural programming that enforces a logical structure in the programming being written, to make it more efficient and easier to understand and modify.

Structured programming frequently employs a top-down design model, in which developers map out the overall program structure into separate subsections.

A defined function or a set of similar functions coded in separate modules can be reused in other programs.

After a module has been tested individually, it is then integrated with other

modules into the overall program structure. Program flow follows a simple hierarchical model that employs looping constructs such as 'for,' 'repeat' and

'while.' Use of the 'Go To' statement is discouraged in structured programming.

Structured programming was first suggested by the mathematicians Corrado

Bohm and Guiseppe Jacopini. They demonstrated that any computer program can be written with just three structures: decision, sequences and loops.

In structured programming coders break larger pieces of code into shorter subroutines (functions, procedures, methods, blocks or otherwise) that are small enough to be understood easily.

In general, programs should use local variables and take arguments by

either value or reference. These techniques help to make isolated small pieces of code easier to understand the whole program at once.

PASCAL, Ada and C are some of the examples of structured programming languages.

Page 2: Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTIONgayanigupta.com/Notes/Java/61.pdf · Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTION

CS1050 --------------------------------Gayani Gupta---------------------------------------------------------------------2

1.1.1 Sequence Structure

A sequence structure consists of a single entry and single exit statement, i.e., it makes a sequential flow .

1.1.2 Loop or Iteration Structure

The loop consists of a sequence of statements, which are executed based on the logical condition.

1.1.3 Decision Structure

Decision structure consists of a condition which may be true or false. Depending upon the condition, different branches are taken and executed .

1.2 PROCEDURAL PROGRAMMING

Procedural programming is a programming paradigm based upon the concept

of the procedural call. Procedures, also known as routines, subroutines, methods or functions, simply contain a series of computational steps to be carried out.

Any given procedure can be called at any point during a program's execution including other procedures or itself. Especially in large, complicated

programs, modularity is desirable. It can be achieved using procedures that have strictly defined channels for input and output and also clear rules about

what types of input and output are allowed or expected. Inputs are usually specified syntactically in the form of arguments and the outputs delivered as return values.

To be considered a procedural, a programming language should support procedural programming by having an explicit concept of a

procedure and syntax to define it. It should ideally support specification of argument types, local variables, recursive procedure calls and use of procedures in separately built program constructs. It may also support distinction of input and output arguments.

C, ALGOL are the classic examples of procedural programming language.

Characteristics of Procedure-oriented Programming:

1. Top-down approach is followed.

2. Data is given less importance than function.

Page 3: Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTIONgayanigupta.com/Notes/Java/61.pdf · Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTION

CS1050 --------------------------------Gayani Gupta---------------------------------------------------------------------3

3. Vulnerability of data is there, as functions share global data.

4. Functions manipulate global data, without letting other functions to know.

5. Big program is divided into small modules.

6. Algorithms are designed first without bothering about minute details.

1.3 PROGRAMMING METHODOLOGY

Bottom-up and Top-down Approaches

Any problem can be dealt with two ways, namely top-down or bottom-up. A simple example is given in to illustrate the concept.

Sorting an array of numbers involves the following:

1. Comparison

2. Exchange

In top-down approach, at the top level, an algorithm has to be formulated to carry out sorting using the above operations. Once the algorithm is

confirmed, then the algorithms for comparison and exchange are formulated,

before implementation of the entire algorithm. Therefore, in this

Page 4: Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTIONgayanigupta.com/Notes/Java/61.pdf · Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTION

CS1050 --------------------------------Gayani Gupta---------------------------------------------------------------------4

approach one begins from the top level without bothering about

the minute details for implementation, to start with.

The bottom-up approach is just the reverse. The lower level tasks are first carried out and are then integrated to provide the solution. In

this method lower level structures are carried out. Here the

algorithms for exchange and comparison are formulated before formulating the algorithms for the whole problem.

In any case, dividing the problem into small tasks and then solving each task provides the solution. Therefore, either the top-down or bottom-up

methodology has to be adopted for dividing the problem into smaller modules and then solving it. In the top-down methodology, the overall structure is defined before getting into the details, but in the bottom-up

approach, the details are worked out first before defining the overall structure.

Points About Bottom-up Approach

1. In bottom-up design, individual parts of the system are specified in detail. The parts are then linked together to form larger

components, which are in turn linked until a complete system is formed.

2. Bottom-up design yield programs which are smaller and more agile. A shorter program does not have to be divided into many

components, and fewer components means programs which are easier to read or modify.

3. Bottom-up design promotes code reusability. When you write two or more programs, many of the utilities you wrote for the first program

will also be useful in the succeeding ones. That is why reusability of code is one of the main benefits of the bottom-up approach.

4. Bottom-up design makes programs easier to read.

5. Working bottom-up helps to clarify your ideas about the design of your program.

6. Bottom-up programming may allow you for unit testing, but until most of the system comes together none of the system can tested

as a whole, often causing compilations near the end of the project.

7. Examples of programming which use this approach are C++ and

Java.

Page 5: Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTIONgayanigupta.com/Notes/Java/61.pdf · Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTION

CS1050 --------------------------------Gayani Gupta---------------------------------------------------------------------5

Points About Top-down Approach

1. In the top-down model, an overview of the system is formulated,

without going into the detail for any part of it. Each part of the system is then refined by designing it in more detail.

2. Each new part may then be refined again, defining it in yet more

detail until the entire specification is detailed enough to validate the model.

3. Top-down approaches emphasize planning and a complete understanding of the system. It is inherent that no coding can begin

until a sufficient level of detail has been reached in the design of at least some part of the system.

4. Top-down programming is a programming style, the mainstay of traditional procedural languages, in which design begins by

specifying complex pieces and then dividing them into successively smaller pieces.

5. The technique for writing a program using top-down methods is to write a main procedure that have been coded the program is done.

6. Top-down programming may complicate testing, since nothing executable will even exist until near the end of the project.

7. Examples of programming which use this approach are C and Pascal.

1.5 BASIC CONCEPTS OF OOPS

The following are the basic concepts applied in object-oriented programming language.

1. Class and object 2. Encapsulation

3. Abstraction 4. Data hiding

5. Polymorphism 6. Inheritance

7. Dynamic binding 8. Message passing

1. Class and object: A class is termed as a basic unit of encapsulation. It is

a collection of function code and data, which forms the basis of object-

oriented programming. A class is an abstract data type (ADT), i.e., the class definition only provides the logical abstraction. The data and function defined within the class, spring to life only when a variable of type class is created.

The variable of type class is called an object which has a physical existence

Page 6: Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTIONgayanigupta.com/Notes/Java/61.pdf · Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTION

CS1050 --------------------------------Gayani Gupta---------------------------------------------------------------------6

and also known as instance of class. From one class, several objects can be created. Each object has similar set of data defined in the class and it can use functions defined in the class for the manipulation of data.

For example: We can create a class car which may have properties such as company, model, year of manufacture, fuel type, etc., and which may have actions such as acceleration (), brake () etc.

Objects are the basic run time entity in a C++ program. All objects are instances of a class. Depending upon the type of class, an object may

represent anything such as a person, mobile, chair, student, employee, book, lecturer, speaker, car, vehicle or anything which we see in our daily life. The

state of an object is determined by the data values they are having at a particular instance. Objects occupy space in memory, and all objects share

same set of data items which are defined when class is created. Two objects may communicate with each other through functions by passing messages.

In layman's terms:

Animal can be stated as a class and lion, tiger, elephant, wolf, cow , etc., are its objects.

Bird can be stated as class and sparrow, eagle, hawk, pigeon, etc.,

are its objects.

Musician can be stated as a class and Himesh Reshmia, Anu Malik,

Jatin-Lalit are its objects.

2. Encapsulation: Encapsulation is the mechanism that binds together function and data in one compact form, known as class .The data and

function may be private or public. Private data/function can be accessed only within the class. Public data/code can be accessed outside the class. The use

of encapsulation hides complexity from the implementation. Linking of function code and data together gives rise to objects which are variables of type class.

3. Abstration: Abstraction is a mechanism to represent only essential

features which are of significance and hides the unimportant details. To make a good abstraction we require a sound knowledge of the problem domain,

which we are going to implement using OOP principle. As an example of the abstraction consider a class Vehicle . When we create the Vehicle class, we

can decide what function code and data to put in the class such as vehicle name, number of wheels, fuel type, vehicle type, etc., and functions such as

changing the gear, accelerating/decelerating the vehicle. At this time we are not interested in vehicle works such as how acceleration, changing gear

takes place. We are also not interested in making other parts of the vehicle to be part of the class such as model number, vehicle color, etc.

Page 7: Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTIONgayanigupta.com/Notes/Java/61.pdf · Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTION

CS1050 --------------------------------Gayani Gupta---------------------------------------------------------------------7

4. Data hiding: Data hiding hides the data from external access by the user. In OOP language, we have special keywords such as private, protected, etc., which hides the data.

5. Polymorphism: If we bifurcate the word polymorphism we get 'poly', which means many and 'morphism', which means form .

Thus, polymorphism means more than one form. Polymorphism provides a way for an entity to behave in several forms.

It is an attribute that allows one interface to control access to a general class of actions. For example, we want to find out maximum of three

numbers; no matter what type of input we pass, i.e., integer, float, etc. Because of polymorphism we can define three variable versions of the same

function with the name max3. Each version of this function takes three parameters of the same time, i.e., one version of max3 takes three

arguments of type integer, another takes three arguments of type double and so on. The compiler automatically selects the right version of the

function depending upon the type of data passed to the function max3. This is also termed as function polymorphism or function overloading.

The two types of polymorphism are the following: (a) Compile time polymorphism , (b) Run time polymorphism

6. Inheritance: Inheritance is the mechanism of deriving a new class from the earlier existing class. The inheritance provides the basic idea of

reusability in object-oriented programming. The new class inherits the features of the old class. The old class and new class is called (given as pair) base-derived, parent-child, super-sub.

Examples:

1. In the example given below, we have a vehicle class at the top in

hierarchy. All the common features of a vehicle can be put in this

class. From this, we can derive a new class, i.e., two wheeler, which contains features specific to two-wheeler vehicles

only.

2. As another example we can take an engineering college as the top

class and its various departments such as computer, electronics, electrical , etc., as the sub classes. The university to which the

engineering college is affiliated may be its parent class.

Page 8: Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTIONgayanigupta.com/Notes/Java/61.pdf · Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTION

CS1050 --------------------------------Gayani Gupta---------------------------------------------------------------------8

7. Dynamic binding: Binding means linking. It involves linking of function definition to a function call.

1. If linking of function call to function definition, i.e., a place where

control has to be transferred is done at compile time, it is known as

static binding.

2. When linking is delayed till run time or done during the execution of

the program then this type of linking is known as dynamic binding. Which function will be called in response to a function call is find out

when program executes.

8. Message passing: In C++, objects communicate each other by passing

messages to each other. A message contains the name of the member function and arguments to pass. The message passing is shown below:

object. method (parameters);

Message passing here means object calling the method and passing

parameters. Message passing is nothing but calling the method of the class and sending parameters. The method in turn executes in response to a message.

1.6 CHRACTERISTICS OF OOPS

1. Programs are divided into classes and functions.

2. Data is hidden and cannot be accessed by external functions.

3. Use of inheritance provides reusability of code.

4. New functions and data items can be added easily.

5. Data is given more importance than functions.

6. Follows bottom-up approach.

7. Data and function are tied together in a single unit known as class.

8. Objects communicate each other by sending messages in the form of function.

1.7 ADVANTAGES OF OOPS

1. Code reusability in terms of inheritance.

2. Object-oriented system can be easily upgraded from one platform to another.

Page 9: Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTIONgayanigupta.com/Notes/Java/61.pdf · Introduction to Object Oriented Programming 1.1 STRUCTURED INTRODUCTION

CS1050 --------------------------------Gayani Gupta---------------------------------------------------------------------9

3. Complex projects can be easily divided into small code functions.

4. The principle of abstraction and encapsulation enables a

programmer to build secure programs.

5. Software complexity decreases.

6. Principle of data hiding helps the programmer to design and develop safe programs.

7. Rapid development of software can be done in short span of time.

8. More than one instance of same class can exist together without any

interference.

1.8 OBJECT-ORIENTED LANGUAGES

Some of the most popular object-oriented programming languages are:

1. C++

2. Java 3. smalltalk

4. Eiffle 5. Ruby

6. Delphi 7. Charm++ 8. Simula

1.9 OBJECT-BASED LANGUAGES

The language which only concerns with classes and objects and do not have features such as inheritance, polymorphism, encapsulation (do not satisfy the

PIE principle) is known as object-based languages. In these types of languages you can create classes and object and can work with them. They

usually have a large number of built-in objects of various types. Some of the languages which are object based are Java script, Visual Basic, etc.