Module-5
INTRODUCTION to OBJECT ORIENTED PROGRAMMING
Contents
The Object Oriented Programming paradigm
Basic concepts of Object Oriented Programming
Benefits of OOP
Applications of OOP
structure of C++ program
A simple C++ program
specifying a class
A C++ program with Class.
Introduction
Developments in software technology continue to be dynamic
Procedure-Oriented Programming
• Some Characteristics exhibited by procedure-oriented programming are:
→ Emphasis is on doing things (algorithms).
→ Large programs are divided into smaller programs known as functions.
→ Most of the functions share global data.
→ Data move openly around the system from function to function.
→ Functions transform data from one form to another.
→ Employs top-down approach in program design.
The Object Oriented Programming paradigm
OOP treats data as a critical element in the program development and
does not allow it to flow freely around the system.
It ties data more closely to the function that operate on it, and protects
it from accidental modification from outside function.
OOP allows decomposition of a problem into a number of entities called
objects and then builds data and function around these objects.
Fig: Organization of data and function in OOP
• Some of the features of object oriented programming are:
→ Emphasis is on data rather than procedure.
→ Programs are divided into what are known as objects.
→ Data structures are designed such that they characterize the
objects.
→ Functions that operate on the data of an object are ties together
in the data structure.
→ Data is hidden and cannot be accessed by external function.
→ Objects may communicate with each other through function.
→ New data and functions can be easily added whenever necessary.
→ Follows bottom up approach in program design.
Basic Concepts of Object Oriented Programming
Objects
Classes
Data abstraction and encapsulation
Inheritance
Polymorphism
Dynamic binding
Message passing
Objects
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 has to handle.
Each object contain data, and code to manipulate data.
When a program is executed, the objects interact by sending messages to one
another. For example, if “customer” and “account” are to object in a program, then
the customer object may send a message to the count object requesting for the
Classes
A class is a collection of objects similar types.
For examples, Mango, Apple and orange members of class fruit.
Data Abstraction and Encapsulation
The wrapping up of data and function into a single unit (called class) is known as
encapsulation.
Data and encapsulation is the most striking feature of a class.
The data is not accessible to the outside world, and only those functions 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.
Abstraction refers to the act of representing essential features without
including the background details or explanation. Classes use the concept of
abstraction
Inheritance
Inheritance is the process by which objects of one class acquired the
properties of objects of another classes.
It supports the concept of hierarchical classification.
Polymorphism
Polymorphism, a Greek term, means the ability to take more than on
form.
An operation may exhibit different behavior is different instances. The
behavior depends upon the types of data used in the operation.
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 behaviors in
different instances is known as operator overloading.
a single function name can be used to handle different number and
different types of argument.
Using a single function name to perform different type of task is known
as function overloading.
Dynamic Binding
• Binding refers to the linking of a procedure call to the code to be
executed in response to the call.
• Dynamic binding means that the code associated with a given
procedure call is not known until the time of the call at run time.
• It is associated with polymorphism and inheritance.
Message Passing
Objects communicate with one another by sending and receiving
information
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 results.
Message passing involves specifying the name of object, the name of the
function (message) and the information to be sent.
Example:
Benefits of OOP
1. Through
eliminate inheritance,codeweextend
redundant can
2. the
We use of existing
can build Classes.
programs from that
the
standard
communicate working modules
with toone another,
rather
the code than
from having
scratch. start
This writing
leads to
saving
higher of development
productivity. time and
3. The
the principle
programmer of data
to hidingsecure
build helps
program
code in that parts
other can not
of abeprograms.
invaded by
4. It is possible
instances of an to haveto multiple
object co-exist
5. without any
It is possible interference.
to map object inin the
problem
program. domain to those the
6. It is easy
project to partition
based on the work in a
objects.
7. The
enables data-centered
us to capturedesign
more approach
detail of
8. aObject-oriented
model can implemental system form.
cantobe easily
upgraded
system. from small large
9. Message passing
communication techniques
between for
objects
makes
external tosystems
interface descriptions
much simpler. with
10. Software complexity can be easily
managed.
Application of OOP
1. Real-time system
2. Simulation and modeling
3. Object-oriented data bases
4. Hypertext, Hypermedia, and expertext
5. AI and expert systems
6. Neural networks and parallel programming
7. Decision support and office automation systems
8. CIM/CAM/CAD systems
Introduction of C++
C++ is an object-oriented programming language. It was developed by
Bjarne Stroustrup at AT&T Bell Laboratories in Murray Hill, New Jersey,
USA, in the early 1980’s.
C+ + is a superset of C.
The most important facilities that C++ adds on to C care classes,
inheritance, function overloading and operator overloading.
These features enable creating of abstract data types, inherit properties
from existing data types and support polymorphism, thereby making C+
+ a truly object-oriented language.
Structure of C++ Program
Simple C++ Program
// C++ program to print a message
#include<iostream> defines a scope for the identifiers that
are used in a program.
using namespace std; This will bring all the identifiers
defined in std to the current global
int main()
scope.
{ Using and namespace are the new
keyword of C++.
cout<<” c++ is better than c \n”;
return 0;
}
//AVERAGE OF TWO NUMBERS
#include<iostream.h>
Using namespace std;
int main()
{
float number1, number2,sum, average;
cin >> number1;
cin >> number2;
sum = number1 + number2;
average = sum/2;
cout << ”Sum = “ << sum << “\n”;
cout << “Average = “ << average << “\n”;
return 0;
}
Example Programs
1. Program to read and display name and phone number.
2. Program to compute area of triangle
3. Program to generate Fibonacci series
Specifying a class
A class is a way to bind the data and its associated functions together
Specifying a class has two parts:
1. Class declarations: describes the type and scope of its members
2. Class function definition: describes how the class functions are
implemented
The syntax of a class definition is shown below :
class class_name
{
private :
variable declaration; // data member
Function declaration; // Member Function (Method)
public :
variable declaration;
Function declaration;
};
The body of the class has two keywords namely : private , public
• In C++, the keywords private and public are called access specifiers.
• The data hiding concept in C++ is achieved by using the keyword private.
• Private data and functions can only be accessed from within the class
itself.
• Public data and functions are accessible outside the class also.
A simple class example
class student
{
int rollno; // variable declaration
char name[20]; // private by default
public:
void getdata(); // function declarations
void putdata();
};
Creating objects
The objects of a class are declared after the class definition.
class definition does not define any objects of its type, but it defines the
properties of a class.
For utilizing the defined class, we need variables of the class type.
Syntax:
class_name object(’s)_name;
For example,
student s ; //memory for s is created
student s1,s2,s3;
Objects can also be created when the class is defined
Example:
class student
int rollno; // variable declaration
char name[20]; // private by default
public:
void getdata(); // function declarations
void putdata();
} s1,s2;
Accessing class members
The private data of class can be accessed only through the member
function of a class
Example:
s1.getdata();
getdata() // illegal
s1.rollno; //illegal
Defining Member Functions
In C++, the member functions can be defined in two places :
1. Inside class definition
2. Outside class definition using scope resolution operator (::)
Inside Class Definition:
We use only small functions inside the class definition and such functions
are known as inline functions.
Ex: class student
{
int rollno;
char name[20];
public:
void getdata()
{
cin>>name>>rollno;
}
void putdata();
};
Outside class definition
Example:
void student :: getdata()
{
cin>>name>>rollno;
}
A C++ program with class
Example Programs(using classes and objects)
1. Program to read and display name and phone number of 2 persons
2. Program to compute area of triangle
3. Lab pgm 10
#include<iostream.h>
void person : : display(void)
using namespace std;
class person
{
{ cout << “\nNameame: “ <<
char name[30]; name;
int age; cout << “\nAge: “ << age;
public: }
void getdata(void); Int main()
void display(void);
{
};
void person :: getdata(void) person p;
{ p.getdata();
cout << “Enter name: “; p.display();
cin >> name; Return 0;
cout << “Enter age: “;
}
cin >> age;
}