Unit - I-Object Oriented Programming Concepts
Unit - I-Object Oriented Programming Concepts
RAMAPURAM CAMPUS
Unit I
OBJECT ORIENTED PROGRAMMING
Dr.S.Veena,Associate Professor/CSE 1
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING
PROGRAMMING METHODOLOGIES
The earliest computers were programmed in binary. Mechanical
switches were used to load programs. With the advent of mass storage
devices and larger and cheaper computer memories, the first high-
level computer programming languages came into existence. These
languages were simple to design and easy to use because programs at
that time were primarily concerned with relatively simple tasks like
calculations. They suffered from following limitations:
• No facilities to reuse existing program code.
• Control was transferred via the dangerous goto statement.
• All variables in the program were global. Tracking down spurious
changes in global data was a very tedious job.
• Writing, understanding and maintaining long programs are very
difficult.
Dr.S.Veena,Associate Professor/CSE 2
Structured Programming
• A structured program is built by breaking down the
program’s primary purpose into smaller pieces that then
become functions within the program.
• Each function can have its own data and logic.
• Information is passed between functions using parameters
and functions can have local data that cannot be accessed
outside the function scope.
• It helps you to write cleaner code and maintain control over
each function.
• By isolating the processes within the functions, a structured
program minimizes the chance that one procedure will affect
another.
Dr.S.Veena,Associate Professor/CSE 3
Structured Programming
Main Program
Function 4 Function 5
Dr.S.Veena,Associate Professor/CSE 4
Structured Programming
Limitations:
• Data is given second class status in the procedural paradigm even
though data is the reason for program’s existence.
• Data types are processed in many functions, and when changes occur in
data types, modifications must be made at every location that acts on
those data types within the program.
• Structured programming components – functions and data structures
doesn’t model the real world very well.
• It emphasizes on fitting a problem to the procedural approach of a
language.
Dr.S.Veena,Associate Professor/CSE 5
Object Oriented Programming
Definition
• It is an approach that provides a way of modularizing programs
by creating partitioned memory area for both data and functions
that can be used as templates for creating copies of such
modules on demand.
Characteristics
– 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 functions
that operate on it, and protects it from accidental modification from outside
functions.
– OOP allows decomposition of a problem into a number of entities called objects and
then builds data and functions around these objects.
– The organization of data and functions in object-oriented programs is shown in
fig.1.1. The data of an object can be accessed only by the functions associated with
that object. However functions of one object can access the functions of other
objects.
Dr.S.Veena,Associate Professor/CSE 6
Object Oriented Programming
Data Data
Communication
Member Member
function Function
Object C
Member
function
Data
Dr.S.Veena,Associate Professor/CSE 7
Comparison between Structured and
Object Oriented Programming
Structured Programming Object Oriented programming
Emphasis is on doing things Emphasis is on data
Large programs are divided into smaller Programs are divided into what are known
programs known as functions as objects.
Most of the functions share global data Functions that operate on data of the object
are tied together in the data structures
Data move openly around the system from Data is hidden and cannot be accessed by
function to function. external function.
Its components doesn’t model the real world Its components model the real world objects
objects
Employs top-down approach in program Follows bottom-up approach in program
design. design.
Dr.S.Veena,Associate Professor/CSE 8
Object Oriented Programming
Concepts
The basic concepts of Object Oriented Programming are
• Objects
• Classes
• Methods
• Message Passing
• Data Abstraction
• Encapsulation
• Inheritance
• Polymorphism
• Dynamic Binding
Dr.S.Veena,Associate Professor/CSE 9
Class
Definition of a class
A class is a way to bind the data and its associated
functions together. It allows the data to be hidden if necessary
from external use. While defining a class, we are creating a new
abstract data type that can be treated as a built-in data type. A class
specification has two parts:
Class declaration – describes the type & scope of its members
Class function definitions – describe how the class functions are
implemented.
Dr.S.Veena,Associate Professor/CSE 10
Class Declaration
class class_name • The keyword class specifies that what follows is an
{
private:
abstract data of type class_name.
variable declarations; • The body of the class is enclosed within braces and
function declarations: terminated by a semicolon.
public: • The class body contains the declaration of variables
variable declarations;
function declarations; and functions. These functions and variables are
}; collectively called class members.
• The keywords private and public are known as
visibility labels and it specify which members are
private which of them are public. These should
followed by a colon.
• Private members can be accessed only within the
class whereas public members can be accessed form
outside the class. By default, the members of a
class are private. If both the labels are missing,
the members are private to the class.
Dr.S.Veena,Associate Professor/CSE 11
Objects
• Objects are the basic run-time entities in
an object oriented programming.
• They may represent a person, a place, a bank
account or any item that the program has to
handle. They may represent user-defined data
such as vectors, time and lists.
• Programming problem is analyzed in terms of
objects and the nature of communication between
them.
• Objects are the instances of the classes.
Dr.S.Veena,Associate Professor/CSE 12
Objects
Object: STUDENT
DATA:
Name
Data Members
Roll No
Mark 1, Mark 2, Mark 3
FUNCTIONS
Total Member Functions
Average
Display
Dr.S.Veena,Associate Professor/CSE 13
Program - Class
Dr.S.Veena,Associate Professor/CSE 14
Methods
Methods
The functions we use in C++ is called as methods. There are two types of
functions. One belongs to the class which is used to access the data of that
particular class. This type of functions is called as member functions. The
member functions may be defined inside the class or outside the class. If the
function is defined outside the class, scope resolution operator is used.
There are cases we can have functions which are not belong to the classes.
They are called as non-member functions.
Functions in C++:
Syntax :
Return type function name( ); //Empty function
Return type function name(arg); // Function with argument
The function that has a return value should use the return statements for
termination.
int main()
{
……
return 0;
} Dr.S.Veena,Associate Professor/CSE 15
Messages Passing
The process of programming in OOP involves the following basic steps:
Creating classes that define objects and their behavior
Creating objects from class definitions
Establishing communication among objects
A message for an object is 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.
E.g.: employee.salary(name);
Object: employee
Message: salary
Information: name
Dr.S.Veena,Associate Professor/CSE 16
Data Abstraction
Abstraction represents the act of representing the essential features
without including the background details or explanations. Classes use the
concept of abstraction and are defined as a list of abstract attributes such as
size, weight and cost and functions to operate on these attributes. The
attributes are called data members and functions are called member
functions or methods.
Abstraction is useful for the implementation purpose. Actually the end
user need not worry about how the particular operation is implemented.
They should be facilitated only with the operations and not with the
implementation.
For example, Applying break is an operation. It is enough for the person
who drives the car to know how pressure he has to apply on the break pad
rather than how the break system functions. The car mechanic will take care
of the breaking system.
Dr.S.Veena,Associate Professor/CSE 17
Encapsulation
The wrapping up of data and functions into a single unit
is known as encapsulation. It is the most striking feature of the
class. The data is not accessible to the outside world and only those
functions which are wrapped in the class can access it. These functions
provide interface between the object’s data and the program. This
insulation of the data from direct access by the program is called data
hiding or information hiding.
Dr.S.Veena,Associate Professor/CSE 18
Inheritance
It is the process by which objects of one class acquire the
properties of objects of another class. It supports the concept of
hierarchical classification. For example, the person ‘son’ is a part of the
class ‘father’ which is again a part of the class ‘grand father’.
This concept provides the idea of reusability. This means that we
can add additional features to an existing class without modifying it. This
is possible by a deriving a new class from an existing one. The new class
will have the combined features of both the classes.
Dr.S.Veena,Associate Professor/CSE 19
Abstract Classes
The classes without any objects are known as abstract classes. These
classes are usually outcome of generalization process which finds out
common elements of the classes of interest and stores them in a base
class. The abstract classes are very important from the designer’s point of
view. Whenever a designer has to model an abstract concept which
cannot have objects, then abstract classes are handy.
For example consider the class shape. It can be inherited to
rectangle, ellipse, which in turn can be inherited to parallelograms into
squares and circles. Except shape, all other can have objects of them.
Dr.S.Veena,Associate Professor/CSE 20
Polymorphism
It means the ability to take more than one form. An operation
may exhibit different behavior in different instances. The behavior depends
upon the types of data used in the operation. For example the operation
addition will generate sum if the operands are numbers whereas if the
operands are strings then the operation would produce a third string by
concatenation.
Dr.S.Veena,Associate Professor/CSE 21
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 at 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.
Dr.S.Veena,Associate Professor/CSE 22