Followers

Monday, December 3, 2007

GENERAL OOPS CONCEPTS

PROGRAMMING PARADIGM

Paradigm means the approach to programming that is the organising principle of a program.

There are different programming paradigms:

  • Procedural
  • Modular
  • Object Oriented

Procedural Programming:

  • Empasis is on doing things.
  • Data is not important.
  • Languages support this paradigm by providing facilities for passing arguments to functions (subprograms) and returing values from functions (subprograms).

Example: To desing an Inventory Control program for a departmental store which keeps records of inventory (stock) purchased and consumed. The following code shows the procedural approach to handle the problem.

int item_code; //unique identification code of the product.

char category[15]; //category to which it bleogs like grocery, decorative, home furnishings, toys, etc.

char date_of_selling[10]; // date of selling the product

float sale_price; // selling price of the product

int unit_left; // total no. of units left

char date_of_purchase[10]; // date of buying the product

float purchase_price; // purchasing price of the product

int unit_purchased; // total no. of units bought

purchase( ); // function defined to purchase a product

selling( ); // function defined to sell a product

Modular Programming:

  • Large program is broken into smaller units called functions (sub-programs)
  • An extension of procedural approach because it groups a number of functions and the data they manipulate into a larger entity called module.
  • The problem with this approach is that since many modules or functions access same data, the way data is being stored becomes important.

Example: The above program of Inventory control can be written in the following manner using Modular approach.

Common data

int item_code; //unique identification code of the product.

char category[15]; //category to which it bleogs like grocery, decorative, home furnishings, toys, etc.

Module Selling

char date_of_selling[10]; // date of selling the product

float sale_price; // selling price of the product

int unit_left; // total no. of units left

// The module may contain some more functions related to the process of selling

Module Purchasing

char date_of_purchase[10]; // date of buying the product

float purchase_price; // purchasing price of the product

int unit_purchased; // total no. of units bought

//The module may contain some more functions related to the process of purchasing

Object Oriented Programming:

  • Defines the real world entities.
  • Views the problem in terms of objects and classes involved rather than functions for doing it.
  • An object is an identifiable entity with some characteristics and behaviour.
  • Object can store data and has its interface through functions.
  • A class is a group of objects that share common properties and relationships.
  • An object is an instance of a class.
  • For example, if we have a class Car with the general characteristics of a car like: steering wheel, seats, a motor, brakes, etc. Opel Astra, SX4 will be the objects that belong to class Car.

Example: The above discussed problem of Inventory control can be dealt in the following manner according to the OOP approach.

Class Item

{

int item_code; //unique identification code of the product.

char category[15]; //category to which it bleogs like grocery, decorative, home furnishings, toys, etc.

char date_of_selling[10]; // date of selling the product

float sale_price; // selling price of the product

int unit_left; // total no. of units left

char date_of_purchase[10]; // date of buying the product

float purchase_price; // purchasing price of the product

int unit_purchased; // total no. of units bought

purchase( ); // function defined to purchase a product



selling( ); // function defined to sell a product

}

Now you can create objects of the class item which will include the common features of the class item and also have their own characteristics and behaviour.

The class item can have the following objects: Grocery, Decorative, Stationary, Toys, Home Furnishings, etc.

THE CONCEPTS OF OBJECT ORIENTED PROGRAMMING

1. DATA ABSTRACTION

  • refers to act of representing essential feaures without including the background details or explanations.
  • For example while driving the car you do not get into the internal details of wiring, motor working, etc. You just change the gears or apply the brakes etc.

Example: As in the above example, To sell or buy a product you simply choose the option buy or sell. The processing required behind is of no conern to the user.

2. ENCAPSULATION

  • Combines data and functions that operate on that data into a single unit.
  • Wrapping up of data and functions (that operate on the data) into a single unit (called class) is known as encapsulation
  • Data is hidden and safe from alteration.
  • For example, while designing the car, the data i.e. steering wheel, acceleerator, brake, clutch, etc. and the process of moving a car forward, moving car backward, etc. are wrapped into a single unit called class. The user is just using the processes and are not bothered about the working of the process.
  • Thus, Encapsulation is a way to implement Data Abstraction.

Example: In the above example, the in-charges of different categories of items do not have access to data of the other category. Item is a class and Grocery, Decorative, Toys, etc. are the objects (instance) of a class.

3. MODULARITY

  • Partitioning the program into individual components is called Modularity.
  • Program is decomposed into set of cohesive (organised) and loosely coupled (less interdependent) modules.
  • Modularity in C++ is implemented through seperate compiled files.
    Module interface is placed in the header files.
  • For example, to design a music system it vcan be decomposed into the following modules: Speakers, CD-player, FM-player, Recorder, etc.

Example: in the above example of inventory control, we can have modules like Selling, Buying, Exchange of product, etc.

4. INHERITANCE

  • It is the capability of one class of things to inherit the capabilites or properties from another class.
  • Each class inherits the properties of one class and also add some of its own properties.
  • For example, the class 'Car' inherits the properties from the class 'Automobiles' which inherits some of its properties from another class 'Vehicles'.

WHY INHERITANCE?

1. Ensures closeness to real world.

2. Provides reusability. For example to extend a project, you can use the already created classes to inherit their properties and add their own properties at the same time.

3. Inheritance is transitive i.e. if class 'Car' inherits the properties of class 'Automobiles' and it further inherits the properties of class 'Vehicles ' then class 'Car' automatically inherits the properties of class 'Vehicle'.

Example: In the Inventory control example, if the need arises to expand the software. The programmer can simply inherit the properties of the class item if required.

5. POLYMORPHISM

  • A key feature of OOP language.
  • Languages that do not support polymorphism cannot be called as Object Oriented languages.
  • Languages that support Classes but not Polymorphism are Object Based languages.
  • It is the ability for a message or data to be processed in more than one form.
  • For example, the class 'Shape' has the function 'area()'. The objects of the class are like Triangle, Circle, Rectangle. When the function 'area()' is called by each of these objects, it behaves in a different way. Area of Triangle, Circle, Rectangle will be calculated respectively.

1 comment:

Rohit V.K. said...

thanks........
it was really helpful....