Professional Documents
Culture Documents
Communication
Object 3
Objects
Representing an object
Classes
A class is defined as a collection of similar objects. i.e objects with
the same data structure and functions are grouped into a class.
Each object is said to be an instance (variables) of the type class.
Once the class has been defined, we can create any number of
objects belonging to that class.
Classes are user-defined data types and behave like the built-in
types of a programming language.
All objects of the particular class occupy equal memory size.
For example chair, tables are members of the class furniture. If
furniture has been defined as a class, then the statement
furniture chair;
will create an object chair belonging to the class furniture.
Each object must be a member of any one class
In class definition, the data are declared and the functions are
defined.
The objects contain the value of the data and the action of the
current function.
DATA ABSTRACTION
Data abstraction is defined as a collection of data and
methods (functions)
Abstraction refers to the act of representing essential
features without including the background details or
explanations.
Data abstraction is the ability to create user defined
data types for modeling real world objects using built-
in data types and a set of permitted operators.
Classes use the concept of abstraction and are defined
as a list is abstract attributes such as size, weight and
cost, and functions to operate on these attributes.
Since the classes use the concept of data abstraction
they are known as Abstract Data Types (ADT).
DATA ENCAPSULATION
The process of combining data and methods in a
single logical unit is called encapsulation
The wrapping up of data and functions (that
operate on the data) into a single unit (called
class)
The only way to access the data is provided by
the functions which are wrapped in the class. The
data is not accessible to the outside world. These
functions provide the interface between the
objects data and program.
It is a technique used to protect the information
in an object from other objects. This concept is
also called as data hiding
INHERITANCE
Inheritance is the process of deriving new classes
from the existing classes. The derived classes
contain all attributes and functions of existing
classes with its own attributes and functions.
It supports the concept of hierarchical
classification.
In OOP, the inheritance principle provides the
idea of reusability.
The existing classes are called base classes and
the inherited classes are called derived classes.
The structure of inheritance is like a tree
structure.
INHERITANCE
From the figure, the class vehicle is divided into subclasses Two wheeler,
Three wheeler and Four wheeler. The principle in this sort of division is that each
sub classes shares common characteristics with the class from which it is derived.
Cars, buses, and motor cycles all have wheels and an engine. These are the
characteristics of vehicles. In addition to these characteristics each sub class also
has its own characteristics.
POLYMORPHISM
Another important OOP concept is polymorphism.
Polymorphism is a Greek term, means the ability for a
message or data to be processed in more than one form
Polymorphism is a technique used to write more than one
function definition with same function name i.e. the same
operation may behave differently on different classes.
For example consider the operation of adding for two
numbers, the operations will generate a sum. If the
operands are strings, then the operations would produce a
third string by concatenation.
Polymorphism is extensively used in implementing
inheritance.
The following figure explains the concept of polymorphism
POLYMORPHISM
In the figure, the function Area is defined in all the classes. But the
operation of the function Area is different.
In the circle class, the Area function calculates the area of circle with the
given radius value. In the square class, the Area function calculates the area of
square with the given side value. The triangle class, the Area function
calculates the area of triangle with the given breadth and height values.
DYNAMIC BINDING
Binding is defined as the connection between the function call and its
corresponding program code to be executed. There are two types of
Binding
- Static Binding
- Dynamic Binding
Static Binding
This type of binding occurs during compile time
Dynamic Binding
The binding occurs during run time. It 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. This is also
called late binding.
MESSAGE PASSING
An object oriented program consists of a set of objects
that communicate with each other. The process of
programming in an object oriented language, it
involves the following steps.
Creating classes that define objects and their behavior
Creating objects from class definition
Establishing communication between objects
Objects are communicated with one another by
sending and receiving information in the form of
messages.
A message for an object is a request for execution of a
procedure and therefore will invoke a function in the
receiving object that generates the desired result.
MESSAGE PASSING
The general form is
Where
Object_name the name of the object
message name of the function to be executed
information data to be send
Eg. Square.area(10);
Object - Square
message area
information 10
In this example, area is a message sent by the object square to execute the
function area with the data 10.
BENEFITS OF OOPS
The following are the some of the important advantages of object
oriented programming.
Code Reusability
In Object oriented programming, programs and functions that are written
by a user can be reused by other users. With this advantage, we can
eliminate redundant code
Productivity
OOP creates many new data types using basic data types
Code Sharing
In OOP, it allows the programmer to share data and functions which are
common to more than one class by defining it in the parent classes in the
hierarchical order
BENEFITS OF OOPS
Maintenance
Program maintenance is easily done in OOPS because OOP is full of
objects. If particular part of the program needs modification it can be
modified easily.
Data hiding
In OOPS, data hiding helps the programmer to build secure programs that
cannot be invaded by code in other parts of the program
Power
The power is obtained by the special features such as polymorphism,
operator overloading, function overloading and inheritance etc.
Reduced complexity of a problem
In OOP, the given problem is viewed as a collection of different objects.
Each object is responsible to do some specific task. The problem can be
solved by interfacing the objects. It reduces the complexity of the program
design.
BENEFITS OF OOPS
Message passing technique
Message passing technique for communications between
objects makes the interface descriptions with external
systems much simpler
Portable
OOP concepts can be used from micro computer to super
computer. It is not hardware dependent.
Extendability
Data
No entry to X
Private area Functions
Public area
Data
Entry allowed
Functions
to public area
Classes
A simple class example
class item
{
int number;
float cost;
public:
void getdata (int a, float b);
void putdata (void);
};