0% found this document useful (0 votes)
10 views7 pages

Adobe Scan 22 Aug 2025

Uploaded by

ramsah3757
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Adobe Scan 22 Aug 2025

Uploaded by

ramsah3757
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Oriented.

Programming wth C++


Ohject-

Global data
Global data

Function -2 Function -3
Function -1

Local data Local data


Local data

ig. 1.5 Relationship of data and functions in proceduralprogramming

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 arvund the system from function to function.
Functions transform data from one form to another.
Employs top-down approach in program design.

1.4 Object-Oriented Programming Paradigm


The major motivating factor in the
of the flaws encountered in invention of object-oriented approach is
in the program development the prócedural approach. to remove som:
OOP treats data as a critical eleme
data more closely to the and does not allow it to flow freely around the
functions that operate on it, and
modification from outside functions. system. It tis
of entities called OP protects it from
objects and then buildsallows
data decomposition of a problem into aaccidental
organization of data and functions
data of an object can be
and functions around
in object-oriented
number
these objects. The
accessed only by the functions programs is shown in Fig. 1.6. The
functions of one object can
access the functions of associated with that object.
Some of the striking other objects. However,
features of object-oriented
Emphasis is on data rather than programming are:
Programs are divided procedure.
Data structures are into what are known as
designed such that they objects.
characterize the objects.
7
Principles of Object-Oritnted Programming

Functions that operate on the data of an object are tied together in the data struc
ture.
Data is hidden and cannot be accessed by external functions.
Objects may communicate with each other through functions.
New data and functions can be easily added whenever necessary.
Follows bottom-up approach in program design.
Object A Object B

Data Data

Communication
Functions Functions

Object C

Functions

Data

Fig. 1.6 Organization of data and functions in OOP

programming paradigms
Object-oriented programming is the most recent concept among
therefore important to have a working
and still meansdifferent things to different people. It is
proceed further. We define "object
definition of object-oriented programming before we
oriented programming as an approach that provides away of modularizing programs by
functions that can be used as templates
creating partitioned memory area for both data and Thus, an object is considered to be a
for creating copies of such modules on demand." and set of operations that can access
partitioned area of computer memory that stores data
the objects can be used in a variety
that data. Since the memory partitions are independent,
of different programs without modifications.

1.5 Basic Concepts of Object-Oriented Programming


concepts used extensively in object-oriented
It is necessary to understand some of the
programming. These include:

Objects
Classes
Object-Oriented Programming with C++

Data abstraction and encapsulation


Inheritance
Polymorphism
Dynamic binding
Message passing
We shall discuss these concepts in some detail in this section.

Objects
Objects are the basic run-time entities in an object-oriented system. They may
person, a place, a bank account, atable of data or any item that the program has to represent ;
They may also represent user-defined data such as vectors, time and lists. handlk
problem is analyzed in terms of objects and the nature of communication between Programming
Program objectsshould be chosen such that they match closely with the real-world them
Objects take up space in the. memory and havean associated address like arecord in objects
or a structure in C. Pasceal
When a program is executed, the objects interact by sending messages to one
anothe
For example, if customer"and "account" are two objects in a program, then the cust
object may send a message to the account object requesting for the bank balanee e
object contains data, and code tomanipulate the data. Objects can interact without ha:
to know details of each other's data or code. It is sufficient to know the type of
accepted, and the type of response returnd by the objects. Although different auth messag:
represent them differently, Fig. 1.7 shows two notations that are popularly used in ohie
oriented analysis and design.

Object: STUDENT STUDENT


DATA Total
Name
Date-of-birth
Marks
Average
FUNCTIONS
Total
Average Display
Display

Fig. 1.7 Twoyays of representing an object

Classes
We just mentioned that objects
set of data and code of an objectcontain data, and code to manipulate that data. Theentir
can be made a user-defined
data type with the e
Principles of Object-Oriented Programming

class. In fact, objects are variables of the type class. Once


a class has been defined, we can
object is associated with the data
create any number of objects belonging to that class. Each
is thus a collection of objects of similar
of type class with which they are created. A class members
and orange are of the class fruit. Classes are
type. For example, mango, apple programming language. The
user-defined data types and behave like the built-in types of a used to create an integer
syntax
syntax used to create an object is no different than the
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.

Data Abstraction and Encapsulation


as
into a single unit (called class) is known
The wrapping up of data and functions striking feature of a class. The data is not
encapsulation. Data encapsulation is the most
functions which are wrapped in the class can
accessible to the outside world, and only those
between the object's data and the program.
access it. These functions provide the interface
by the program is called data hiding or
This insulation of the data from direct access
information hiding.
essential features without incluing the
Abstraction refers to the act of representing defined
background details or explanations. Classes
use the concept of abstraction and are
weight and cost, and functions to operate on
as a list of abstract attributes such as size, be
essential properties of the objects that are to
these attributes. They encapsulate all the data members because they hold information.
called
created. The attributes are sometimes member functions.
functions that operate on these data are sometimes called methods or
The
abstraction, they are known as Abstract
Data
Since the classes use the concept of data
Types (ADT).

Inheritance
one class acquire the properties of objects of
Inheritance is the process by which objects of bird
supports the concept of hierarchical classification. For example, the
another class. It 'bird'. The principle
which is again a part of the classcharacteristics with
'robin' is a part of the class flying bird'
each derived class shares common
behind this sort of division is that
illustrated in Fig. 1.8.
theclass from which it is derived as

concept of inheritance provides the idea of reusability. This means that we


In 00P, the without modifying it. This is possible by
additional features to an existing class features of
can add The new class will have the combined
derivinga new class from the existing one. allows
classes. The real appeal and power of the inheritance mechanism is that it tailor
both the almost, but not exactly, what he wants,
and to
programmer to reuse a class that is therest
the
such a way that it does not introduce any undesirable side-effects into
the class in
of the classes.
Programming with C++
Object-Oriented
10
Note that ench sub-class defines only those features that are unique to it. Without tha
include all of its features,
have to explicitly
of elassifiention, each class would
Bird
Attributes
Feathers

Lay eggs

Nonflying Bird
Flying Bird
Attributes Attributes

Robin Swallow .Penguin Kiwi


Attributes Attributes Attributes Attributes

Fig.1.8 Property inheritance

Polymorphism
Polymorphism is another important O0OP
ability to take more than one concept. Polymorphism, a Greek
instances. The behaviour form. An operation may exhibit different term, means the
consider the operation of depends upon the types of data behaviours in different
the operands are addition. For two numbers, used in the operation. For example,
strings, then the operation
The process of making the operation will
known as operator an would produce a third stringgenerate
operator to exhibit different by
a sum. I
overloading. behaviours in differentconcatenation.
instances is
Figure 1.9 illustrates
and different that a single
several different types of arguments. function name can be used
This is something to handle
different types ofmneanings depending on the context.
tasks is known similar to a different nun
as function Using a single particular word having
Polymorphism plays overloading. function name to pe
structures to share the samean important role in
allowing objects having
external interface. This
different internal
means that a
general class of opera
Principles of Object-Oriented Programming 11

may be accessed in the same manner even though specific actions associated with each
operation may differ, Polymorphism is extensively used in implementing inheritance.

Shape

Draw ()

Circle object Box object Triangle object

Draw (circle) Draw (box) Draw (triangle)

Fig, 1.9 Polymorphisn

Dynamic Binding
Binding refers to the linking of a procedure call tothe code to be executed in response to the
call. Dymamic binding (also known as late binding) means that the code associated with a
with
given procedure call is not known until the time of the call at run-time. It is associated
polymorphism and inheritance. A function call associated with a polymorphic reference
depends on the dynamic type of that reference.
Consider the procedure "draw" in Fig. 1.9. By inheritance, every object will have this
procedure. Its algorithm is, however, unique toeach 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.
The process of programmingin an object-oriented language, therefore, involves the following
basic steps:
1. Creating classes that define objects and their behaviour,
2. Creating objects from class definitions, and
3. Establishing communication among objects.
Objects communicate with one another by sending and receiving information much the
same way as people pass messages to one another. The concept of message passing makes it
easier to talk about building systems that directly model or simulate their real-world
counterparts.
Programming with C++
12 Object Oriented

object is a request for execution of a procedure, and


an
A message for
object that generates the desired.
invoke afunction (procedure)in the receiving object, the name of the function result.
involves specifving the name of the
hereloreMes
passing
the information to be sent. Example: (message):
employee.salary (name);

information
object
message

Objects have a life cycle. They can be created and destroyed. Communicatiom
object is feasible as long as it is alive. with

L.6 Benefits of OOP

OOP offers several benefits to both the program designer and the user.
contributes to the solution of many problems associated with the development and Object-orientatig
of software products. The new technology promises greater programmer productivity, het
quality of software and lesser maintenance cost. The principal advantages are:
Through inheritance, we can eliminate redundant code and extend the use of evi.
ing classes.
We can build programs from the standard working modules that communicate wit
one another, rather than having to start writing the code from scratch. This lead:
to saving of development time and higher productivity.
The principle of data hiding helps the programmer to build secure
cannot be invaded by code in other parts of the program. programs tha
It is possible to have multiple instances of an object to
ference. co-exist without any inter
It is possible to map objects in the problem domain
It is easyto partition the work in a project based to those in the program.
on objects.
The data-centered design approach enables us to
implementable form. capture more details of amodel
Object-oriented systems can be ea_ily upgraded from small to
Message passing techniques for communication large systems.
face descriptions between objects makes the inte
with external systems much simpler.
Software complexity can be easily managed.
While it is possible to incorporate these
importance depends on the type of theallproject features in an obiect-oriented system, t
and the preference of the programmer. :
are a number of issues that need to be tackled
to reap above. F
some of the benefits stated

You might also like