0% found this document useful (0 votes)
39 views11 pages

Oopm (Cs305) Unit-1 Notes

Uploaded by

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

Oopm (Cs305) Unit-1 Notes

Uploaded by

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

OOP&M CS 305 UNIT I

Mahakal Institute of Technology, Ujjain


Object Oriented Programming & Methodology (CS-305)
Class Notes
UNIT- I

Syllabus: Introduction to Object Oriented Thinking & Object-Oriented Programming: Comparison with
Procedural Programming, features of Object-oriented paradigm– Merits and demerits of OO
methodology; Object model; Elements of OOPS, IO processing.

Introduction to Object Oriented Thinking

Object oriented methodology provides a new way of viewing the real-world situations. 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. The object-oriented technique is different from conventional programming, which focuses on
functions/behaviors, while object-oriented works on the interactions of one or more objects.

What is abstract interaction?


If you want to change the television channel from your seat, you use a remote control. That remote control
is an object with a number of attributes and behaviors hidden inside of it. Without an understanding of
those hidden attributes the microchips, wiring, etc., you still know and expect that pressing a button will
perform that particular function. You’ve interacted with the remote control in the abstract, skipping the
steps the remote was designed to carry out. That’s the beauty of OOP the focus is on how the objects behave,
not the code required to tell them how to behave. Figure 1.1 shows the concept of abstract interaction.

Figure 1.1: Abstract interaction

What are Objects?


An object is a component of a program that knows how to perform certain actions and how to interact with
other elements of the program. Objects are the basic units of object-oriented programming. A simple
example of an object would be a person. Logically, you would expect a person to have a name. This would
be considered a property of the person. You could also expect a person to be able to do something, such as
walking or playing. This would be considered a method of the person.

1
OOP&M CS 305 UNIT I

Figure 1.2: Object in OOP

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. The car’s behaviors have been made simple for us through object-oriented
logic: put the key in the ignition, and the car turns on and gets us where we need to go. The attributes that
make this possible—all of the car’s parts, electronics, and engineering—are a “package” we don’t need to
break down in order to understand.

Object-Oriented Programming
Object-Oriented programming, or OOP, is an approach to problem solving where all computations are
carried out using objects. Code in object-oriented programming is organized around objects. Once you have
your objects, they can interact with each other to make something happen.

Let's say you want to have a program where a person gets into a car and drives it from A to B. You would start
by describing the objects, such as a person and car. That includes methods: a person knows how to drive a
car, and a car knows what it is like to be driven. Once you have your objects, you bring them together so the
person can get into the car and drive.

In OOP, concepts of objects and classes came into existence. The whole program is written in a class
containing different objects and a number of member functions. The main reason behind the OOP is that
the developers can use real-world entities in a program.

In modern software development, object-oriented programming is commonly used for writing huge &
complex software. Object-oriented programming was developed as a method for reducing complexity in
software development, making it easier to build and scale large software programs.

2
OOP&M CS 305 UNIT I

Characteristics of Object-Oriented Programming

Object

Overloading Class

OOP
Inheritance Abstraction

Encapsulation Polymorphism

Figure 1.3: OOP Characteristics

1) Object and Class


Each object is identified by a unique name. Each object must be a member of a particular class. When you
define a class, you define a blueprint for an object. This doesn't define any data, but it does define what the
class name means, that is, what an object of the class will consist of and what operations can be performed
on such an object.

2) Abstraction and Encapsulation


Abstraction is the method of hiding the unwanted information. Encapsulation is placing the data and the
functions that work on that data in the same place. While working with procedural languages, it is not always
clear which functions work on which variables but object-oriented programming provides you a framework
to place the data and the relevant functions together in the same object.

Encapsulation

Data Methods

Figure 1.4: Encapsulation


3) Inheritance
One of the most useful aspect of object-oriented programming is code reusability. Inheritance is the process
of forming a new class from an existing class that is from the existing class called as a base class, a new class
is formed called as derived class. This is a very important concept of object-oriented programming since this
feature helps to reduce the code size.

3
OOP&M CS 305 UNIT I

Figure 1.5: Inheritance


We have the following types of inheritance:
 Single Inheritance
 Multiple Inheritance
 Multilevel Inheritance
 Hierarchical Inheritance
 Hybrid Inheritance

4) Polymorphism
The ability to use an operator or function in different ways in other words giving different meaning or
functions to the operators or functions is called polymorphism. Poly refers to many. That is a single function
or an operator functioning in many ways different upon the usage is called polymorphism.

VEHICLE

FORMS

2-WHEELER 4-WHEELER
3-WHEELER

Figure 1.6: Polymorphism


Types of Polymorphism
 Compile-time polymorphism
 Runtime polymorphism
5) Overloading
The concept of overloading is also a branch of polymorphism. When the existing operator or function is made
to operate on new data type, it is said to be overloaded.

Figure 1.7: Overloading

4
OOP&M CS 305 UNIT I

Benefits of Object-Oriented technology include:


 Ease of software design
 High Productivity
 Easy testing, debugging, and maintenance
 It’s reusable
 More thorough data analysis, less development time, and more accurate coding (due to inheritance)
 Data is safe and secure, with less data corruption, thanks to hiding and abstraction

Procedure Oriented Programming (POP)


Procedural Oriented Programming is one of the programming methods where the main focus is on functions
or procedures required for computation, instead of data. The program is divided into functions, and the task
is done sequentially. These functions share the global data or variables, and there is an exchange of data
among those functions.

Figure 1.8: POP working

Characteristics of procedure-oriented programming language:


1. Emphasis on doing things (algorithm).
2. Large programs are divided into smaller programs known as functions.
3. Function can communicate by global variable.
4. Data move freely from one function to another function.
5. Functions change the value of data at any time from any place. (Functions transform data from one
form to another.)
6. It uses top-down programming approach.

Comparison between Procedure Oriented Programming (POP) & Object-Oriented Programming (OOP)

Table 1.1: - POP VS OOP approach


S No Object-Oriented Programming Procedure Oriented Programming
In OOP, program is divided into parts called In POP, program is divided into small parts called
1
objects. functions.
In OOP, Importance is given to the data rather In POP, Importance is not given to data but
2 than procedures or functions because it works to functions as well as sequence of actions
as a real world. to be done.

5
OOP&M CS 305 UNIT I

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


OOP has access specifies named Public,
4 POP does not have any access specified.
Private, Protected, etc.
In OOP, objects can move and communicate In POP, Data can move freely from function to
5
with each other through member functions. function in the system.
OOP provides an easy way to add new data To add new data and function in POP is not so
6
and function. easy.
In OOP, data cannot move easily from function In POP, most function uses Global data for
7 to function, it can be kept public or private so sharing that can be accessed freely from function
we can control the access of data. to function in the system.
OOP provides Data Hiding so POP does not have any proper way for
8
provides more security. hiding data so it is less secure.
In OOP, overloading is possible in the form of
9 Function Overloading and Operator In POP, Overloading is not possible.
Overloading.
Example of OOP is: C++, JAVA,
10 Example of POP is: C, VB, FORTRAN, and Pascal.
VB.NET, C#.NET.

Figure 1.9: Comparison between POP & OOP

Features of Object-Oriented Paradigm


 Programs are divided into simple elements referred as objects.
 Focus is on properties and functions rather than procedure.
 Data is hidden from external functions.
 Functions operate on properties of an object.
 Objects may communicate with each other through a function called messaging.

6
OOP&M CS 305 UNIT I

Merits of Object-Oriented Methodology


1) Improved software-development productivity: Object-Oriented methodology is modular, as it provides
separation of duties in object-based program development. It is also extensible, as objects can be
extended to include new attributes and behaviors. Object-Oriented methodology provides improved
software-development productivity over traditional procedure-based programming techniques.
2) Improved software maintainability: For the reasons mentioned above, Object- Oriented software is
also easier to maintain. Since the design is modular, part of the system can be updated in case of issues
without a need to make large-scale changes.
3) Faster development: Reuse enables faster development. Object-Oriented programming languages
come with rich libraries of objects, and code developed during projects is also reusable in future
projects.
4) Lower cost of development: The reuse of software also lowers the cost of development. Typically, more
effort is put into the object-oriented analysis and design, which lowers the overall cost of development.
5) Higher-quality software: Faster development of software and lower cost of development allows more
time and resources to be used in the verification of the software. Although quality is dependent upon
the experience of the teams, object- oriented methodology tends to result in higher-quality software.

Demerits of Object-Oriented Methodology


1) Steep learning curve: The thought process involved in Object-Oriented methodology may not be natural
for some people, and it can take time to get used to it. It is complex to create programs based on
interaction of objects. Some of the key programming techniques, such as inheritance and polymorphism,
can be challenging to comprehend initially.
2) Larger program size: Object-Oriented programs typically involve more lines of code than procedural
programs.
3) Slower programs: Object-Oriented programs are typically slower than procedure- based programs, as
they typically require more instructions to be executed.
4) Not suitable for all types of problems: There are problems that lend themselves well to functional-
programming style, logic-programming style, or procedure-based programming style, and applying
Object-Oriented methodology in those situations will not result in efficient programs.

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): 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. It defines the logical structure of documents and
the way a document is accessed and manipulated.
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

7
OOP&M CS 305 UNIT I

interface that allows programs and scripts to dynamically access and update the content, structure, and style
of a document.

DOM

INTERFACE

HTML XML JAVA ANY QUERY LANGUAGE

Figure 1.10: Document Object Model

2. Component Object Model (COM): Component Object Model (COM) is a simple Microsoft specification
method that defines a binary standard for exchanging code between two systems, regardless of the
operating system 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 uses one intra- and inter-process communication model
for developer transparency. Developers increase efficiency by modifying service implementation. COM
machine components also share memory, which increases efficiency and provides superior error handling
and debugging.

CLIENT SERVER

IDL PROXY IDL STUB

COM/DOM CORE

OBJECT RPC (Remote Procedure Call)

Figure 1.11: Microsoft Component Object Model

Above Figure 1.11 shows the architecture of Microsoft component object model. Here client refers to a piece
of code that is using the services of a COM component. A COM server is any object that provides services to
client. The Interface Definition Language (IDL) describe the interfaces being implemented by objects.

Elements of OOPS
The conceptual framework of Object–Oriented systems is based upon the object model. There are two
categories of elements in an Object-Oriented system: -

8
OOP&M CS 305 UNIT I

Figure 1.12: OOPS elements


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 −
 Abstraction
 Encapsulation
 Modularity
 Hierarchy

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 −
 Typing
 Concurrency
 Persistence

1) 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. Booch has defined modularity as- “Modularity is the property of a system
that has been decomposed into a set of cohesive and loosely coupled modules”.
Modularity is intrinsically linked with encapsulation. Modularity can be visualized as a way of mapping
encapsulated abstractions into real, physical modules having high cohesion within the modules and their
inter– modules interaction or coupling is low.

2) Hierarchy
In Grady Booch’s words, “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.
The two types of hierarchies in OOA are −
 “IS–A” hierarchy − It defines the hierarchical relationship in inheritance, whereby from a super-class, a
number of subclasses may be derived which may again have subclasses and so on. For example, if we derive
a class car from a class vehicle, we can say that a car “is–a” vehicle.
 “PART–OF” hierarchy − It defines the hierarchical relationship in aggregation by which a class may be
composed of other classes. For example, a CPU is composed of hard disk, motherboard and power supply. It
can be said that motherboard is a “part-of” CPU.

9
OOP&M CS 305 UNIT I

3) Typing
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.
The two types of typing are: -
 Strong Typing − Here, the operation on an object is checked at the time of compilation. 
 Weak Typing − Here, messages may be sent to any class. The operation is checked only at the time of
execution.

4) 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. However, most systems
have multiple threads, some active, some waiting for CPU, some suspended, and some terminated. Systems
with multiple CPUs inherently permit concurrent threads of control; but systems running on a single CPU use
appropriate algorithms to give equitable CPU time to the threads so as to enable concurrency.

In an Object-Oriented environment, there are active and inactive objects. The active objects have
independent threads of control that can execute concurrently with threads of other objects. The active
objects synchronize with one another as well as with purely sequential objects.

5) 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. In files or
databases, the object lifespan is longer than the duration of the process creating the object. This property
by which an object continues to exist even after its creator ceases to exist is known as persistence.

IO processing
Every program takes some data as input and generates processed data as output following the cycle input-
process-output. C++ uses the concept of streams and stream classes to implement its I/O operations with
the console and disk files. Input and output functionality is not defined as part of the core C++ language, but
rather is provided through the C++ standard library (and thus resides in the std namespace).

Streams
A stream is just a sequence of bytes that can be accessed sequentially. Over time, a stream may produce or
consume potentially unlimited amounts of data. The stream library's unified approach makes it very friendly
to use. Using a consistent interface for outputting to the screen and sending files over a network makes life
easier. The programs below will show you what is possible.

Streams work with built-in data types, and you can make user-defined types work with streams
by overloading the insertion operator (<<) to put objects into streams, and the extraction operator (>>) to
read objects from streams.

10
OOP&M CS 305 UNIT I

Figure 1.13: Data Streams

Typically, we deal with two different types of 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.
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.

Stream classes
The I/O system contains a hierarchy of classes that are used to define various streams to deal with both the
console and disk files. These classes are called stream classes. Figure 1.14 shows hierarchy of stream classes
used for input and output operations with the console unit. These classes are declared in the header file
iostream. This file should be included in all the programs that communicate with console unit.

Figure 1.14: Stream classes

ios is the base class for istream (input stream) and ostream (output stream) which are base classes for
iostream, (input/output stream). The class ios provides the basic support for formatted and unformatted I/O
operations.

11

You might also like