Procedural Vs. Object Oriented and Advantages of OOPS
Programming Paradigms
Purists define many ways or models for ways to give instructions to computer systems. These models are also called paradigms. Here we shall discuss two most common and relevant programming paradigms which are relevant in context of advent of C++ as a programming Language. These are
- Procedural Programming Paradigm
- Object Oriented Programming Paradigm
Procedural Programming
Here one uses defined set of instructions which are commands as defined in the language library or one can define their own procedures which can be called whenever desired. The command flow is usually sequential with an option to jump to desired section as per choice of programmer. In the procedural programming the procedures are actual action oriented entities which act on real data items in the memory.
For e.g in a C code.
int a=1;
printf("hello %d",a);
The printf() is a procedure which acts on data item a and string literal “Hello %d” directly.
Object Oriented Programming
Object oriented programming is more near to real life models. For e.g. a map is required to build many houses based on that map, similarly a class is required to build many objects based on that class. Which means the Class is not a real entity while the object is. Object takes up memory space, while the class doesn’t. For e.g.
Class Display
{
int a;
void print()
{
a=1;
cout<<"Hello "<<a;
}
};
Display obj;
Obj.print();
In above example the declaration of class is just a template about how things will happen. It the line Display obj; which allows the memory to be reserved for data items in the class. For e.g. int a; will take space when obj is declared. One can have many objects of a class with different manipulation possibilities.
Advantages of Object Oriented Paradigm
- Templates for data members and actionable procedures can be defined as single named units called classes.
- A declaration of object can be done many times so that different memory blocks based on same class templates can co-exist.
- Class templates can be extended using the inheritance phenomenon and a powerful technique of extending the power of functions can be achieved. For e.g. Interest class can extend to simple interest and compound interest.
- A controlled level of Abstraction and Encapsulation can be achived so that only relevant portion of code and functionality is of concern at a given point of time.
- Different levels of access and visibility can be achieved to keep the programming discipline of not touching unrequired variables accidentally.
- Polymorphism, the ability to take more than one form for the same entity, for functions and operators etc. can be effectively used.
- Programs are better maintainable as we have templates or classes defined at one place.
- Due to limited and definable access to data and procedures, the security for internal (programmer level) and external (user level) can definitely be achieved at a better level.