You are on page 1of 16

OBJECT ORIENTED PROGRAMMING

LANGUAGE

The object oriented approach lays more emphasis on data rather than
function and does not allow data to be accessed freely in the system.

The basic principle of OOP approach is to combine both data & function
that operate on data into a single unit. Such a unit is called an object. The
only way to access the data of an object is by its function. This secures
data from any accidental modification from the outside function. The OOP
approach decomposes a problem into number of units called objects.

Object 1
Dat
a

Fun
()

Fun Fun
() ()

Dat Dat
a a

Object 2 object 3

It is clear that data of an object can be accessed only by the function of


that object. The major motivating factor in the invention of object oriented
approach is to remove some of the flaws encountered in the procedural
approach. 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.
FEATURES OF OBJECT ORIENTED PROGRAMMING:

1 Emphasis is on data rather than procedure.

2 Programs are divided into what are known as objects.

3 Data structures are designed such that they characterize the objects.

4 Functions that operate on the data of an object are tied together in the
data structure.

5 Data is hidden and cannot be accessed by external functions.

6 Objects may communicate with each other through functions.

7 New data and functions can be easily added whenever necessary.

8 Follows bottom-up approach in program design.

BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING:

It is necessary to understand some of the concepts used extensively in


object oriented programming. These include:

 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. They may also represent user-
define 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.

An object is an instance of a class. It can be uniquely identified by its


name and defines a state which is represented by the values of its
attributes at a particular time. Each object posses the following
characteristics:

1 Each object has an identity and are distinguishable.

2 Object in the system communicate with each other using the concept of
message passing.

3 Data & function are binded together to form an object.

4 Object represents the basic run time entities is an object oriented


system. They occupy space in memory that stores its data which is
manipulated by its functions.

CLASSES: -

A class is a group of objects with same attributes and common


behaviours. It is just a template or a blueprint to create objects. As object
combine data & function. Class represents both data and function and
manipulate it. A class is the implementation of an abstract data type
(ADT). It defines attributes and methods which implement the data
structure and operations of ADT, respectively. Once a class has been
defined, we can create any number of objects belonging to that class.

DATA ABSTRACTION AND ENCAPSULATION: -

The wrapping up of data and functions into a single unit is known as


encapsulation. Data 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. 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.

Abstraction refers to the act of representing 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. They
encapsulate all the essential properties of the object that are to be
created. The attributes are sometimes called data members because they
hold information. The functions that operate of 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 concepts 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.

BIRD

Attributes

Flying Birds Non flying


Birds
Attributes
Attributes

Penguin Kiwi
Robin Swallow
Attributes Attributes
Attributes Attributes
Property Inheritance

POLYMORPHISM: -

It is a very powerful concept that allows the designing of amazing


flexible applications. The word polymorphism is derived from two Greek
words Poly which means many and morphs which means forms. So,
Polymorphism means the ability to take many forms. It can be defined as
the ability to use the same name for two or more related but technically
different tasks. In other words, polymorphism can also be defined as one
interface that can be used to perform related but different tasks.
Polymorphism plays an important role in allowing objects having different
internal structures to share the same external interface. This means that a
general class of operations may be accessed in the same manner even
though specific actions associated with each operation may differ.
Polymorphism is extensively used in implementing inheritance.

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. A
function call associated with a polymorphic reference depends on the
dynamic type of that reference. Binding is of two types:

1 Static Binding or Early Binding

2 Dynamic Binding or Late Binding

In static binding, the linking of function call to the code of the function to
be executed in response to the function call is made at the compile time.
In other words, binding is performed at the compile time. Through static
binding makes the program efficient and faster but the flexibility of the
program becomes poor.

In dynamic binding the linking of function call to the code of the function
to be executed in response to the function call is made at run-time. The
code of the function to be linked with function call is unknown until it is
executed. The main advantage of dynamic binding is greater flexibility
use to create class libraries that can be reused & executed. The main
drawback of dynamic binding is that there is loss of execution speed of
the program to some extent.

MESSAGE PASSING: -

In OOPs different objects communicate with each other using the


concept of message passing. From the programmer point of view,
message passing provides an easy way of modelling real world problems
on the computer. Objects communicate with each other in the same way
as human beings exchange message among themselves. Message
passing involves sending a message from one object to another object in
the system. A message to an object is treated as a request for the
execution of its function. On receiving the message, the suitable function
of the object is involved and the desired results ore generated. A message
comprises of the name of the object, name of the function and any
information to be sent to the object. For example if a user wants to check
the balance of the account, then he simply sends the message getbalance
to the object account by passing the information account_number. To
solve a particular problem using OOPs, the following steps are followed:

1 Identifying the classes in the system and specify data & function that it
contains.

2 Define the required objects.

3 Establish the communication among object through message passing.


CLASSES
A class is a way to bind the data and its associated functions together. It
allows the data and function to be hidden, if necessary, from external use.
When defining a class, we are creating a new abstract data type that can
be treated like any other built in data type. Generally, a class specification
has two parts:

1 Class declaration

2 Class function definitions

The class declaration describes the type and scope of its members. The
class function definitions describe how the class functions are
implemented. The general form of a class declaration is:

Syntax:

class class_name

private:

variable declarations;

function declaration;

public:

variable declaration;
function declaration;

The class declaration is similar to a structure declaration. The keyword


class specifies, that what follows is an abstract data of type class_name.
The body of a class is enclosed within braces and terminated by a
semicolon. The class body contains the declaration of variables and
functions. These functions and variables are collectively called class
members. They are usually grouped under two sections, namely, private
and public to denote which of the members are private and which of them
are public. The class members that have been declared as private can be
accessed only from within the class. On the other hand, public members
can be accessed from outside the class also. The data hiding is the key
feature of object-oriented programming. The use of the keyword private is
optional. By default, the members of a class are private. If both the lables
are missing, then, by default, all the members are private. Such a class is
completely hidden from the outside world and does not serve any
purpose.

For example:

Class item

int number;

float cost;

private:

void getdata();

void putdata();

};

Member functions can be defined in two places:

1 Outside the class definition.

2 Inside the class definition.

Outside the Class Definition:


Member functions that are declared inside a class have to be defined
separately outside the class. Their definitions are very much like the
normal functions. They should have a function header and a function
body. The general form of a member function definition is:

Syntax:

return_type class_name : : function_name (argument declaration)

Function body;

Inside the Class Definition:

Another method of defining a member function is to replace the function


declaration by the actual function definition inside the class. For example:

Class item

int number;

float cost;

public:

void getdata()

cout<< “ Enter Item Number: “;

cin>>number;

cout<< “ Enter Cost: “;

cin>> cost;

void putdata()

cout<<number<<endl;
cout<<cost<<endl;

};

ACCESS SPECIFIERS

The three access specifiers are:

1. Private

2. Public

3. Protected

The public, private and protected access specifiers are followed by a


special symbol colon(:).

1 Private:-

In the private access specifier, a data member can be accessed and


processed by the member function and the friend function of this class.
These can be use to read or write private data members. The private
member can’t be accessed by the outer class or the outside the class.

2 Public:-

In the public access specifier, data members can be accessed by all


the member functions and the friend function by the other classes or
outside this class. It is also called a member function operations or
methods or interfaces which are used to outside the class. So any
member function or friend can send messages to an object of this class
through these methods. The public data member can always read and
write the data outside the functions of class from any location.

3 Protected:-

In the protected access specifier, the data members can be


accessed and processed by the member function and the friend function
of the class in which these are declared. These functions can be accessed
by the member functions and friend function derived from this class. The
protected data members and member function is not accessible outside
the class.

CONSTRUCTORS

A constructor is a special member function used for automatic


initialization of an object. Constructors are used for initializing values to
the member data element of a class. In C++, constructor is also a
member of the class and has the same name as that of the class. If you
have not defined any constructor, then C++ will automatically define a
constructor. Constructor function does not return any value. It is only used
for initialization and not used for input/output operations. Constructors are
modified outside the class provided the member data are declared public.

Syntax:

class class_name

private:

data_type data members;

public:

class_name();

It is a special member function whose task is to initialize the objects of its


classes. The constructor is invoked whenever an object of its associated
class is created. It is called constructor because it constructs the values of
data members of the class.

CLASSIFICATION OF CONSTRUTOR:

Mainly constructor are classified in four categories:

1 Default Constructor

2 Parameterized Constructors

3 Copy Constructor

4 Dynamic Constructor

1 Default Constructor:-

A constructor that accepts no parameters or arguments is called the


default constructor. No object can be created without default constructor.
With default constructor, objects are created just the same way as
variables of other data types are created.

e.g. class abc m1;

will create the object m1 of type abc by invoking the default constructor.
But when the constructor accepts some parameters, the initial value must
be passed at the time of object creation.

Default constructor are further categorized into three ways:

a) Default Constructor by the Compiler

b) Default Constructor by the programmer without default arguments.

c) Default Constructor by the programmer with default arguments.

a)Default Constructor by the Compiler:-

When a user defined class doesn’t contain an explicit constructor,


the compiler automatically supplies a default constructor having no
arguments.

e.g. class abc

{ int a;
Public:

Void read();

Void write;

Void main()

Class abc m1; //uses default constructor for creating object m1

m1.read();

m1.write();

b)Default Constructor by the Programmer without Default Arguments:-

A constructor is default constructor provided by the programmer


without any argument, with or without any initial value is explained.

class abc

private:

int a;

float b;

public:

abc() //default constructor without argument

a=0;

b=1.2;

};

c)Default Constructor by the Programmer with Default Arguments:-


A constructor is default constructor provided by the programmer
with at least one argument, i.e. initialization of value in the arguments.

e.g. class abc

private:

int rm;

float fees;

public:

abc(int a=20, float b=126.23);

void write()

cout<<rm;

cout<<fees;

};

abc : : abc(int a, float b)

rm=a;

fees=b;

void main()

class abc m1(32);

m1.write();

abc m2(54, 320.66);

m2.write();

}
2 Parameterized Constructors:-

This constructor works similar to the default constructor by the


programmer with default argument. The basic difference is execution
speed, which is more in the case of parameterized constructor. The
constructor which may take argument is called parameterized constructor.
The parameterized constructor allows us to initialize the various data
elements of different objects with different value when they are created.
This can be done by passing different values as argument to the
constructor function, when the objects are created.

Syntax:

class class-name

private:

data members;

public:

class-name (argument list)

//parameterized constructor

};

3 Copy Constructor:-

A copy constructor is used to declare and initialized an object from


another object. The process of initializing through a copy constructor is
known as copy initialization. A copy constructor takes a reference to an
object of the same class as itself as an argument. A reference variable has
been used as an argument to the copy constructor. We can’t pass the
argument by value to a copy constructor.
4 Dynamic Constructor:-

The constructors can also be used to allocate memory while creating


objects. This will enable the system to allocate the right amount of
memory for each object when the objects are not of the same size, thus
resulting in the saving of memory. Allocation of memory to objects at the
time of their construction is known as dynamic construction of objects.
The memory is allocated with the help of the new operator.

You might also like