Principal of Object Oriented Programming
 
Introduction of OOP
 
Object
 
Object is an identifiable run-time entity with some characteristic and behavior. for example 'orange' is an object and its characters are shape, color etc.
 
Class
 
A class is a group of object that share common properties and relationship. for example fruit has been defined as a class, then the statement
 
fruit mango;
 
will create an object mango belonging to the class fruit.
 
Message
 
At runtime instances of classes, the objects, achieve the goal of the program by changing their states.
 
Consequently, we can think of our running program as a collection of objects. The question arises of how these objects interact? We therefore introduce the concept of a message.
 
A running program is a pool of objects where objects are created, destroyed and interacting. This interacting is based on messages which are sent from one object to another asking the recipient to apply a method on itself.
 
A message is a request to an object to invoke one of its methods. A message therefore contains.
 
the name of the method and the arguments of the method.
 
 
In this figure, the program consists of only four objects. These objects send messages to each other, as indicated by the arrowed lines. Note that the third object sends itself a message.
 
Data Abstraction
 
Abstraction refers to the act of representing essential features without including the background details.
 
To understand abstraction, lets an example. you are driving a car you only know the essential features to drive the car e.g. gear, steering etc.
 
But while driving the car you get into the internal details of the car such as wiring, battery etc.
 
What is happening inside is hidden from you. This is data abstraction where you know only the essential things to drive the car without including the background details.
 
Data Encapsulation
 
Encapsulation is the most fundamental concept of OOP. It is the way of combining both data and the functions that operate on data under a single unit.
 
The wrapping of the data and functions (that operate on the data) into a single unit (called classes) is known as encapsulation.
 
The only way to access the data is provided by the functions (that are combined along with the data). These function are called member functions in C++.
 
The data can not be accessed directly. If you want to read a data item in an object (an instance of a class), you call a member function in the object it will read the items and return the value to you. Encapsulation is just a way to implement data abstraction.
 
 
As you know encapsulation is an important feature of C++. How ever, encapsulation provides another important attribute : access control.
 
Through encapsulation you can control what part of program can access the member of a class.
 
C++ specifies three access modes:
 
a) Private: It means can be accessed only with in the class and not by the out side world. It is by default.
 
b) Public: It means visible to all any class in that program can access.
 
c) Protected: It means only visible to those who inherit that class.
 
Inheritance
 
Understanding inheritance is critical to understanding the whole point behind object oriented programming.
 
Definition:
 
Inheritance is the mechanism which allows a class A to inherit properties of a class B. We say "A inherits from B''. Objects of class A thus have access to attributes and methods of class B without the need to redefine them.
 
Inheritance is the capability of one class of things to inherit capabilities or properties from another class.
 
For instance we are humans. We inherit from the class 'humans' certain properties, such as ability to speak, breath eat drink etc but these properties are not unique to the humans it inherit some properties from class called 'Mammals' which again inherit some of its properties from from another class 'Animals'.
 
Another example is the 'Car' inherits its properties from the class 'Automobiles' which inherit its properties from another class 'Vehicles'.
 
 
Super Class/Subclass
 
Definition:
 
If class A inherits from class B, then B is called superclass of A. A is called subclass of B. Objects of a subclass can be used where objects of the corresponding superclass are expected.
 
This is due to the fact that objects of the subclass share the same behaviour as objects of the superclass.
 
In the literature you may also find other terms for "superclass'' and "subclass''. Superclasses are also called parent classes. Subclasses may also be called child classes or just derived classes.
 
Of course, We can again inherit from a subclass, making this class the superclass of the new subclass.
 
This leads to a hierarchy of superclass/subclass relationships. If you draw this hierarchy you get an inheritance graph.
 
A common drawing scheme is to use arrowed lines to indicate the inheritance relationship between two classes or objects. In our examples we have used "inherits-from''.
 
Consequently, the arrowed line starts from the subclass towards the superclass as illustrated in below figure:
 
A simple inheritance graph.
 
 
In the following sections an unmarked arrowed line indicates "inherit-from''.
 
Static and Dynamic Binding
 
In strongly typed programming languages you typically have to declare variables prior to their use. This also implies the variable's definition where the compiler reserves space for the variable.
 
For example, in Pascal an expression like:
 
var i : integer;
 
declares variable i to be of type integer. Additionally, it defines enough memory space to hold an integer value.
 
With the declaration we bind the name i to the type integer. This binding is true within the scope in which i is declared. This enables the compiler to check at compilation time for type consistency.
 
For example, the following assignment will result in a type mismatch error when you try to compile it:
 
var i : integer;
...
i := 'string';
 
We call this particular type of binding "static'' because it is fixed at compile time.
 
If the type T of a variable is explicitly associated with its name N by declaration, we say, that N is statically bound to T.
 
The association process is called static binding.
 
There exist programming languages which are not using explicitly typed variables.
 
For example, some languages allow to introduce variables once they are needed:
 
... /* No appearance of i */
 
i := 123 /* Creation of i as an integer */
 
The type of i is known as soon as its value is set. In this case, i is of type integer since we have assigned a whole number to it. Thus, because the content of i is a whole number, the type of i is integer.
 
If the type T of a variable with name N is implicitly associated by its content, we say, that N is dynamically bound to T. The association process is called dynamic binding.
 
Both bindings differ in the time when the type is bound to the variable.
 
Consider the following example which is only possible with dynamic binding:
 
if somecondition() == TRUE then
n := 123
else
n := 'abc'
endif
 
The type of n after the if statement depends on the evaluation of somecondition(). If it is TRUE, n is of type integer whereas in the other case it is of type string.
 
Polymorphism
 
Polymorphism allows an entity (for example, variable, function or object) to take a variety of representations.
 
Polymorphism is key to the power of object oriented programming. It is so important that languages don't support support polymorphism can not advertise them self as OO languages.
 
Polymorphism is the ability for a message or data to be processed in more than one form.
 
Polymorphism is a concept that support the capability of an object of a class to behave differently in response to a message or action.
 
Polymorphism is a property by which the same message can be sent to objects of several different classes, and each object can be respond in a different way depending on its class.
 
 
Abstract Classes
 
We introduce the new keyword abstract here. It is used to express the fact that derived classes must "redefine'' the properties to fulfill the desired functionality.
 
Thus from the abstract class' point of view, the properties are only specified but not fully defined.
 
The full definition including the semantics of the properties must be provided by derived classes.
 
A class A is called abstract class if it is only used as a superclass for other classes. Class A only specifies properties. It is not used to create objects. Derived classes must define the properties of A.
 
Template class
 
Definition:
 
If a class A is parameterized with a data type B, A is called template class. Once an object of A is created, B is replaced by an actual data type.
 
This allows the definition of an actual class based on the template specified for A and the actual data type.
 
Benefit of OOP
 
Some of the benefit of using OOP given here:
 
1. Through inheritance, we can eliminate redundant code and extend the use of existing classes.
 
2. Software complexity can be easily managed.
 
3. Object oriented system can be easily upgraded from small to large system.
 
4. It is easy to Partition the work in a project based on object.
 
5. We can build program from the standard working modules that communicate with one another, rather than having to start writing the code from scratch.
 
6. Message passing techniques for communication between object makes the interface description with external system much simpler etc.