You are on page 1of 24

Object-Oriented Programming

Procedure – Oriented Programming


• Emphasis on procedure/Algorithm.
• Large program are divided into smaller program
known as functions.
• Most of the function share global data.
• Data move openly between function to function.
• Top- down approach.
• Eg. COBOL, PASCAL, C.
Procedural Programming

Main Program

Function-1 Function-2 Function-3

Function-4 Function-5
Function-6

Function-7 Function-8 Function-9


Global Data 1 Global Data 2

Function 1 Function 2 Function 3

Local data Local data Local data


Object-Oriented Programming
• Emphasis on Data rather than Procedure.
• Program are divided into objects.
• Data is hidden and cannot accessed by external
function directly.
• Objects may communicate with each other through
functions.
• Follow Bottom -Up design approach.
• C++, Java
Organization of data and Functions
Object An
Object Bn

Data Data

Communication
Functions Functions

Object C

Data

Functions
Difference Between Procedure Oriented Programming
& Object Oriented Programming
Difference Between Procedure Oriented Programming (POP) & Object Oriented Programming
Two Programming Paradigms
Structural (Procedural) Object-Oriented
PROGRAM PROGRAM

FUNCTION OBJECT

Operations

FUNCTION Data
OBJECT

OBJECT Operations
FUNCTION
Operations Data

Data
Function calls Messages passing
9
OOPS Principles
• Objects
• Classes
• Data Abstraction and Encapsulation
• Polymorphism
• inheritance
What is an object?
Objects are the runtime entities of a class or an
instance of a class.
OBJECT
set of methods
Operations (member functions)

Data internal state


(data members)

11
Class
• Consists of both data and methods
• The entire set of data and code can be made as a
user-defined type with the help of a class. Once a
class has been defined, we can create any number of
objects belonging to that class.
Declaration of an Object

class Rectangle
main()
{ {
private: Rectangle r1;
Rectangle r2;
int width;
int length; r1.set(5, 8);
public: cout<<r1.area()<<endl;

void set(int w, int l); r2.set(8,10);


int area(); cout<<r2.area()<<endl;
}
}

13
Declaration of an Object

class Rectangle
main()
{ {
private: Rectangle r1;
Rectangle r2;
int width;
int length; r1.set(5, 8);
public: cout<<r1.area()<<endl;

void set(int w, int l); r2.set(8,10);


int area(); cout<<r2.area()<<endl;
}
}

14
• For example, the object for Rectangle class
can be created as follows:

Rectangle r1;

• r1 is the runtime entity of Rectangle


• Have the data members width and length
• Functions Set and Area can be used by r1.
Data Abstraction & Encapsulation
Encapsulation :

• The wrapping up of data and functions into a single


unit.
• The data is not accessible to the outside world and
those functions which are wrapped in the class can
access it.
Abstraction:

• The act of representing essential features


without including the explanations.

• Since the classes use the concept of data


abstraction, they are known as Abstract Data
Types.
Inheritance
• The process by which objects of one class acquire the
properties of another class.

• Allows the programmer to reuse defined properties.

• Four Wheeler

Car Jeep
Polymorphism
• Ability to take more than one form.

• An operation can exhibit different behaviours in different


instance.

• For example, consider the operation of addition.


For two numbers, the operation will generate a sum.
For strings, the operation would produce a third string
by concatenation.

• Operator Overloading , Function Overloading


Dynamic Binding
• Binding means connecting the function call to
a function implementation at run time.

• C++ provides facility to specify that the


compiler should match function calls with the
correct definition at the run time.
Message Passing
• Message Passing is sending and receiving of information by the
objects same as people exchange information.

• This helps in building systems that simulate real life.

• Following are the basic steps in message passing:


1. Creating classes that define objects and its behaviour
2. Creating objects from class definitions
3. Establishing communication among objects

In OOPs, Message Passing involves specifying the name of objects,


the name of the function, and the information to be sent.
• *********C Program**********
# include <iostream>
int add(int, int);
int main()
{ int num1, num2, sum;
printf(“Enters two numbers to add: )";
scanf(“%d, %d”, &num1, &num2);
sum = add(num1,num2); //Function call
printf(“%d”, sum);
return 0;
}
int add(int a, int b)
{ int add;
add = a+b;
return add; //Return statement
}
******** C++ Program********
# include <iostream>
using namespace std;

int add(int, int); //Function prototype(declaration)


int main()
{ int num1, num2,
sum;
cout<<"Enters two numbers to add: ";
cin>>num1>>num2;
sum = add(num1,num2); //Function call
cout<<"Sum = "<<sum;
return 0;
}
Continued...

int add(int a,int b)


{ //Function declarator
int add;
add = a+b;
return add; //Return statement
}

You might also like