Jan 2, 2015

Four pillars of OOP (C++)


The four pillars of object oriented programming development :

1. Encapsulation :



                 Encapsulation is one of the fundamental concept of OOP's. It refers to the bundling (combining) of data with the methods (member functions that operate on that data) into a single entity (like wrapping of data and methods into a capsule) called an Object. It is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties, direct access with them. Publicly accessible methods are generally provided in the class to access the values, and other client classes call these methods to retrive and modify the values within the object.
                 
                  We generally call the functions that are declared in c++ are called as Member functions. But in some Object Oriented languages (OO's) we call them as Methods. Also the data members are called Attributes or Instance variables.
Take a look with an example :



                  In the above example u can observe that all tha data members and methods are binded into a object called “obj”. In Encapsulation we can declare data members as private, protected, or public. So we can directly access the data members (here count).
  
Features and Advantages of concept of Encapsulation :


  • Makes maintenance of program easier .
  • Improves the understandability of the program.
  • As the data members and functions are bundled inside the class, human errors are reduced.
  • Encapsulation is alone a powerful feature that leads to information hiding, abstract data type and friend functions.
 
2.Data Hiding :                   
                  
              The concept of encapsulation shows that a non -member function cannot access an object's private or protected data. This has leads to the new concept of data hiding. Data Hiding is a technique specifically used in object oriented programming(OOP) to hide the internal object details (data members or methods) from being accessible to outside users and unauthorised parties. The Private access modifier was introduced to provide that protection. Thus it keeps safe both the data and methods from outside interference and misuse. In data hiding, the data members must be private. The programmer must explicitly define a get or set method to allow another object to read or modify these values.
  
For example :
 


                 Here the data member count is private. So u can't access it directly. Instead u can access it, by the methods of the same class. Here It is accessed by the disp method .
  • In some cases of two classes, the programmer might require an unrelated function to operate on an object of two classes. The programmer then able to utilize the concept of friend functions.
  • It enhances the security and less data complexity

  • The focus of data encapsulation is on the data inside the capsule while that data hiding is concerned with restrictions and allowance in terms of access and use.

    3.Inheritance:


                    Reusability is yet another important feature of OOP's concept. It is always nice, if we could reuse something instead of creating the same all over again. For instance, the reuse of a class that has already tested and used many times can save the effort of developing and testing it again.
                      C++ supports the concept of reusability. Once a class has written and tested, it can be adapted by other programmers to suit their requirements. This is done by creating new classes, and reusing the existing properties. The mechanism of deriving a new class from old one is called Inheritance. The old class is referred as “Base class”. And the new one created is called as Derived class”. 

     


    Here is an example :



    Here a is declared in the base class e and is inherited in the class c so that we need not to it declare again.
    • Reusing existing code saves time and increases a program's reliability.
    • An important result of reusability is the ease of distributing class libraries. A programmer can use a class created by another person or company, and without modifying it, derive other classes from it that are suited to their requirements.

    4.POLYMORPHISM:

                  Polymorphism -- “one interface, multiple methods.”
                      

                 Polymorphism occurs when there is a hierarchy of classes and they are related by inheritance. Generally, from the Greek meaning Polymorphism is the ability to appear in many forms (having multiple forms) and to redefine methods for derived classes. 
     
              A real-world example of polymorphism is a thermostat. No matter what type of furnace your house has(gas, oil, electric, etc), the thermostat works in the same way. In this case, the thermostat (which is the interface) is the same, no matter what type of furnace (method) you have. For example, if you want a 70-degree temperature, you set the thermostat to 70 degrees. It doesn't matter what t ype of furnace actually provides the heat.
           This same principle can also apply to programming. For example, you might have a program that defines three different types of stacks. One stack is used for integer values, one for character values, and one for floating-point values. You can define one set of names, push( ) and pop( ) that can be used for all three stacks. In your program, you will create three specific versions of these functions, one for each type of stack, but names of the functions should be same. The compiler will automatically select the right function based upon the data being stored. Thus, the interface to a stack -- the functions push( ) and pop( ), are the same no matter which type of stack is being used. The individual versions of these functions define the specific implementations (methods) for each type of data.
    • Polymorphism reduces complexity by allowing the same interface to be used to access class of actions. It is the compiler's job to select the specific action (method) to each situation.

    Filed under  |