You are on page 1of 46

OBJECT ORIENTED

PROGRAMMING
Satyendra Singh Chouhan
Assistant professor (CSE)
High level Languages
• COBOL – developed in the 1960s for business
transactions.
• FORTRAN – developed for mathematical calculations.
• Pascal - is a structured high-level language.
• C – is designed to support only procedure-oriented
programming.
 Popular language for developing system
 applications such as operating system and compilers.
• C++ - is extension of C programming language that support
object oriented programming and procedure oriented
approach.
• Java – is an object-oriented language.
tutorialspoint.com (Online compilation)
http://ideaone.com/
http://codechef.com/
A program in C++
#include <iostream>
#include <math>
using namespace std;
int main()
{int x;
cin>>x; //cin: read x
float y=sqrt(x); //sqrt() inbuilt
//function in math library
cout<<y; //cout: write y
return 0;
Another program
# include <iostream.h>
using namespace std;
int main()
{
float rad,area;
const float PI=3.14; //constant declaration
cout<< endl<<“Enter radius:”;
cin>> rad; // read data
area= PI * rad * rad; //calculate
cout<< endl<< “The area is: “ << area;
return 0;
}
• Single line comments by // {double slash}

• “iostream” is a header file. It contains the “iostream” class

• „cin‟ and „cout‟ are the predefined objects of this class, i.e. ,
iostream.h, These are not functions

• „>>‟, „<<„ are extraction & insertion operator, These are


basically functions

• „endl‟ – manipulator. It performs 2 tasks:-


Inserts a new line character „\n‟
Flushes the o/p stream
Procedural oriented Programming
Procedure oriented Programming
Drawbacks of Procedural Language
• Less importance to data, and more importance to
procedure
• Data is exposed, so it can be changed accidentally
• The procedural approach is not similar to our visualization
of our system
• Procedural modules are not independent of each other. It
hampers its independent and simultaneous development
and reusability
• Good for small programs.
Problems with procedural languages
• First, functions have unrestricted access to global data.
• Second, unrelated functions and data
• The important problem with the procedural paradigm is
that its arrangement of separate data and functions does
a poor job of modeling things in the real world.
• In the physical world we deal with objects such as people
and cars. Such objects aren‟t like data and they aren‟t like
functions. Complex real-world objects have both attributes
and behavior.
Object oriented programming
• Object means a real word entity such as pen, chair, table
etc.

• Object-Oriented Programming is a methodology or


paradigm to design a program using classes and objects.
It simplifies the software development and maintenance
by providing some concepts:
• Object and Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
The Object-Oriented Approach
Object oriented programming (benefits)
• Modularity
• Information hiding
• Code re-use
Save development time (and cost) by reusing code
– once an object class is created it can be used
in other applications
• Easy debugging
– classes can be tested independently
– reused objects have already been tested
• What kinds of things become objects in object-oriented
programs?
• The answer to this is limited only by your imagination.
• Some typical categories to start you thinking:
• 1. Physical objects
 Automobiles in a traffic-flow simulation
 Electrical components in a circuit-design program
 Countries in an economics model
 Aircraft in an air traffic control system
2. Elements of the computer-user environment
Windows
Menus
Graphics objects (lines, rectangles, circles)
The mouse, keyboard, disk drives, printer
3. Data-storage constructs
Customized arrays
Stacks
Linked lists
Binary trees
4.Human entities
Employees
Students
Customers
Salespeople
Examples1: Cars
Consider we have a Class of Cars under which Santro
Xing, Alto and WaganR represents individual Objects.

In this context each Car Object will have its own, Model,
Year of Manufacture, Colour, Top Speed, Engine Power
etc., which form Properties of the Car class and the
associated actions i.e., object functions like Start, Move,
Stop form the Methods of Car Class.
No memory is allocated when a class is created. Memory is
allocated only when an object is created, i.e., when an
instance of a class is created.
Class
• A class serves as a plan, or blueprint. It specifies what
data and what functions will be included in objects of that
class.
• Defining the class doesn‟t create any objects, just as the
mere existence of data type int doesn‟t create any
variables.
• Objects are the member of a class
class
Example 2:
Class: Btech_CSE_IIIrd_year

Objects?

Common Properties?

Function??
A class syntax in C++
• Declared with a keyword class .

• The syntax for class declaration is as follows :

class class_name
{
access_specifier_1:
member1;
access_specifier_2:
member2;
...
} object_names;
Access specifiers
private: members of a class accessible only from with in
other members of the same class or from their
friends (functions).

protected: accessible from members of their same class


and from their friends, and also from members of their
derived classes.

public: accessible from anywhere where the object is


visible.
An Example with Class
#include<iostream.h> // include header file
using namespace std;
class person
{
char name[30]; // No access specifier??
Int age;
public:
void getdata(void);
void display(void);
};
7/26/2017 27

void person :: getdata(void)


{
cout << “Enter name: “;
cin >> name;
cout << “Enter age: “;
cin >> age;
}
void person : : display(void)
{
cout << “\nNameame: “ << name;
cout << “\nAge: “ << age;
}
7/26/2017

Int main() • The output of program


{ is:
• Enter Name: Ravinder
person p;
• Enter age:30
p.getdata();
• Name:Ravinder
p.display();
• Age: 30
return 0;
} //end of example
7/26/2017 29

Structure of C++ Program


7/26/2017 30

Abstraction
• Abstraction refers to the act of representing essential
feature without including the background details or
explanations.
• Example: ATM Machine; All are performing operations
on the ATM machine like cash withdrawal, money
transfer, retrieve mini-statement…etc. but we can't
know internal details about ATM.
Encapsulation
• The Wrapping up of data and functions into a single
unit is known as Encapsulation.
• Data encapsulation is the most striking feature of a
class.
• The data is not accessible to the outside world and
only those function which are wrapped in the class
can access it.
• This insulation of the data from direct access by the
program is called data hiding or information
hiding.
Encapsulation = Data Hiding + Abstraction
Encapsulati on (Contd..)
Encapsulation Example
class Exforsys
{
public:
int sample();
int example(char *se)
int endfunc(); All data members and
//Other member functions functions are bundled inside
a single autonomous entity
private: called class Exforsys.
int x;
float sq;
//Other data members
};

32
7/26/2017 33

Inheritance
• The idea of classes leads to the idea of inheritance
• In our daily lives, we use the concept of classes divided
into subclasses. We know that the animal class is divided
into mammals, amphibians, insects, birds, and so on. The
vehicle class is divided into cars, trucks, buses,
motorcycles, and so on.
• The principle in this sort of division is that each subclass
shares common characteristics with the class from which
it‟s derived.
• Cars, trucks, buses, and motorcycles all have wheels and
a motor; these are the defining characteristics of vehicles.
7/26/2017 34

Inheritance in OOP
A process of forming a new class from an existing class
(often called base class ).
 The base class – also called parent class/super class .
 The derived class – also called as child class/sub
class/extended class .
 Inheritance helps reduce the overall size of the program
code, which helps the overall performance.
 The derived/extended classes have all the properties of
the base class. Upon them, a programmer can choose to
add new features specific t o the newly created derived
class.
7/26/2017 35
Advantages of inheritance:

• Reusability

• Once the base class is defined and worked out, it need


not be reworked.

• Any no. of derived classes can be created from the base


class as needed while adding specific features to each
derived class as needed .

• Saves Time & Efforts


Inheritance (Contd..)

Inheritance declaration :

class < derived _class _name> : < access _specifier ><base _class
_name>

e.g. –
class bread: public food

Class e_n_c : private engg

class science_students: protected students

37
Inheritance (Example)
//base class // derived class // main program
class food class bread: public food int main (void)
{ { {
public: public: bread s;
food(void) { x=0; } bread(void) { s1=0; } s.f(10);
void f(int n1) void f1(int n1) s.output();
{ { s.f1(50);
x= n1*5; s1=n1*10; s.output();
} } }
void output(void) //end of main program
void output(void) {
{ cout << x; } food :: output();
private: cout << s1;
int x; }
}; //end of base class private: int s1;
}// end of derived class
7/26/2017 39

Reusability
• Once a class has been written, created, and
debugged, it can be distributed to other
programmers for use in their own programs. This is
called reusability.
• It is similar to the way a library of functions in a
procedural language can be incorporated into
different programs.
• in OOP, the concept of inheritance provides an
important extension to the idea of reusability.
• A programmer can take an existing class and,
without modifying it, add additional features and
capabilities to it. This is done by deriving a new
class from the existing one.
• The new class will inherit the capabilities of the old
one, but is free to add new features of its own.
Polymorphism
• Example - Suppose if you are in class room that time you
behave like a student, when you are in market at that time
you behave like a customer, when you at your home at
that time you behave like a son or daughter, Here one
person present in different-different behaviors.
7/26/2017 41

Polymorphism in OOP

• Using operators or functions in different ways,


depending on what they are operating on, is called
polymorphism (one thing with several distinct
forms).
• The process of making an operator to exhibit
different behaviors in different instances is known as
operator overloading.
• Using a single function name to perform different
types of tasks is known as function overloading.
7/26/2017 42
Function overloading: example
void printMovie(char arr[10], int rtime)
{
cout<<arr;
cout<<rtime;
}
void printMovie(char arr[3])
{
cout<<arr;
cout<<10;
}
int main()
{
printMovie("hello",5);
printMovie("hello");
}
7/26/2017 44

Message passing
• An OOP consists of a set of objects that
communicate with each other.
• The process of programming in an object oriented
language , involves the following basic step:
• Creating classes that define object and their
behaviour
• Creating object from class defination
• Establishing communication among object
• Message passing involves specifying the name of
object , the name of function and the name of the
information to be sent
employees.salary(name)
Procedure oriented Object oriented programming
programming
• Program is divided into small parts • Program is divided into
called functions number of entities called
• In Procedural programming, Data objects.
can move freely from function to • In OOP, objects can move
function in the system. and communicate with each
• Procedural programming follows Top other through member
Down approach. functions.
• To add new data and function in • OOP follows Bottom Up
Procedural programming is not so approach
easy. • OOP provides an easy way
• Procedural programming does not to add new data and function.
have any proper way for hiding data • OOP provides Data Hiding
so it is less secure. so provides more security.
• There is no concept of reusability • Reusability concept is
• Example of Procedural programming introduced here
are : C, VB, FORTRAN, Pascal. • Example of OOP are : C++,
JAVA, VB.NET, C#.NET.
Application of OOP
• Real-time system
• Simulation and modeling
• Object-oriented data bases
• Hypertext, Hypermedia, and expertext
• AI and expert systems
• Neural networks and parallel programming
• Decision support and office automation systems
• CIM/CAM/CAD systems

You might also like