Articles

OOP vs POP programming model

OOP vs POP programming model

Object oriented programming has emerged as a highly efficient way of programming for the real world, as opposed to procedural oriented programming where one focused on code rather than on the real world problem.The title itself, ‘Object Oriented Programming’ suggests us about its working; it is ofcourse focused around an object, and all the concepts are designed in such a way that the program itself returns to the ‘objects’ of the problem.
 
To understand object oriented programming, we refer to a real world problem and its solution in OOP and POP.
 
For example, we are to make a program where we take the measurements of a wall, let the user choose the type of paint and tell them the cost of painting that wall, including the painter’s charges.
 
Procedural Oriented Programming
If we were to solve this problem in a POP way, we would have focused on how to calculate the area, how to add the charges, and to display the result. One of the POP languages is C. Let us see how the code would look like in C.
 
#include<stdio.h>
#define TYPE1 6
#define TYPE2 8
#define TYPE3 10
#define CHARGES 300
double calculate(double area,int type,double charges){
double cost=0;
switch(type){
case 1:
cost=(area*TYPE1)+charges;
break;
case 2:
cost=(area*TYPE2)+charges;
break;
case 3:
cost=(area*TYPE3)+charges;
break;
default:
printf("\nType Error!");
}
return cost;
}
int main(){
double height=7,breadth=9;
int type = 2;
printf("The Cost of painting a wall with measurements : 
            \nHeight : %.2f \nBreadth : %.2f \nand paint of type %d is Rs. %.2f\n"
           ,height,breadth,type,calculate((height*breadth),type,CHARGES));
return 0;
}

OUTPUT
 
The Cost of painting a wall with measurements : 
Height : 7.00 
Breadth : 9.00 
and paint of type 2 is Rs. 804.00
 
 
Here, input is given in the main function itself for brevity. Here we do not focus any object, but only on the calculation.
 
Object Oriented programming : - If we are to solve the above problem in POP way, we would have to focus on the Object rather than on the calculation. There could be many ways of thinking of a particular problem,one of them is thinking that the object could be the wall,having variables the height and breadth. C++ being an Object Oriented Programming language, lets have a look at the solution in C++.
 
#include<iostream>
using namespace std;
#define TYPE1 6
#define TYPE2 8
#define TYPE3 10
#define CHARGES 300
class Wall{
double height,breadth;
public:
Wall(double h,double b){
height=h;
breadth=b;
}
double area(){
return height*breadth;
}
double cost(int type){
return (area()*type)+CHARGES;
}
void show(int type){
cout<<"The Cost of painting a wall with measurements : \nHeight : "<<height<<"\nBreadth : "<<breadth<<"\nand paint of type "<<type<<" is Rs. "<<cost(type);
}
};
int main(){
Wall w(7.0,9.0);
w.show(TYPE2);
return 0;
}
 
OUTPUT
The Cost of painting a wall with measurements : 
Height : 7
Breadth : 9
and paint of type 8 is Rs. 804
 
In the above program , we focus on the wall as an object and operate all the values as the calculations related to the object. In the main function, we create an object of the class Wall and call  method show from it. Here again input is provided in the program itself and not taken from the user for the sake of brevity.

This article was submitted by:

Name: Prabjot Kaur
Studying:  MCA 
College: RCC Institute of Information Technology,Beliaghata,West Bengal.


Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com
Page Views : 111    Created By : PrabjotK    Approved By : rajneesh   Published Date : Jun 03, 2018  
Please Share this page