Unitm
Unitm
By
DR. DEVANSHU TIWARI
Assistant Professor
Computer Science & Business System Department
MITS-DU, Gwalior
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.
• 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.
•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
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.
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.
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
•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 map objects in the problem domain to those in the program.
•Message passing techniques for communication between objects make the interface
descriptions with external systems much simpler.
•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:
4.If a function return type is other than void, and the return statement doesn’t exist in a
function body.
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
};
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.