Friday 14 June 2013

object oriented programming

Object-oriented programming (OOP) is a programming paradigm that represents concepts as "objects" that have data fields (attributes that describe the object) and associated procedures known as methods. Objects, which are usually instances of classes, are used to interact with one another to design applications and computer programs. C++ and Java are examples of object-oriented programming languages.
An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object's behavior. For example, the hand can grip something or a Student (object) can give the name or address
An object oriented program may be viewed as a collection of interacting objects, as opposed to the conventional model, in which a program is seen as a list of tasks (subroutines) to perform. In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects. Each object can be viewed as an independent "machine" with a distinct role or responsibility. Actions (or "methods") on these objects are closely associated with the object. For example, OOP data structures tend to "carry their own operators around with them" (or at least "inherit" them from a similar object or class)—except when they must be serialized.


There are different types of OOPs are used, they are
1.    Object
2.    Class
3.    Data Abstraction & Encapsulation
4.    Inheritance
5.    Polymorphism
6.    Dynamic Binding
7.    Message Passing

1) Object :
Object is the basic unit of object-oriented programming. Objects are identified by its unique name. An object represents a particular instance of a class. There can be more than one instance of an object. Each instance of an object can hold its own relevant data.
An Object is a collection of data members and associated member functions also known as methods.
For example whenever a class name is created according to the class an object should be created without creating object can’t able to use class.
The class of Dog defines all possible dogs by listing the characteristics and behaviors they can have; the object Lassie is one particular dog, with particular versions of the characteristics. A Dog has fur; Lassie has brown-and-white fur.
2) Class :
Classes are data types based on which objects are created. Objects with similar properties and methods are grouped together to form a Class. Thus a Class represents a set of individual objects. Characteristics of an object are represented in a class as Properties. The actions that can be performed by objects become functions of the class and is referred to as Methods.
For example consider we have a Class of Cars under which Santro Xing, Alto and WaganR represents individual Objects. In this context each Car Object will have its own, Model, Year of Manufacture, Colour, Top Speed, Engine Power etc., which form Properties of the Car class and the associated actions i.e., object functions like Start, Move, Stop form the Methods of Car Class.No memory is allocated when a class is created. Memory is allocated only when an object is created, i.e., when an instance of a class is created.

Classes are generally declared using the keyword class, with the following format:

class class_name {
  access_specifier_1:
    member1;
  access_specifier_2:
    member2;
  ...
  } object_names;

Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers.

All is very similar to the declaration on data structures, except that we can now include also functions and members, but also this new thing called access specifier. An access specifier is one of the following three keywords:privatepublic or protected. These specifiers modify the access rights that the members following them acquire:

  • private members of a class are accessible only from within other members of the same class or from theirfriends.
  • protected members are accessible from members of their same class and from their friends, but also from members of their derived classes.
  • Finally, public members are accessible from anywhere where the object is visible.

By default, all members of a class declared with the class keyword have private access for all its members. Therefore, any member that is declared before one other class specifier automatically has private access
3) Data abstraction & Encapsulation :
The wrapping up of data and its functions into a single unit is called Encapsulation.
When using Data Encapsulation, data is not accessed directly, it is only accessible through the functions present inside the class.
Data Abstraction increases the power of programming language by creating user defined data types. Data Abstraction also represents the needed information in the program without presenting the details.
Abstraction refers to the act of representing essential features without including the background details or explanation between them.
For example, a class cuboid would be made up of an length,breadth and height. To build the cuboid class, one does not need to know how the different components work internally, but only how to interface with them, i.e., send messages to them, receive messages from them, and perhaps make the different objects composing the class interact with each other.

class cuboid
{
  int length, breadth, height;
  float area, volume;
  public:
totalarea =2(length+breadth+height);
volume = length*breadth*height;
};
    
4) Inheritance :
Inheritance is the process of forming a new class from an existing class or base class.
The base class is also known as parent class or super class, the new class that is formed is called derived class.
Derived class is also known as a child class or sub class. Inheritance helps in reducing the overall code size of the program, which is an important concept in object-oriented programming.
It is classifieds into different types, they are
  • Single level inheritance
  • Multi-level inheritance
  • Hybrid inheritance
  • Hierarchial inheritance
       example:-
#include <iostream>
using namespace std;

class A {
   int data;
public:
   void f(int arg) { data = arg; }
   int g() { return data; }
};

class B {
public:
   A x;
};

int main() {
   B obj;
   obj.x.f(20);
   cout << obj.x.g() << endl;
//   cout << obj.g() << endl;
}



5) Polymorphism :
Polymorphism allows routines to use variables of different types at different times. An operator or function can be given different meanings or functions. Polymorphism refers to a single function or multi-functioning operator performing in different ways.
Poly a Greek term ability to take more than one form. Overloading is one type of Polymorphism. It allows an object to have different meanings, depending on its context. When an exiting operator or function begins to operate on new data type, or class, it is understood to be overloaded.
#include <iostream>
using namespace std;

class A {
   int data;
public:
   void f(int arg) { data = arg; }
   int g() { return data; }
};

class B {
public:
   A x;
};

int main() {
   B obj;
   obj.x.f(20);
   cout << obj.x.g() << endl;
//   cout << obj.g() << endl;
}

6) Dynamic binding :
It contains a concept of Inheritance and Polymorphism.
7) Message Passing :
It refers to that establishing communication between one place to another.



2 comments:

  1. is there are any disadvantages of oops over structural programming ???

    If yes then why we are still using it ?

    And can u please also elaborate the concept of Dynamic Binding?


    Thanx.....

    ReplyDelete
  2. Disadvantages of OOPS:-
    1. It requires good planning before writing the program, as the programs are larger coders spent more time actually writing the program.
    2. Object Oriented programs are slower than other programs, partially because of their size.

    These are the disadvantages of OOPS over structured programming language but we are still using it because it invloves the creation of objects, class and we can have inheritance, polymorphism, data hinding, data abstraction,etc as its properties..
    Data Binding:-in c++, data binding refers to make the data/variables and
    functions which operate on these data into a single unit.
    in c++ the unit is class.

    ReplyDelete