Sunday, November 27, 2011

Object oriented programming

Why Object Oriented Programming (OOP)?
With continuously increasing hardware capabilities such as faster CPUs, better graphics and easier networking, users tend to expect software to have greater functionality. Features such as window-based GUI, transparent access to data stored in mini- or mainframe computers, and the ability to work with a networked environment. These features are particularly easy to implement if we follow object oriented approach.

E.g. An MSPaint window has multiple menus. A user may choose the menu of her choice. There is no predefined sequence which an application must follow. Every operation is user driven (or message driven). Hence each menu of mspaint can be thought of as an object. And every menu has got to do its task (its methods). This scenario perfectly fits in object oriented paradigm.



Three basic principles underlying OOP:
1. Data Abstraction
2. Inheritance
3. Polymorphism

1. Data Abstraction = Defining an ADT + Data hiding.

An ADT is a collection of variables together with the functions necessary to operate on those variables. You can think of the ADT as a template from which specific instances of objects can be created as needed. The term class is often used for this template. In fact, C++ provides the class declaration precisely for the purpose of defining an ADT.

In OOP, you can create an object from an ADT. An object is anything that has got an existence in real world. It is the instance of specific class.
e.g. If Man is a class, then you and I are objects. (I suppose no pet animal by mistake is reading this ;) ) Because you and I are visible, existing things. A class is abstract entity, while an object is real.

The functions that operate on data are called as methods. This term comes from the Smalltalk (early object oriented programming language). In C++, methods are often called the member functions of the class.

2. Inheritance:

Real world objects do not exist in isolation. Each object is related to one or more other objects. We often say that A is just like B, but A is different in .... and B does.... Here we are expressing object of type A in terms of those of type B.
This notion of defining a new object in terms of old ones, is called as inheritance.
We can  imagine a man class inheriting the data and behavior of animal class. i.e. A man exhibits respiration, locomotion, etc (which are functions of any animal)
Inheritance imposes a hierarchical relationship among classes, in which child class inherits data and functions from its parent. In C++, parent class is known as the base class and the child is the derived class.


3. Polymorphism:

Literally polymorphism means showing multiple (poly) forms (morphos). In the context of OOP, polymorphism refers to the fact that a single operation can have different behavior in different objects. In other words, different objects react differently to the same message.
e.g. consider addition of two numbers. We denote it by x + y ('+' is an operator, which internally calls a function for performing an addition). In this, x and y can be integers, floats, and even complex numbers too. But if x and y were strings, then '+' operator has to concatenate two strings, presenting the polymorphic behavior.