0% found this document useful (0 votes)
43 views90 pages

Unit I Oopm

Uploaded by

ajaysinghkhichi5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views90 pages

Unit I Oopm

Uploaded by

ajaysinghkhichi5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 90

Chameli Devi Group of Institutions,

Indore

Department of Computer Science &


engineering

Object Oriented Programming &


Methodology (CS-305)

UNIT 1
Topics to be cover:
1. Introduction to Object Oriented Thinking,
2. Concept of Object Oriented Programming,
3. Comparison POP with OOP,
4. Features of Object oriented paradigm,
5. Merits and demerits of OO methodology,
6. Object model,
7. Elements of OOPS,
8. IO processing
Overview of Programming
 Programming :- Programming language is Artificial human
created language which translates instructions from
human readable to computer readable format.

 Art of solving computational problems by using


computational language.

 With the help of programming we can code and develop


software and computer applications.
Procedure Oriented Programming concepts
 Programming Languages like COBOL, FORTRAN and C
were based on procedure oriented Programming.

 It focuses on procedure (function) where algorithm is


needed to perform the derived computation.

 When program become larger, it is divided into function


& each function has clearly defined purpose.

 Procedure also known as a routine or subroutine


or a function, contains series of computational steps.
Typical Structure of Procedure-Oriented Program
Global data
Main function
Local data

Function-1 Function-2 Function-3

Function-4 Function-5

Function-6 Function-7 Function-8

Figure 1.1 Structure of POP


Relationship of data and functions in
procedural programming

Global Data Global Data

Function-1 Function-2 Function-3

Local Data Local Data Local Data

Figure 1.2 Relationship of data and function in POP


 POP language program uses branching statements
and procedures to execute one or other set of
instructions, depending on the result that you want.

 Data move openly around the system from function to


function.

 Procedure-oriented programming basically consists of


writing a list of instructions for the computer to follow
and organizing these instructions into groups known as
functions.

 Any Given Procedure might be called at any point during


Program execution.
Why POP is better than simple
Sequential Programming?
 Reuse the same code at different places.

 Easierway to keep track of Program flow.(Jump


and goto.)

 Ability to be strongly modular.


 Large Programs are divided into smaller Programs known
as functions.

 Most of the functions shares the global data.

 Data move openly around the system from function to


function.

 Functions transform data from one form to another,

 Employs Top-Down Approach.


Problem with Procedure Oriented
Programming?

1.The functions have unrestricted access to global data.

2. No security for data. If we declare a main function then it


can be accessed freely from any function present in the
program.

3. No better memory management.

4. Code in one application cannot be used in another


Program.

5. Time of development, testing and length of program is


more.
6. Computer languages generally have built-in data type (integer,
character, float etc.), when we try to invent a new data type or a
user-define data type, then it become very difficult to create it.

7. If we are working with Dates or Complex number then it


becomes very difficult to work with built in types.

8. Since every function has complete access to the global data or


variables, the new programmer can corrupt the data accidentally
by creating function. Similarly, if new data is to be added, all the
function needed to be modified to access the data.
Any Other Technique?

This problem is overcome with the development of


new programming technique known as object oriented
programming (OOP).

 The OOP approach eliminates weakness of POP by


combining the data and function that work on it into a
single unit called Class.

 So the data is secured from the outside world.


What are Objects?

 Objects are the basic run time entities in an object-oriented


system. They may represent a person, a place, a bank account,
a table of data or any item that the program has to handle.

 Objects are self-contained entity that consists of both data


and procedures to manipulate the data. An object has state
(data) and behavior (code). Objects can correspond to things
found in the real world.

 A car is an example of a complex object, with many attributes.


We don’t need to understand all of its internal mechanics,
what kind of engine it has, how the gas makes it run, or even
where the gas came from in order to know how to interact
with it.
Object Oriented Thinking
 Object-Oriented refers to a programming language, system
or software methodology that is built on the concepts of
logical objects.

 It works through the creation, utilization and manipulation


of reusable objects to perform a specific task, process or
objective.

 Object Oriented is a computer science concept that has


been widely implemented, specifically in programming
languages and applications/software.
 An object-oriented based system is modeled and created
through the use of objects, where each object's class
instance has specific attributes and behaviors, and the
relative methods or behaviors are called to manipulate or
utilize such a system.

 The essence of object-oriented is that each of the created


objects can be reused in the same and other programs and
applications.
Benefits of Object-Oriented Technology
 Ease of software design
 Productivity

 Easy testing, debugging, and maintenance


 It’s reusable
 More thorough data analysis, less development time, and
more accurate coding (due to OOP’s inheritance method)
 Data is safe and secure, with less data corruption (due to
hiding and abstraction).
 It’ssharable (classes are reusable and can be distributed
to other networks).
Object Oriented Programming
 Object-Oriented Programming (OOP) refers to a type of
computer programming (software design) in which
programmers define the data type of a data structure, and
also the types of operations (functions) that can be
applied to the data structure.

 The main aim of OOP is to bind together the data and the
functions that operate on them so that no other part of
the code can access this data except that function.

 Its benefits include reusability, extensibility, effective


problem solving and efficiency.
Class, Object, Abstraction, Polymorphism, Inheritance and
Encapsulation are the basic concepts of Object Oriented
Programming also called as OOPs Concepts.

Figure 1.3 OOPs Concepts


The most popular OOP languages are:

 Java,
 JavaScript,
 Python,
 C++,
 Visual Basic,
 .NET,
 Ruby,
 PHP etc.
Advantages of OOP

1) Emphasis is on data rather than procedure.

2) Programs are divided in objects.

3) Data structures are designed such that they characterize


the objects.

4) Data is hidden and cannot be accessed by external


functions.
Advantages of OOP

5) Objects may communicate with each other through


function.

6) New data and functions can be easily added whenever


necessary.

7) Follows bottom-up approach in program design.

8) It follows the concept of Reusability, means reusing some


facilities rather than building it again and again.
Disadvantages of OOP

1. Larger Program size: OOP typically involves more lines of


code than POP.

2. Slower programs: Because they require more instructions to


be executed.

3. Not suitable for all types of program.

4. To convert a real world problem into an object oriented


model may be difficult.

5. Polymorphism and dynamic binding requires more


processing time, due to overload of function call during run
time.
Difference between Top Down and Bottom Up approach

S
no. Top Down approach Bottom Up approach
The top-down approach focuses The bottom-up approach first
on breaking down a big problem focuses on solving the smaller
1. into smaller and understandable problems at the fundamental level
chunks and then integrating them into a
whole and complete solution.
Mainly used by structured Mainly used by object oriented
2. programming language such as programming language such as C++,
COBOL, FORTAN, C etc. C#, Python.
Each part is programmed Redundancy is minimized by using
3. separately therefore contain data encapsulation and data hiding.
redundancy.
In top down approach, In bottom up approach composition
4. decomposition takes place. takes place.
Difference between POP and OOP
S Procedure Oriented Programming Object Oriented Programming
no.
In POP, program is divided into In OOP, program is divided into
1 small parts called functions. parts called objects.
In POP, Importance is not given In OOP, Importance is given to
to data but to functions as well the data rather than procedures
2 as sequence of actions to be or functions because it works as
done. a real world.

3 POP follows Top Down approach. OOP follows Bottom Up approach.


POP does not have any access OOP has access specifiers named
4 specifiers. Public, Private, Protected, etc.

In POP, Data can move freely In OOP, objects can move and
5 from function to function in the communicate with each other
system. through member functions.
Difference between POP and OOP
S Procedure Oriented Programming Object Oriented Programming
No.
To add new data and function in OOP provides an easy way to add
6 POP is not so easy. new data and function.
In POP, Most function uses Global In OOP, data cannot move easily
data for sharing that can be from function to function, it can
7 accessed freely from function to be kept public or private so we can
function in the system. control the access of data.

POP does not have any proper way OOP provides Data Hiding so
8 for hiding data so it is less secure. provides more security.

In POP, Overloading is not possible. In OOP, overloading is possible in


9 the form of Function Overloading
and Operator Overloading.
Example of POP is: C, COBOL, Example of OOP is: C++,
10 FORTRAN, and Pascal. JAVA, VB.NET, C#.NET.
Features of Object Oriented paradigm

Object-oriented programming uses objects in programming.

Object-oriented programming aims to implement real-world


entities like inheritance, hiding, polymorphism, etc in
programming.

The main aim of OOP is to bind together the data and the
functions that operate on them so that no other part of
the code can access this data except that function.
Features of Object Oriented paradigm

Features of OOP are:

1. Objects
2. Class
3. Encapsulation
4. Abstraction
5. Polymorphism
6. Inheritance
7. Dynamic Binding
8. Message Passing
1. Objects
 An Object is an identifiable entity with some characteristics and
behavior. An Object is an instance of a Class. When a class is
defined, no memory is allocated but when it is instantiated (i.e. an
object is created) memory is allocated.

class person
{
char name[20];
int id;
public:
void getdetails()
};
int main()
{
person p1; // p1 is a object
}
2. Class
 Itis a user-defined data type, which holds its own data
members and member functions, which can be accessed
and used by creating an instance of that class.

A class is like a blueprint for an object. For Example:


Consider the Class of Cars.

 There may be many cars with different names and brand


but all of them will share some common properties like all
of them will have 4 wheels, Speed Limit, Mileage range etc.
So here, Car is the class and wheels, speed limits, mileage
are their properties.
Differences between Object and Class
S
no.
Object Class

1. An object is the instance of A class is a blueprint from which


the class, which helps to you can create the instance, i.e.,
use variables and methods. objects.
2. Object acts like a variable A class is used to bind data as well
of the class. as methods together as a single
unit.
3. Objects have a physical Classes have logical existence.
existence.
4. An object takes memory When a class created, it doesn't
when a programmer take any memory spaces.
creates one.
Differences between Object and Class
S
no
. Object Class

5. Objects can be declared The class has to be declared only


several times depending on once.
the requirement.
6. It is created with a Class is declared by using class
class name in C++ keyword.
and with
the new keywords in
Java.
7. Object is a real-world A class is a group of similar
entity such as book, objects.
car, etc.
3. Encapsulation
 Encapsulation is defined as wrapping up of data and
information under a single unit.
 In Object-Oriented Programming, Encapsulation is defined
as binding together the data and the functions that
manipulate them.

Figure 1.4 Encapsulation


4. Abstraction
 Abstraction means displaying only essential information
and hiding the details.

 Data abstraction refers to providing only essential


information about the data to the outside world, hiding
the background details or implementation.

 Consider a real-life example of a man driving a car. The


man only knows that pressing the accelerators will
increase the speed of the car or applying brakes will stop
the car but he does not know about how on pressing
accelerator the speed is actually increasing, he does not
know about the inner mechanism of the car or the
implementation of accelerator, brakes etc in the car.
5. Polymorphism
 The word polymorphism means having many forms.

 In simple words, we can define polymorphism as the


ability of a message to be displayed in more than one
form.

A person at the same time can have different


characteristic.

 Like a man at the same time is a father, a husband, an


employee. So the same person posses different behavior
in different situations.
6. Inheritance
 Inheritance is a process in which one object acquires all
the properties and behaviors of its parent object
automatically.

 In such way, you can reuse, extend or modify the


attributes and behaviors which are defined in other class.

 In C++, the class which inherits the members of another


class is called derived class and the class whose members
are inherited is called base class. The derived class is the
specialized class for the base class.
Inheritance provide ability to use existing class to
create new one.
7. Binding
 Binding refers to the process of converting identifiers (such as
variable and performance names) into addresses. Binding is
done for each variable and functions.

 For functions, it means that matching the call with the right
function definition by the compiler. It takes place either at
compile time or at runtime.
Static-Binding:

 If function calling is known at compile time, called as


Static binding.
 By default, matching of function call with the correct
function definition happens at compile time. This is
called static binding or early binding or compile-time
binding.
 Static binding is achieved using function overloading
and operator overloading. Even though there are two
or more functions with same name, compiler uniquely
identifies each function depending on the parameters
passed to those functions.
Dynamic-Binding:

 Iffunction calling is known at run time, called it as


Dynamic Binding.

 C++ provides facility to specify that the compiler should


match function calls with the correct definition at the run
time; this is called dynamic binding or late binding or run-
time binding.

 Dynamic binding is achieved using virtual functions.


8. Message Passing
 Objects communicate with one another by sending and
receiving information to each other.

A message for an object is a request for execution of a


procedure and therefore will invoke a function in the
receiving object that generates the desired results.

 Message passing involves specifying the name of the


object, the name of the function and the information to be
sent.
A Real-World Example

Let's say that you are working for a vehicle parts


manufacturer that needs to update its online inventory
system. Your boss tells you to program two similar but
separate forms for a website, one form that processes
information about cars and one that does the same for
trucks.
• For cars, we will need to record the following information:
Color, Engine Size, Transmission Type, and Number of
doors.
• For trucks, the information will be similar, but slightly
different. We need: Color, Engine Size, Transmission Type,
Cab Size, and Towing Capacity.
Scenario 1: Suppose that we suddenly need to add a bus
form that records the following information: Color,
Engine Size, Transmission Type, Number of passengers.

• Procedural: We need to recreate the entire form,


repeating the code for Color, Engine Size, and
Transmission Type.

• OOP: We simply extend the vehicle class with a bus class


and add the method, number of Passengers.
Scenario 2: We want to move from a generic car to specific
makes, for example: Nissan.

• Procedural: We create a new form for each make,


repeating all of the code for generic car information and
adding the code specific to each make.

• OOP: We extend the car class with a Nissan class and add
methods for each set of unique information for that car
make.
Scenario 3: We found a bug in the transmission type area
of our form and need to fix it.

• Procedural: We open and update each form.

• OOP: We fix the transmission Type method in the


vehicle class and the change occurred in every class
that inherits from it.
Object Model
 An object model is a logical interface, software or system
that is modeled through the use of object-oriented
techniques.

 It enables the creation of an architectural software or


system model prior to development or programming.

 An object model is part of the object-oriented


programming (OOP) lifecycle.

 An object model helps describe or define a


software/system in terms of objects and classes.
It defines the interfaces or interactions between different
models, inheritance, encapsulation and other object-
oriented interfaces and features.

Types of Object models:

1. Document Object Model (DOM)


2. Component Object Model (COM)
1. Document Object Model (DOM)

A set of objects that provides a modeled representation of


dynamic HTML and XHTML-based Web pages.

 The Document Object Model (DOM) is a programming


interface for HTML and XML (Extensible markup language)
documents.

 Itdefines the logical structure of documents and the way a


document is accessed and manipulated.
 The Document Object Model (DOM) is a cross-platform
and language-independent interface that treats an XML or
HTML document as a tree structure wherein each node is
an object representing a part of the document.

 The DOM represents a document with a logical tree.

 Each branch of the tree ends in a node, and each node


contains objects.
Benefits of DOM:

 DOM is a way to represent the webpage in the structured


hierarchical way so that it will become easier for
programmers.

 With DOM, we can easily access and manipulate tags, IDs,


classes, Attributes or Elements using commands or
methods provided by Document object.

 DOM is a platform and language-neutral interface that


allows programs and scripts to dynamically access and
update the content, structure, and style of a document.
The Component Software Problem

 Themost fundamental problem that COM solves is: How


can a system be designed so that binary executables
from different vendors, written in different parts of the
world and at different times, are able to interoperate?
 To solve this problem, we must first find answers to
these four questions:

1. Basic interoperability. How can developers create their


own unique binary components, yet be assured that
these binary components will interoperate with other
binary components built by different developers?
2. Versioning: How can one system component be
upgraded without requiring all the system components
to be upgraded?

3. Language independence: How can components written


in different languages communicate?

4. Transparent cross-process interoperability: How can


we give developers the flexibility to write components to
run in-process or cross-process, and even cross-network,
using one simple programming model?
2. Component Object Model (COM)

 Component Object Model (COM) is a binary-


interface standard for software components.

 Introduced by Microsoft in 1993.

 It is used to enable inter-process communication.

 COM is a binary standard, it applies after the code in


programming language is converted to machine code.
 COM defines a binary standard for exchanging code
between two systems, regardless of the OS or
programming language.

 COM provides access to distributed client object services


and is used to share cross-platform binary code and
programming languages.

 COM interactivity occurs via interfacing with expandable


COM software components that do not have an impact on
underlying implementation objects.
COM fundamentals

1) Virtual Function tables

2) Interfaces and IUnknown interface

3) GUIDs

4) Reference counting

5) Loading and Unloading Components


1. Virtual Function Tables

 Any language, supporting function call via a pointer, can


be used to write COM components.

 COM has a standard way to put virtual function tables in


memory which store function pointers and at runtime
functions are called through pointers in tables.
2. Interfaces and IUnknown interface

 COM components always interact with each other


through interfaces which are a group of function and not
components or classes.
 COM components never have direct access to the object’s
data, they can only interact through interfaces.

 Iunknown is the base interface implemented by all COM


components which provides functionality to query
interface and adding and deleting references.

 Clients use QueryInterface method to request the


interface available with COM component.
3. GUIDs

 For every COM interface and class there is a identifier


assigned, which is called GUID.

 GUIDs are 128 bit integer that are guaranteed to be


unique (Globally Unique IDs) in the universe, to identify
every COM interface (IIDs) and every COM component
class (CLSIDs).

 Microsoft supplies a tool uuidgen.exe which generates


GUIDs.
4. Reference counting

 COM components are reference counted through AddRef


and Release functions.

 AddRef method is called when another COM component


no longer requires use of that interface.

 The COM components remains in memory until


reference count is non-zero; when the reference count
becomes zero, the COM components can safely unload
itself.
5. Loading and Unloading Components

 Microsoft provides a com library as part of Windows


Operating System which facilitates loading and unloading
of COM components and also connections between
loaded components.

 COM library is basically a set of DLLs and EXEs which


provides mechanisms for creation of COM components,
Remote Procedural calls, memory management etc.

 When application wants to use a COM component, COM


library uses CLSIDs to look up for COM components to be
loaded.
A client that needs to communicate with a component
in another process has to use some form of inter-
process communication provided by the operating
system.

 COM provides this communication in a completely


transparent fashion:
It intercepts calls from the client and forwards
them to the component in another process.
 LPC(Local Procedure Call) is used for inter-process
communication.

 COM uses DCE’s (Distributed Computing Environment


specified by Open Software Foundation) RPC (Remote
Procedure Call) protocol.
7. Elements of OOP

The conceptual framework of Object–Oriented systems is


based upon the object model.
There are two categories of elements in an object-oriented
system:

1. Major Elements

2. Minor Elements
1. Major Elements− By major, it is meant that if a model
does not have any one of these elements, it ceases to be
object oriented.

The four major elements are −

A. Abstraction

B. Encapsulation

C. Modularity

D. Hierarchy
A. Abstraction:

Abstraction means to focus on the essential features of an


element or object in OOP, ignoring its extraneous or
accidental properties.

The essential features are relative to the context in which


the object is being used.
B. Encapsulation:

Encapsulation is the process of binding both attributes and


methods together within a class.

Through encapsulation, the internal details of a class can be


hidden from outside.

The class has methods that provide user interfaces by which


the services provided by the class may be used.
C. Modularity:

Modularity is the process of decomposing a problem


(program) into a set of modules so as to reduce the
overall complexity of the problem.
D. Hierarchy:

“Hierarchy is the ranking or ordering of abstraction”.

Through hierarchy, a system can be made up of interrelated


subsystems, which can have their own subsystems and
so on until the smallest level components are reached.

It uses the principle of “divide and conquer”. Hierarchy


allows code reusability.
2. Minor Elements− By minor, it is meant that these
elements are useful, but not indispensable part of the
object model.

The three minor elements are −

A. Typing

B. Concurrency

C. Persistence
A. Typing:

According to the theories of abstract data type, a type is a


characterization of a set of elements.

In OOP, a class is visualized as a type having properties


distinct from any other types.

Typing is the enforcement of the notion that an object is an


instance of a single class or type.
B. Concurrency:

Concurrency in operating systems allows performing


multiple tasks or processes simultaneously.

When a single process exists in a system, it is said that there


is a single thread of control.
C. Persistence:

An object occupies a memory space and exists for a


particular period of time.

In traditional programming, the lifespan of an object was


typically the lifespan of the execution of the program that
created it.

This property by which an object continues to exist even


after its creator ceases to exist is known as persistence.
8. IO processing
 C++ IO are based on streams, which are
sequence of bytes flowing in and out of the
programs (just like water and oil flowing
through a pipe).

 In input operations, data bytes flow from


an input source (such as keyboard, file,
network or another program) into the
program.

 In output operations, data bytes flow from the


program to an output sink (such as console,
file, network or another program).
 Streams acts as an intermediaries between
the programs and the actual IO devices, in
such the way that frees the programmers
from handling the actual devices.

 C++ IO is provided in
headers <iostream> (which
included <ios>, <istream>, <ostream> and
<streambuf>), <fstream> (for file IO),
and <sstream> (for string IO).
 C++ provides both
the formatted and unformatted IO functions.

 In formatted or high-level IO, bytes are grouped


and converted to types such as int, double,
string or user-defined types.

 In unformatted or low-level IO, bytes are


treated as raw bytes and unconverted.

 Formatted IO operations are supported via


overloading the stream insertion (<<) and
stream extraction (>>) operators, which
presents a consistent public IO interface.
1. Input streams

 Input streams are used to hold input from a data


producer, such as a keyboard, a file, or a network.

 For example, the user may press a key on the keyboard


while the program is currently not expecting any input.

 Rather than ignore the users key press, the data is put
into an input stream, where it will wait until the program
is ready for it.
2. Output streams

 Output streams are used to hold output for a particular


data consumer, such as a monitor, a file, or a printer.

 When writing data to an output device, the device may


not be ready to accept that data yet - for example, the
printer may still be warming up when the program writes
data to its output stream.
Figure 1.5 IO Stream

Figure 1.5 IO Processing

You might also like