Custom Search

Monday, March 4, 2013

OOPS Concept in C++


OOPS Concept in C++
OOPS Concept in C++

  • Object oriented programming is method of programming where a system is considered as a collection of objects that interact together to accomplish certain tasks. 
  • Objects are entities that encapsulate data and procedures that operate on the data.

  • The main purpose of object oriented programming is to simplify the design, programming and most importantly debugging a program. 
  • So to modify a particular data, it is easy to identify which function to use. 
  • To add additional features it is easy to identify where to add functions and its related data.

Following are the basic elements of Object oriented programming(OOPS)
·        Object
·        Classes
·        Inheritance
·        Dynamic Binding
·        Polymorphism
·        Message passing
·        Encapsulation

Object

  • Objects are instance of a class, that interact with each other at runtime. 
  • In OOPs, Objects are decalred at the end of the class definition or after the "}" braces. 
  • They can be of any type based on its declaration

Example:

#include
class Cube
{
 public:
   int cub( val)
     {
      r = val*val*val;
      return r;
     }
   void show()
     {
       cout << "The cube is::" << r;
     }
private:
   int val, r;
}x;
void main()
 {
Clrscr();
   Cube x;
   x.cub(2);
   x.show();
getch();
 }

Result:

    The cbe is :: 8

Classes:

  • Classes has the data and its associated function wrapped in it.
  • Classes are also known as a collection of similar objects or objects of same type.
  • In the OOPs concept the variables declared inside a class are known as "Data Members" and the functions are known as "Member Functions".

Syntax:
   class class-name
    {
      private:
          variable declaration;
          function declaration;
      public:
          variable declaration;
          function declaration;

    };

Example:

#include
#include
class Square
  {
     public:
        int area( side)
          {
            a =side*side;
            return a;
          }
     void show()
     {cout << "The area is::" << a;}
    private:
      int side, a;
  }x;
void main()
 {
      Clrscr();
      Square x;
      x.area(10);
      x.show();
 }
Result:

    The area is:: 100

Inheritance

  • Inheritance is a method by which new classes are created or derived from the existing classes. 
  • Using Inheritance some qualities of the base classes are added to the newly derived class, apart from its own features The advantage of using "Inheritance" is due to the reusability of classes in multiple derived classes. 
  • The ":" operator is used for inherting a class.

Following are the different types of inheritance followed in C++.
1.     Single inheritance
2.     Multiple inheritance
3.     Hierarchial inheritance
4.     Multilevel inheritance
5.     Hybrid inheritance

Example:
#include
#include
class Value
  {
   protected:
    int val;
   public:
    void set_values (int a)
      { val=a;}
  };
class Square: public Value
  {
   public:
    int square()
      { return (val*val); }
  };
int main ()
{
Clrscr();
 Square sq;
  sq.set_values (5);
  cout << "The square of 5 is::" << sq.square() << endl;
  return 0;
}

Result:

The square of 5 is :: 25

Dynamic Binding 

  • Dynamic Binding refers to linking a procedure call to the code that will be executed only at run time. 
  • The code associated with the procedure in not known until the program is executed, which is also known as late binding.

Polymorphism

  1. Polymorphism is the ability of an object or reference to take many different forms at different instances. 
  2. These are of two types one is the "compile time polymorphism" and other one is the "run-time polymorphism".


Compile time polymorphism:

In this method object is bound to the function call at the compile time itself.

Run time polymorphism:

In this method object is bound to the function call only at the run time.

Message Passing is nothing but sending and receving of information by the objects same as people exchange information. So this helps in building systems that simulate real life. 
Following are the basic steps in message passing.
  • Creating classes that define objects and its behaviour.
  • Creating objects from class definitions
  • Establishing communication among objects

Message Passing

  • Message Passing involves specifying the name of objects, the name of the function, and the information to be sent.
Encapsulation

  • Encapsulation is the method of combining the data and functions inside a class. This hides the data from being accessed from outside a class directly, only through the functions inside the class is able to access the information.
  • This is also known as "Data Abstraction", as it gives a clear separation between properties of data type and the associated implementation details. 
  • There are two types, they are "function abstraction" and "data abstraction". 
  • Functions that can be used without knowing how its implemented is function abstraction. Data abstraction is using data without knowing how the data is stored.


No comments:

Post a Comment