0% found this document useful (0 votes)
2 views

Unitm

Object-Oriented Programming (OOP) is a programming paradigm centered around objects that encapsulate data and functions, contrasting with Procedure-Oriented Programming (POP) which focuses on functions and procedures. OOP features include encapsulation, inheritance, polymorphism, and abstraction, allowing for better data management and code reusability, while POP emphasizes a top-down approach and global data sharing. Key programming languages associated with OOP include Java, Python, and C++, while POP languages include C and FORTRAN.

Uploaded by

nsohailkhan39
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unitm

Object-Oriented Programming (OOP) is a programming paradigm centered around objects that encapsulate data and functions, contrasting with Procedure-Oriented Programming (POP) which focuses on functions and procedures. OOP features include encapsulation, inheritance, polymorphism, and abstraction, allowing for better data management and code reusability, while POP emphasizes a top-down approach and global data sharing. Key programming languages associated with OOP include Java, Python, and C++, while POP languages include C and FORTRAN.

Uploaded by

nsohailkhan39
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Object Oriented Programming

By
DR. DEVANSHU TIWARI
Assistant Professor
Computer Science & Business System Department
MITS-DU, Gwalior

Friday, February 14, 2025


Friday, February 14, 2025
Object-Oriented Programming (OOP)
• Object-oriented programming (OOP) is a programming language model organized
around objects rather than actions and focuses on data rather than logic.
• Historically, a program has been viewed as a logical procedure that:
1.Takes input data
2.Processes the data
3.Produces output data
• 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.

Friday, February 14, 2025


History of OOP
• Simula was the first object-oriented programming language.
• Java – Specifically designed for distributed applications, Java is widely used in corporate networks, enterprise systems, and the
Internet. Its platform independence, achieved through the Java Virtual Machine (JVM), makes it highly versatile.
• Python – Known for its simplicity and readability, Python supports multiple paradigms, including OOP. It is widely used in web
development, data science, artificial intelligence, and automation.
• C++ – An extension of the C programming language, C++ incorporates OOP features while retaining high performance. It is
commonly used in game development, system programming, and real-time applications.
• Visual Basic .NET – Developed by Microsoft, this language is used primarily for Windows applications, providing a simple
syntax for developers.
• Ruby – A dynamically typed, object-oriented language often used in web development, particularly with the Ruby on Rails
framework.
• Other Notable OOP Languages:
• Besides the widely known languages, several other programming languages incorporate OOP principles:
• Curl – A multi-paradigm language used for rich internet applications.
• Smalltalk – One of the earliest pure OOP languages, which influenced many modern OOP concepts.
• Delphi – An object-oriented version of Pascal, commonly used for desktop and enterprise applications.
• Eiffel – Designed with a focus on software reliability and reusability, Eiffel promotes the design by contract principle.
Friday, February 14, 2025
Procedure-oriented programming -
•Programming in the high-level languages such as COBOL, FORTRAN,
C, etc. is known as procedure-oriented programming.
•In the procedure oriented approach the problem is viewed as sequence of
thing to be done such as reading, calculating, and printing. A number of
functions are written to complete these tasks.The primary focus here is on
the function.

•“POP consists of writing a list of instructions for the computer


to follow and organizing these instructions into group known as
function.”
Structure of procedure oriented programs
⚫ In a multi-function program we use global variable to communicate between two functions.
⚫ Global variable can be use by any function at any time while local variables are only used within the function.
⚫ A global variable is a variable declared outside of any function, making it accessible from anywhere
within the program, while a local variable is a variable declared inside a function, meaning it can only
be accessed within that specific function where it was defined.
•Global variable access: Can be used by any part of the code, including other functions.
•Local variable access: Can only be used within the function where it is declared.
⚫ But it creates problem in large program because we can’t determine which global variables (data) are used by
which function.
⚫ Also global variables are accessed by all the function so any function can change its value at any time so all the
function will be affected.
⚫ In case we need to revise an external data structure we also need to revise all functions that access that
data. This creates a chance for bugs to appear.
Relationship of data and function in POP
Characteristics of procedure-oriented programming language:

⚫ Emphasis is on doing things, (Algorithms)


⚫ Large programs are divided into smaller program– function
⚫ Most of functions share global data.
⚫ Data more openly around system from function to function.
⚫ Function transform data from one form to another
⚫ Employs top-down approach for program design
Advantage of POP-
Easy to use and understand.
Easy to learn
Simple
Error can be easily removed.
Disadvantage of POP-
Time consuming
No security for data
No structure or code reusability
Slow performance
Difficult to maintenance
Exception handling
Object-oriented programming (OOP)-
• Object-oriented programming (OOP) is a computer programming model
that organizes software design around data, or objects, rather than functions
and logic.An object can be defined as a data field that has unique attributes
and behavior.
• “ In OOPS languages all the applications are built around class
and object”
OOP-
Features of OOP-
•Some features of object oriented programming are as following-
⚫ Emphasis is on data rather than procedures.
⚫ Programs are divided into objects.
⚫ Data structures are designed such that they characterize the objects .
⚫ Functions &data are tied together in the data structures so that data
abstraction is introduced in addition to procedural abstraction.
⚫ Data is hidden &can’t be accessed by external functions.
⚫ Object can communicate with each other through function.
⚫ New data &functions can be easily added.
⚫ Follows Bottom up approach.
Advantage of OOP-
1. Reusability: In OOP's programs functions and modules that are written by a user can
be reused by other users without any modification.
2. Inheritance: Through this we can eliminate redundant code and extend the use of existing
classes.
3. Data Hiding: The programmer can hide the data and functions in a class from other
classes. It helps the programmer to build the secure programs.
4. Reduced complexity of a problem: The given problem can be viewed as a
collection of different objects. Each object is responsible for a specific task. The problem
is solved by interfacing the objects. This technique reduces the complexity of the program
design.
5. Easy to Maintain and Upgrade: OOP makes it easy to maintain and modify existing
code as new objects can be created with small differences to existing ones. Software
complexity can be easily managed.

6. Message Passing: The technique of message communication between objects


makes the interface with external systems easier.

7. Modifiability: it is easy to make minor changes in the data representation or the


procedures in an OO program. Changes inside a class do not affect any other part
of a program, since the only public interface that the external world has to a class is
through the use of methods.
Disadvantage of OOP-
• The length of the programmes developed using OOP language is much larger than the
procedural approach. Since the programme becomes larger in size, it requires more time to
be executed that leads to slower execution of the programme.
• We can not apply OOP everywhere as it is not a universal language. It is applied only
when it is required. It is not suitable for all types of problems.
• Programmers need to have brilliant designing skill and programming skill along with
proper planning because using OOP is little bit tricky.
• OOPs take time to get used to it.The thought process involved in object-oriented
programming may not be natural for some people.
• Everything is treated as object in OOP so before applying it we need to have excellent
thinking in terms of objects.
POP Vs OOP
Procedure Oriented Programming Object Oriented Programming

Divided Into In POP, program is divided into small parts In OOP, program is divided into parts
called functions. called objects.
Importance In POP, Importance is not given to data but to In OOP, Importance is given to the data rather
functions as well as sequence of actions to be done. than procedures or functions because it works
as a real world.

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

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

Data Moving In POP, Data can move freely from function to In OOP, objects can move and communicate
function in the system. with each other through member functions.

Expansion To add new data and function in POP is not so easy. OOP provides an easy way to add new data
and function.
POP Vs OOP
Procedure Oriented Programming (POP) Object Oriented Programming (OOP)

Data Access In POP, Most function uses Global data for In OOP, data can not move easily from function
sharing that can be accessed freely from to function, it can be kept public or private so
function to function in the system. we can control the access of data.

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

Overloading In POP, Overloading is not possible. In OOP, overloading is possible in the form of
Function Overloading and Operator
Overloading.

Examples Example of POP are : C, VB, FORTRAN, Example of OOP are : C++, JAVA, VB.NET,
Pascal. C#.NET.
Characteristics of an Object-Oriented Programming
Language
• Class
• A class is a blueprint for an object.
• A group of objects that share common properties for data part and some
program part are collectively called as class.
• Class is 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.
• Data members are the data variables and member functions are the
functions used to manipulate these variables together these data
members and member functions define the properties and behavior of
the objects in a Class.
For Example: Consider the Class of Cars. There may be many cars with different
names and brands but all of them will share some common properties like all of them
will have 4 wheels, Speed Limit, Mileage range, etc. So here, the Car is the class, and
wheels, speed limits, and mileage are their properties.
class Car
{ public:
// class data (data member)
string brand, model;
int mileage = 0;
// class function (member function)
void drive(int distance)
{ mileage += distance;
}
};
• 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.

• 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 must handle.
The fundamental idea behind object oriented approach is to combine both data and function into a
single unit and these units are called objects.

• Object is a real world entity that have attribute/characteristics and behavior.


• Attributes (data fields) are those terms which have particular value at a particular
instant of time.
• Behavior (methods) is the way of performing action by an object.

• An object has two characteristics: attributes and behavior. For example, a car can be an
object. And, it has
• attributes - brand, model, size, mileage, etc.
• behavior - driving, acceleration, parking, etc.
•Objects use memory just like variables in a program. Each object has its own
unique address in memory, just like records in Pascal or structures in C.

•When a program runs, objects communicate with each other by sending


messages (calling functions or methods).

•Each object has data (information) and code (functions) to work with that
data.

•Objects don’t need to know the internal details of other objects—they only need
to know what messages (function calls) the other object can accept and what
response it will return.
Characteristics of OOP

• Encapsulation – Encapsulation is capturing data and keeping it safely and


securely from outside interfaces.

Inheritance- This is the process by which a class can be derived from a base
class with all features of base class and some of its own. This increases code
reusability.

Polymorphism- This is the ability to exist in various forms. For example an


operator can be overloaded so as to add two integer numbers and two floats.

Abstraction- The ability to represent data at a very conceptual level without


any details.
• Encapsulation
Encapsulation is defined as wrapping up 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.
Encapsulation also leads to data abstraction or data hiding.

Real-life example of encapsulation, in a company, there are different sections like the accounts section, finance
section, sales section, etc. The finance section handles all the financial transactions and keeps records of all the
data related to finance. Similarly, the sales section handles all the sales-related activities and keeps records of all
the sales. Now there may arise a situation when for some reason an official from the finance section needs all the
data about sales in a particular month. In this case, he is not allowed to directly access the data of the sales
section. He will first have to contact some other officer in the sales section and then request him to give the
particular data. This is what encapsulation is. Here the data of the sales section and the employees that can
manipulate them are wrapped under a single name “sales section”.
Polymorphism
Polymorphism, a Greek term, means the ability to take more than one form. polymorphism means
having many forms.
Example-A person at the same time can have different characteristics. A man at the same time is a
father, a husband, and an employee. So the same person possesses different behavior in different
situations. This is called polymorphism.
•Operator Overloading:
•An operation may exhibit different behaviours in different instances. The behaviour depends upon
the types of data used in the operation.

• The process of making an operator exhibit different behaviors in different instances is known as
operator overloading.

For example, consider the operation of addition. For two numbers, the operation will generate a
sum. If the operands are strings, then the operation would produce a third string by concatenation.
The process of making an operator to exhibit different behaviours in different instances is known as
operator overloading.
•Function Overloading: Function overloading is using a single function name to
perform different types of tasks.

•A single function name can be used to handle different number and different types
of arguments. This is something similar to a particular word having several different
meanings depending on the context. Using a single function name to perform
different types of tasks is known as function overloading.
Example: Suppose we have to write a function to add some integers, sometimes there are 2 integers,
and sometimes there are 3 integers. We can write the Addition Method with the same name having
different parameters, the concerned method will be called according to parameters.
Polymorphism plays an important role in allowing objects having different internal structures to share
the same external interface.

Polymorphism is extensively used in implementing inheritance.


Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those
methods to perform different tasks. This allows us to perform a single action in different ways.
Abstraction
Data abstraction is one of the most essential and important features of object-oriented
programming in C++. 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.

Real-life Example of a man driving a car. The man only knows that pressing the
accelerator will increase the speed of the car or applying brakes will stop the car but he
does not know how on pressing the accelerator the speed is actually increasing, he does
not know about the inner mechanism of the car or the implementation of an accelerator,
brakes, etc. in the car.
•Abstraction using Classes: We can implement Abstraction in C++ using classes. The
class helps us to group data members and member functions using available access
specifiers. A Class can decide which data member will be visible to the outside world and
which is not.

•Abstraction in Header files: One more type of abstraction in C++ can be header files.
For example, consider the pow() method present in math.h header file. Whenever we
need to calculate the power of a number, we simply call the function pow() present in the
math.h header file and pass the numbers as arguments without knowing the underlying
algorithm according to which the function is actually calculating the power of numbers.
Inheritance is the process by which objects of one class inherits the properties of objects of
another class. It supports the concept of hierarchical classification.

•Sub Class: The class that inherits properties from another class is called Sub class or
Derived Class (child).

•Super Class: The class whose properties are inherited by a sub-class is called Base Class
(Parent) or Superclass.

•Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a
new class and there is already a class that includes some of the code that we want, we can
derive our new class from the existing class. By doing this, we are reusing the fields and
methods of the existing class.
Message Passing

An object-oriented program consists of a set of objects that communicate with each other.
The process of programming in an object-oriented language, therefore, involves the
following basic steps:
1.Creating classes that define objects and their behaviour,
2.Creating objects from class definitions, and
3.Establishing communication among objects.

Objects communicate with one another by sending and receiving information much the same
way as people pass messages to one another. The concept of message passing makes it easier
to talk about building systems that directly model or simulate their real-world counterparts.
A message for an object is a request for execution of a procedure, and therefore will invoke a
function (procedure) in the receiving object that generates the desired result. Message passing
involves specifying the name of the object, the name of the function (message), and the
information to be sent.

Objects have a life cycle. They can be created and destroyed. Communication with an object
is feasible as long as it is alive.
Dynamic Binding

Binding refers to the linking of a procedure call to the code to be executed in


response to the call. Dynamic binding (also known as late binding) means that the
code associated with a given procedure call is not known until the time of the call at
run-time.

In Dynamic binding , the code to be executed in response to the function call is


decided run time.

It is associated with polymorphism and inheritance. A function call associated with a


polymorphic reference depends on the dynamic type of that reference.
Benefits of OOP
•Through inheritance, we can eliminate redundant code and extend existing classes.

•We can build programs from the standard working modules that communicate with one
another, rather than having to start writing the code from scratch. This leads to savings in
development time and higher productivity.

•The principle of data hiding helps the programmer to build secure programs that cannot be
invaded by code in other parts of the program.

•It is possible to have multiple instances of an object to coexist without interference.

•It is possible to map objects in the problem domain to those in the program.

•It is easy to partition the work in a project based on objects.


Benefits of OOP
•The data-centered design approach enables us to capture more details in an implementable
form.

•Object-oriented systems can be easily updated from small to large systems.

•Message passing techniques for communication between objects make the interface
descriptions with external systems much simpler.

•Software complexity can be easily managed.

While it is possible to incorporate all these features in an object-oriented system, their


importance depends on the type of project and the preference of the programmer. However,
there are a number of issues that need to be tackled to reap some of these benefits.
Application of OOPs in C++

•Real-Time System design: Real-time system inherits complexities and makes it difficult to
build them. OOP techniques make it easier to handle those complexities.

•Hypertext and Hypermedia: Hypertext is similar to regular text as it can be stored, searched,
and edited easily. Hypermedia on the other hand is a superset of hypertext. OOP also helps in
laying the framework for hypertext and hypermedia.

•AI Expert System: These are computer application that is developed to solve complex
problems which are far beyond the human brain. OOP helps to develop such an AI expert
System

•Office automation System: These include formal as well as informal electronic systems that
primarily concerned with information sharing and communication to and from people inside
and outside the organization. OOP also help in making office automation principle.
•Neural networking and parallel programming: It addresses the problem of prediction and approximation of
complex-time varying systems. OOP simplifies the entire process by simplifying the approximation and
prediction ability of the network.

•Stimulation and modeling system: It is difficult to model complex systems due to varying specifications of
variables. Stimulating complex systems require modeling and understanding interaction explicitly. OOP
provides an appropriate approach for simplifying these complex models.

•Object-oriented database: The databases try to maintain a direct correspondence between the real world and
database object in order to let the object retain it identity and integrity.

•Client-server system: Object-oriented client-server system provides the IT infrastructure creating object-
oriented server internet(OCSI) applications.

•CIM/CAD/CAM systems: OOP can also be used in manufacturing and designing applications as it allows
people to reduce the efforts involved. For instance, it can be used while designing blueprints and flowcharts.
So it makes it possible to produce these flowcharts and blueprint accurately.
Inline Functions in C++

•C++ provides inline functions to reduce the function call overhead. An inline function is a function that is
expanded in line when it is called. When the inline function is called whole code of the inline function gets
inserted or substituted at the point of the inline function call. This substitution is performed by the C++
compiler at compile time. An inline function may increase efficiency if it is small.

Syntax:
inline return-type function-name(parameters)
{
// function code
}

Inlining is only a request to the compiler, not a command. The compiler can ignore the request for inlining.
The compiler may not perform inlining in such circumstances as:

1.If a function contains a loop. (for, while and do-while)

2.If a function contains static variables.

3.If a function is recursive.

4.If a function return type is other than void, and the return statement doesn’t exist in a
function body.

5.If a function contains a switch or goto statement.


#include <iostream>
using namespace std;
inline int cube(int s)
{ return s * s * s;
}
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
}

OutputThe cube of 3 is: 27


Inline function and classes
It is also possible to define the inline function inside the class. In fact, all the functions defined inside the class
are implicitly inline. Thus, all the restrictions of inline functions are also applied here. If you need to explicitly
declare an inline function in the class then just declare the function inside the class and define it outside the
class using the inline keyword.

Syntax:
class S
{
public:
inline int square(int s) // redundant use of inline
{
// this function is automatically inline
// function body
}
};
For Example:
class S
{
public:
int square(int s); // declare the function
};

inline int S::square(int s) // use inline prefix


{
}
Default Arguments in C++
A default argument is a value provided for a parameter in a function declaration that is
automatically assigned by the compiler if the calling function doesn’t provide a value
for those parameters. If the value is passed for it, the default value is overridden by the
passed value.

Allows a function to be called without providing one or more trailing argumemts.

Syntax of Default Arguments

A default argument is defined by assigning a value to a function parameter in its


declaration.
return_type name (p1= v1, p2= v2, …);

where v1, v2 and more are the default values for the parameters p1, p2 and so on.
Using default arguments can simplify your function calls.
Rules for Using Default Arguments
1. Default Values Must Be Specified in Function Declarations
The default values for parameters must be specified in the function
declaration (or prototype). If a function is declared and defined
separately, the default values must be in the declaration, not in
definition.
2. Default Arguments Cannot Be Modified
Once default arguments are defined, they cannot be modified in the
function definition. If you try to change the default value in the
definition, it will result in a compilation error.
3. Default Arguments Must Be Provided from Right to Left
In a function with multiple parameters, default values must be provided
from the rightmost parameter to the left. If a parameter has a default
argument, all parameters to its right must also have default values.
4. Ambiguity in Function Overloading
If a function containing default arguments is overloaded, then we
need to make sure it is not ambiguous to the compiler, otherwise
it will throw an error. For example,
Advantages of Default Arguments
•Default arguments are useful when we want to increase the capabilities of an existing
function as we can do it just by adding another default argument to the function.
•It helps in reducing the size of a program.
•It provides a simple and effective programming approach.
•Default arguments improve the consistency of a program.

Disadvantages of Default Arguments


•If the default values are not well-documented or understood, it can lead to confusion about
what arguments are being used.
•Overloading functions with default arguments can sometimes lead to ambiguities,
•It increases the execution time as the compiler needs to replace the omitted arguments by
their default values in the function call.
THANK YOU

You might also like