Introduction to OOP (Object Oriented Programming)
Introduction to OOP
Object-oriented programming – As the name suggests uses objects in programming. Object Oriented Programming is defined as an approach that provides a way of modularising programs by creating a partitioned memory area for both data and functions that can be used as a template for creating copies of such modules on demand.
Writing an OOP involves creating classes, creating objects from these classes, and creating applications that are stand-alone executable programs that use those objects. After being created, classes can be re-used over and over again, to develop new programs.
Features of OOP
- Class: Class is a user defined data type. A class is a logical abstraction. It is a template that defines the form of an object. A class specifies both code and data. When defined class, you declare data that it contains, and the code that operates on the data. Data is contained in instance variable defined by the class known as data members and code is contained in functions known as member functions. The code and the data that constitute a class are called members of the class.
- Object: Object is a variable of a class. It is an identifiable entity with specific characteristics (data members) and behaviour (member functions). Defining an object is similar to defining a variable of any data type.
object
#include<iostream.h>
#include<conio.h>
class addition
{
private : int a,b,c;
public :
void getdata()
{
cout<<"\nEnter value of a: ";
cin>>a;
cout<<"\nEnter value of b: ";
cin>>b;
}
void add()
{
c=a+b;
cout<<"\nAddition is: "<<c;
}
};
void main()
{
addition A;
A.getdata();
A.add();
} - Data Encapsulation (Data Hiding): Encapsulation is a programming mechanism that binds together the code and the data it manipulates and that keeps both safe from outside inter reference and misuse.
The basic unit of encapsulation in C++ is the class. Within a class, code or data or both maybe private to that object or public. Private code or data is known to and accessible by only another part of the object. That is, private code or data cannot be accessed by a piece of the program that exists outside the object. This insulation of data from direct access by the program is called Data Encapsulation or Data Hiding. - Data Abstraction: Data abstraction is one of the most essential and important feature of object oriented programming in C++. Abstraction means displaying only essential information and hiding the details. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation.
Consider a real life example of a man driving a car. The man only knows that pressing the accelerators will increase the speed of car or applying brakes will stop the car but he does not know about how on pressing accelerator the speed is actually increasing, he does not know about the inner mechanism of the car or the implementation of accelerator, brakes etc in the car. This is what abstraction is. - Message Passing: Objects communicate with one another by sending and receiving information to each other. 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 results. Message passing involves specifying the name of the object, the name of the function and the information to be sent.
- Polymorphism: The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. Consider an example of real world... A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee. So the same person posses different behaviour in different situations. This is called polymorphism.
An operation may exhibit different behaviours in different instances. The behaviour depends upon the types of data used in the operation. C++ supports operator overloading and function overloading which are examples of polymorphism. - Inheritance: The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object Oriented Programming.
a. Sub-class/Derived Class - The class that inherits properties from another class is called Sub class or Derived Class.
b. Super Class/ Base Class - The class whose properties are inherited by sub class is called Base Class or Super class.
c. Reusability - Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class. - Dynamic Binding: Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding also known as Late Binding means that the code associated with a given procedure call is not known until the time of call (until run-time). It is associated with polymorphism and inheritance. A function call associated with a polyforming reference depends on the dynamic type of that reference.
- Persistency: The phenomenon where the object (data) outlives the program execution time and exists between executions of a program is known as persistence. In C++, user can use file streams in a program which is the best example of persistency.
- Delegation: Delegation is a way of making object composition as powerful as inheritance. In delegation, two objects are involved in handling a request: a receiving object delegates operations to its delegate. This is analogous to subclasses deferring requests to parent classes.
Applications of OOP
There are many applications of Object Oriented Programming Language. Few of them are -
- User interface design such as windows, menu.
- Real Time Systems
- Simulation and Modeling
- Object oriented databases
- AI and Expert System
- Neural Networks and parallel programming
- Decision support and office automation systems etc.
Comparison between OOP and POP
![]() |
Comparison between OOP and POP |
Advantages of OOP
- Simplicity: Software objects model the real world objects, so the complexity is reduced and the program structure is very clear.
- Modularity: Each object forms a separate entity whose internal workings are decoupled from other parts of the system.
- Modifiability: It is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods.
- Extensibility: Adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones.
- Maintainability: Objects can be maintained separately, making locating and fixing problems easier.
- Re-usability: Objects can also be reused within an across applications. The reuse of software also lowers the cost of development. More effort is put into the object-oriented analysis and design, which lowers the overall cost of development.
This comment has been removed by a blog administrator.
ReplyDelete