Chapter 1
Chapter 1
Chapter 1
Chapter 1
Principles of Object Oriented
Programming
-by-
Prof. Bhandare P. S.
SVERI’s COE(Poly), Pandharpur
1
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Syllabus:
1.1 Its need & requirement, Procedure Oriented Programming (POP)
verses Object Oriented Programming (OOP), Basic concepts of Object
Oriented Programming, Object Oriented Languages, Applications of
OOP.
Assignment No 1.
List two memory management operators available in C++ and state its
use in one line.(2M)
Explain : (4M)
2
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Central Idea behind including this chapter in subject is that the student
will understand new programming approach.
Importance of chapter :
3
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Introduction
Object-Oriented Programming (OOP) is an approach to program
organization and development that attempts to eliminate some of the
pitfalls of conventional programming methods by incorporating the best
of structured programming features with several powerful new concepts.
However, not all languages are suitable to implement the OOP concepts
easily.
A number of functions are written to accomplish these tasks. The primary focus is on
functions.
4
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
The technique of hierarchical decomposition has been used to specify the tasks to be
completed for solving a problem.
We normally use a flowchart to organize these actions and represent the flow of
control from one action to another.
In a multi-function program, many important data items are placed as global so that
they may be accessed by all the functions. Each function may have its own local data.
5
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Characteristics of OOP:
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.
Data structures are designed such that they characterize the objects.
Functions that operate on the data of an object are tied together in the data
structure.
Classes
Inheritance
Polymorphism
Dynamic binding
Message passing
7
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
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.
They may also 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.
Program objects should be chosen such that they match closely with the real-
world objects.
Objects take up space in the memory and have an associated address like a
record in Pascal, or a structure in C.
For example, if "customer’ and 'account" are two objects in a program, then
the customer object may send a message to the account object requesting for
the bank balance.
Fig. shows two notations that are popularly used in object-oriented analysis and
design.
8
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Classes
We just mentioned that objects contain data, and code to manipulate that data.
The entire set of data and code of an object can be made a user-defined data
type with the help of a class.
objects are variables of the type class. Once a class has been defined, we can
create any number of objects belonging to that class.
Each object is associated with the data of type class with which they are
created.
E.g mango, apple and orange are members of the class fruit.
Classes are user-defined data types and behave like the built-in types of a
programming language.
The syntax used to create an object is no different than the syntax used to
create an integer object in C.
If fruit has been defined as a class, then the statement fruit mango; will create
an object mango belonging to the class fruit.
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 the 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.
9
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
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.
They encapsulate all the essential properties of the objects that are to be
created.
The attributes are sometimes called data members because they hold
information.
The functions that operate on these data are sometimes called methods or
member functions.
Since the classes use the concept of data abstraction, they are known as
Abstract Data Types (ADT).
Inheritance
Inheritance 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 bird 'robin' is a part of the class 'flying bird' which is again a
part of the class 'bird'.
The principle behind this sort of division is that each derived class shares
common characteristics with the class from which it is derived as illustrated in
Fig. below.
10
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
This means that we can add additional features to an existing class without
modifying it .
The new class will have the combined features of both the classes.
The real appeal and power of the inheritance mechanism is that it allows the
programmer to reuse a class
Polymorphism
Polymorphism is another important OOP concept. Polymorphism, a Greek
term, means the ability to take more than one form.
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.
Figure illustrates that a single function name can be used to handle different
number and different types of arguments.
11
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
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 the call at run-time.
Its algorithm is, however, unique to each object and so the draw procedure will
be redefined in each class that defines the object.
At run-time, the code matching the object under current reference will be
called.
Message Passing
An object-oriented program consists of a set of objects that communicate with
each other.
Objects communicate with one another by sending and receiving information much
the same way as people pass messages to one another.
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.
Benefits of OOP
The principal advantages are:
Through inheritance, we can eliminate redundant code and extend the use of
existing classes.
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 saving of 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 have multiple instances of an object to co-exist without any
interference.
It is possible to map objects in the problem domain to those in the program.
It is easy to partition the work in a project based on objects.
The data-centered design approach enables us to capture more details of a
model in implementable form.
Object-oriented systems can be easily upgraded from small to large systems.
Message passing techniques for communication between objects makes the
inter-face descriptions with external systems much simpler.
Software complexity can be easily managed.
13
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Object-Oriented Languages
The languages should support several of the OOP concepts to claim that they
are object-oriented.
Depending upon the features they support, they can be classified into the
following two categories:
◦ 1. Object-based programming languages,
◦ 2. Object-oriented programming languages.
Object-based programming is the style of programming that primarily supports
encapsulation and object identity.
Major features that are required for object-based programming are:
• Data encapsulation
• Data hiding and access mechanisms
•Automatic initialization and clear-up of objects
• Operator overloading
Languages that support programming with objects are said to be object-based
programming languages.
They do not support inheritance and dynamic binding. Ada is a typical object-
based programming language.
Object-oriented programming incorporates all of object-based programming
features along with two additional features, namely, inheritance and dynamic
binding.
Object-oriented programming can therefore be characterized by the following
statement:
Object-based features + inheritance + dynamic binding
Languages that support these features include C++, Smalltalk, Object Pascal
and Java. There are a large number of object-based and object-oriented
programming languages.
14
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Application of OOP:
Real-time systems
Simulation and modeling
Object-oriented databases
Hypertext, hypermedia and expertext
AI and expert systems
Neural networks and parallel programming
Decision support and office automation systems
CIM/CAM/CAD systems
What is C++:
C++ is an object-oriented programming language. It was developed by Bjarne
Stroustrup at AT&T Bell Laboratories in Murray Hill, New Jersey.
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.
Stroustrup, an admirer of Simula67 and a strong supporter of C, wanted to
combine the best of both the languages and create a more powerful language
that could support object-oriented programming features and still retain the
power and elegance of C.
The result was C++. Therefore, C++ is an extension of C with a major addition
of the class construct feature of Simula67.
The idea of C++ comes from the C increment operator ++, thereby suggesting
that C++ is an augmented (incremented) version of C.
Tokens
As we know, the smallest individual unit in a program are known as tokens.
C++ has the following tokens:
◦ Keywords
◦ Identifiers
◦ Constanta
◦ Strings
◦ Operators
15
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Keywords
The keywords implement specific C++ language features.
They are explicitly reserved identifiers and cannot be used as names for the
program variables or other user-defined program elements.
ANSI C++ standards committee has added some more keywords to make the
language more versatile.
16
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Each language has its own rules for naming these identifiers.
Variable declaration
17
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
In C++, a variable can be initialized at run time using expressions at the place
of declaration.
◦ int n = strlen(string);
Thus, both the declaration and the initialization of a variable can be done
simultaneously at the place where the variable is used for the first time.
◦ average = sum/i;
Reference Variables
C++ introduces a new kind of variable known as the reference variable.
For example, if we make the variable sum a reference to the variable total, then
sum and total can be used interchangeably to represent that variable.
18
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Example:
Operators in C++
We have already seen two such operators, namely, the insertion operator «, and
the extraction operator ».
.* Pointer-to-member operator
We know that the same variable name can be used to have different meanings
in different blocks.
The scope of the variable extends from the point of its declaration till the end
of the block containing the declaration.
◦ int x = 10;
◦ {
◦ ……………..
◦ ……………
19
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
◦ int x = 1;
◦ }
Statements in the second block cannot refer to the variable x declared in the
first block, and vice versa.
In C, the global version of a variable cannot be accessed from within the inner
block.
C++ resolves this problem by introducing a new operator :: called the scope
resolution operator.
This can be used to uncover a hidden variable. It takes the following form:
:: variable-name
For example, ::count means the global version of the variable count
Manipulators
Manipulators are operators that are used to format the data display.
◦ « “n=“ « n « endl
◦ « "p =“ « p « endl;
20
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Input Operator
The statement
◦ cin » number1;
is an input statement and causes the program to wait for the user to type in a
number.
The identifier cin pronounced 'C in') is a predefined object in C++ that
corresponds to the standard input stream.
Output Operator
The output statement.
21
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
It inserts (or sends) the contents of the variable on its right to the object on its
left.
Although C++ supports these functions, it also defines two unary operators
new and delete that perform the task of allocating and freeing the memory in a
better and easier way.
Since these operators manipulate memory on the free store, they are also
known as free store operators.
22
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
An object can be created by using new, and destroyed by using delete, as and
when required.
A data object created inside a block with new, will remain in existence until it
is explicitly destroyed by using delete.
The new operator can be used to create objects of any type. It takes the
following general form:
The new operator offers the following advantages over the function malloc().
1. It automatically computes the size of the data object. We need not use the
operator sizeof.
24
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
These sections may be placed in separate code files and then compiled
independently or jointly.
Finally, the main program that uses the class is placed in a third file which
"includes" the previous two files as well as any other files required.
The class definition including the member functions constitute the server that
provides services to the main program known as client.
The client uses the server through the public interface of the class.
25
Subject: Object oriented programming (17432)